Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -339,6 +339,10 @@ public void setRequiresAuthenticationRequestMatcher(RequestMatcher requiresAuthe
this.requiresAuthenticationRequestMatcher = requiresAuthenticationRequestMatcher;
}

protected RequestMatcher getRequiresAuthenticationRequestMatcher() {
return this.requiresAuthenticationRequestMatcher;
}

/**
* Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
* the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,7 @@
import org.springframework.security.web.authentication.ForwardAuthenticationFailureHandler;
import org.springframework.security.web.authentication.ForwardAuthenticationSuccessHandler;
import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -389,6 +390,41 @@ public void requestMatchesRequestMatcher() throws Exception {
verify(am).authenticate(any(PreAuthenticatedAuthenticationToken.class));
}

@Test
public void customMatchesRequestMatcherWhenMatchPath() throws Exception {
assertThat(customRequiresAuthenticationRequestMatcher("/test")).isNotNull();
}

@Test
public void customMatchesRequestMatcherWhenNoMatchPath() throws Exception {
assertThat(customRequiresAuthenticationRequestMatcher("/hello")).isNull();
}

private Authentication customRequiresAuthenticationRequestMatcher(String servletPath) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServletPath(servletPath);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter() {
@Override
protected void initFilterBean() throws ServletException {
setRequiresAuthenticationRequestMatcher(new AndRequestMatcher(getRequiresAuthenticationRequestMatcher(),
new AntPathRequestMatcher("/test/**")));
super.initFilterBean();
}
};
filter.setAuthenticationManager((authentication) -> {
if (authentication instanceof PreAuthenticatedAuthenticationToken token) {
return new PreAuthenticatedAuthenticationToken(token.getPrincipal(), token.getCredentials(),
AuthorityUtils.createAuthorityList("ROLE_USER"));
}
return null;
});
filter.afterPropertiesSet();
filter.doFilter(request, response, chain);
return SecurityContextHolder.getContext().getAuthentication();
}

private void testDoFilter(boolean grantAccess) throws Exception {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
Expand Down