Skip to content

Commit e8ed0f1

Browse files
committed
Use PathPatternRequestMatcher in web
Issue gh-16887
1 parent 3e53cc2 commit e8ed0f1

21 files changed

+79
-387
lines changed

web/src/main/java/org/springframework/security/web/access/HandlerMappingIntrospectorRequestTransformer.java

Lines changed: 0 additions & 98 deletions
This file was deleted.

web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@
4646
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
4747
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
4848
import org.springframework.security.web.context.SecurityContextRepository;
49-
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
5049
import org.springframework.security.web.util.matcher.RequestMatcher;
5150
import org.springframework.util.Assert;
5251
import org.springframework.web.filter.GenericFilterBean;
5352

53+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
54+
5455
/**
5556
* Abstract processor of browser-based HTTP-based authentication requests.
5657
*
@@ -395,7 +396,7 @@ public void setAuthenticationManager(AuthenticationManager authenticationManager
395396
* @param filterProcessesUrl
396397
*/
397398
public void setFilterProcessesUrl(String filterProcessesUrl) {
398-
setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher(filterProcessesUrl));
399+
setRequiresAuthenticationRequestMatcher(pathPattern(filterProcessesUrl));
399400
}
400401

401402
public final void setRequiresAuthenticationRequestMatcher(RequestMatcher requestMatcher) {

web/src/main/java/org/springframework/security/web/authentication/logout/LogoutFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
import org.springframework.security.core.Authentication;
3030
import org.springframework.security.core.context.SecurityContextHolder;
3131
import org.springframework.security.core.context.SecurityContextHolderStrategy;
32-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
3332
import org.springframework.security.web.util.UrlUtils;
3433
import org.springframework.security.web.util.matcher.RequestMatcher;
3534
import org.springframework.util.Assert;
3635
import org.springframework.util.StringUtils;
3736
import org.springframework.web.filter.GenericFilterBean;
3837

38+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
39+
3940
/**
4041
* Logs a principal out.
4142
* <p>
@@ -140,7 +141,7 @@ public void setLogoutRequestMatcher(RequestMatcher logoutRequestMatcher) {
140141
}
141142

142143
public void setFilterProcessesUrl(String filterProcessesUrl) {
143-
this.logoutRequestMatcher = PathPatternRequestMatcher.withDefaults().matcher(filterProcessesUrl);
144+
this.logoutRequestMatcher = pathPattern(filterProcessesUrl);
144145
}
145146

146147
}

web/src/main/java/org/springframework/security/web/authentication/ott/OneTimeTokenAuthenticationFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import org.springframework.http.HttpMethod;
2020
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
21-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
21+
22+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
2223

2324
/**
2425
* Filter that processes a one-time token for log in.
@@ -34,7 +35,7 @@ public final class OneTimeTokenAuthenticationFilter extends AbstractAuthenticati
3435
public static final String DEFAULT_LOGIN_PROCESSING_URL = "/login/ott";
3536

3637
public OneTimeTokenAuthenticationFilter() {
37-
super(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, DEFAULT_LOGIN_PROCESSING_URL));
38+
super(pathPattern(HttpMethod.POST, DEFAULT_LOGIN_PROCESSING_URL));
3839
setAuthenticationConverter(new OneTimeTokenAuthenticationConverter());
3940
}
4041

web/src/main/java/org/springframework/security/web/authentication/switchuser/SwitchUserFilter.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@
6363
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
6464
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
6565
import org.springframework.security.web.context.SecurityContextRepository;
66-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
6766
import org.springframework.security.web.util.UrlUtils;
68-
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
6967
import org.springframework.security.web.util.matcher.RequestMatcher;
7068
import org.springframework.util.Assert;
7169
import org.springframework.web.filter.GenericFilterBean;
72-
import org.springframework.web.util.UrlPathHelper;
70+
71+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
7372

7473
/**
7574
* Switch User processing filter responsible for user context switching.
@@ -129,9 +128,9 @@ public class SwitchUserFilter extends GenericFilterBean implements ApplicationEv
129128

130129
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
131130

132-
private RequestMatcher exitUserMatcher = createMatcher("/logout/impersonate", true);
131+
private RequestMatcher exitUserMatcher = createMatcher("/logout/impersonate");
133132

134-
private RequestMatcher switchUserMatcher = createMatcher("/login/impersonate", true);
133+
private RequestMatcher switchUserMatcher = createMatcher("/login/impersonate");
135134

136135
private String targetUrl;
137136

@@ -408,7 +407,7 @@ public void setUserDetailsService(UserDetailsService userDetailsService) {
408407
public void setExitUserUrl(String exitUserUrl) {
409408
Assert.isTrue(UrlUtils.isValidRedirectUrl(exitUserUrl),
410409
"exitUserUrl cannot be empty and must be a valid redirect URL");
411-
this.exitUserMatcher = createMatcher(exitUserUrl, false);
410+
this.exitUserMatcher = createMatcher(exitUserUrl);
412411
}
413412

414413
/**
@@ -428,7 +427,7 @@ public void setExitUserMatcher(RequestMatcher exitUserMatcher) {
428427
public void setSwitchUserUrl(String switchUserUrl) {
429428
Assert.isTrue(UrlUtils.isValidRedirectUrl(switchUserUrl),
430429
"switchUserUrl cannot be empty and must be a valid redirect URL");
431-
this.switchUserMatcher = createMatcher(switchUserUrl, false);
430+
this.switchUserMatcher = createMatcher(switchUserUrl);
432431
}
433432

434433
/**
@@ -547,11 +546,8 @@ public void setSecurityContextRepository(SecurityContextRepository securityConte
547546
this.securityContextRepository = securityContextRepository;
548547
}
549548

550-
private static RequestMatcher createMatcher(String pattern, boolean usePathPatterns) {
551-
if (usePathPatterns) {
552-
return PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, pattern);
553-
}
554-
return new AntPathRequestMatcher(pattern, "POST", true, new UrlPathHelper());
549+
private static RequestMatcher createMatcher(String pattern) {
550+
return pathPattern(HttpMethod.POST, pattern);
555551
}
556552

557553
}

web/src/main/java/org/springframework/security/web/authentication/ui/DefaultLogoutPageGeneratingFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
import org.springframework.core.log.LogMessage;
3030
import org.springframework.http.HttpMethod;
31-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
3231
import org.springframework.security.web.util.matcher.RequestMatcher;
3332
import org.springframework.util.Assert;
3433
import org.springframework.web.filter.OncePerRequestFilter;
3534

35+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
36+
3637
/**
3738
* Generates a default log out page.
3839
*
@@ -41,7 +42,7 @@
4142
*/
4243
public class DefaultLogoutPageGeneratingFilter extends OncePerRequestFilter {
4344

44-
private RequestMatcher matcher = PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, "/logout");
45+
private RequestMatcher matcher = pathPattern(HttpMethod.GET, "/logout");
4546

4647
private Function<HttpServletRequest, Map<String, String>> resolveHiddenInputs = (request) -> Collections.emptyMap();
4748

web/src/main/java/org/springframework/security/web/authentication/ui/DefaultResourcesFilter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import org.springframework.core.io.ClassPathResource;
2929
import org.springframework.http.HttpMethod;
3030
import org.springframework.http.MediaType;
31-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
3231
import org.springframework.security.web.util.matcher.RequestMatcher;
3332
import org.springframework.util.Assert;
3433
import org.springframework.web.filter.GenericFilterBean;
3534

35+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
36+
3637
/**
3738
* Serve common static assets used in default UIs, such as CSS or Javascript files. For
3839
* internal use only.
@@ -89,8 +90,7 @@ public String toString() {
8990
* @return -
9091
*/
9192
public static DefaultResourcesFilter css() {
92-
return new DefaultResourcesFilter(
93-
PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, "/default-ui.css"),
93+
return new DefaultResourcesFilter(pathPattern(HttpMethod.GET, "/default-ui.css"),
9494
new ClassPathResource("org/springframework/security/default-ui.css"),
9595
new MediaType("text", "css", StandardCharsets.UTF_8));
9696
}
@@ -107,8 +107,7 @@ public static DefaultResourcesFilter css() {
107107
* @return -
108108
*/
109109
public static DefaultResourcesFilter webauthn() {
110-
return new DefaultResourcesFilter(
111-
PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, "/login/webauthn.js"),
110+
return new DefaultResourcesFilter(pathPattern(HttpMethod.GET, "/login/webauthn.js"),
112111
new ClassPathResource("org/springframework/security/spring-security-webauthn.js"),
113112
new MediaType("text", "javascript", StandardCharsets.UTF_8));
114113
}

web/src/main/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public String toString() {
202202
* <p>
203203
* To match a request URI like {@code /app/servlet/my/resource/**} where {@code /app}
204204
* is the context path, you can do
205-
* {@code PathPatternRequestMatcher.withDefaults().matcher("/servlet/my/resource/**")}
205+
* {@code PathPatternRequestMatcher.pathPattern("/servlet/my/resource/**")}
206206
*
207207
* <p>
208208
* If you have many paths that have a common path prefix, you can use

web/src/main/java/org/springframework/security/web/webauthn/authentication/WebAuthnAuthenticationFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
import org.springframework.security.web.authentication.HttpMessageConverterAuthenticationSuccessHandler;
3838
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
3939
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
40-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
4140
import org.springframework.security.web.webauthn.api.AuthenticatorAssertionResponse;
4241
import org.springframework.security.web.webauthn.api.PublicKeyCredential;
4342
import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions;
4443
import org.springframework.security.web.webauthn.jackson.WebauthnJackson2Module;
4544
import org.springframework.security.web.webauthn.management.RelyingPartyAuthenticationRequest;
4645
import org.springframework.util.Assert;
4746

47+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
48+
4849
/**
4950
* Authenticates {@code PublicKeyCredential<AuthenticatorAssertionResponse>} that is
5051
* parsed from the body of the {@link HttpServletRequest} using the
@@ -77,7 +78,7 @@ public class WebAuthnAuthenticationFilter extends AbstractAuthenticationProcessi
7778
private PublicKeyCredentialRequestOptionsRepository requestOptionsRepository = new HttpSessionPublicKeyCredentialRequestOptionsRepository();
7879

7980
public WebAuthnAuthenticationFilter() {
80-
super(PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, "/login/webauthn"));
81+
super(pathPattern(HttpMethod.POST, "/login/webauthn"));
8182
setSecurityContextRepository(new HttpSessionSecurityContextRepository());
8283
setAuthenticationFailureHandler(
8384
new AuthenticationEntryPointFailureHandler(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)));

web/src/test/java/org/springframework/security/web/RequestMatcherRedirectFilterTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.mock.web.MockHttpServletRequest;
2424
import org.springframework.mock.web.MockHttpServletResponse;
25-
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
2625

2726
import static org.assertj.core.api.Assertions.assertThat;
2827
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2928
import static org.mockito.Mockito.mock;
3029
import static org.mockito.Mockito.verify;
3130
import static org.mockito.Mockito.verifyNoInteractions;
3231
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.get;
32+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
3333

3434
/**
3535
* Tests for {@link RequestMatcherRedirectFilter}.
@@ -40,8 +40,7 @@ public class RequestMatcherRedirectFilterTests {
4040

4141
@Test
4242
public void doFilterWhenRequestMatchThenRedirectToSpecifiedUrl() throws Exception {
43-
RequestMatcherRedirectFilter filter = new RequestMatcherRedirectFilter(new AntPathRequestMatcher("/context"),
44-
"/test");
43+
RequestMatcherRedirectFilter filter = new RequestMatcherRedirectFilter(pathPattern("/context"), "/test");
4544

4645
MockHttpServletRequest request = get("/context").build();
4746
MockHttpServletResponse response = new MockHttpServletResponse();
@@ -57,8 +56,7 @@ public void doFilterWhenRequestMatchThenRedirectToSpecifiedUrl() throws Exceptio
5756

5857
@Test
5958
public void doFilterWhenRequestNotMatchThenNextFilter() throws Exception {
60-
RequestMatcherRedirectFilter filter = new RequestMatcherRedirectFilter(new AntPathRequestMatcher("/context"),
61-
"/test");
59+
RequestMatcherRedirectFilter filter = new RequestMatcherRedirectFilter(pathPattern("/context"), "/test");
6260

6361
MockHttpServletRequest request = get("/test").build();
6462

@@ -81,21 +79,19 @@ public void constructWhenRequestMatcherNull() {
8179
@Test
8280
public void constructWhenRedirectUrlNull() {
8381
assertThatIllegalArgumentException()
84-
.isThrownBy(() -> new RequestMatcherRedirectFilter(new AntPathRequestMatcher("/**"), null))
82+
.isThrownBy(() -> new RequestMatcherRedirectFilter(pathPattern("/**"), null))
8583
.withMessage("redirectUrl cannot be empty");
8684
}
8785

8886
@Test
8987
public void constructWhenRedirectUrlEmpty() {
90-
assertThatIllegalArgumentException()
91-
.isThrownBy(() -> new RequestMatcherRedirectFilter(new AntPathRequestMatcher("/**"), ""))
88+
assertThatIllegalArgumentException().isThrownBy(() -> new RequestMatcherRedirectFilter(pathPattern("/**"), ""))
9289
.withMessage("redirectUrl cannot be empty");
9390
}
9491

9592
@Test
9693
public void constructWhenRedirectUrlBlank() {
97-
assertThatIllegalArgumentException()
98-
.isThrownBy(() -> new RequestMatcherRedirectFilter(new AntPathRequestMatcher("/**"), " "))
94+
assertThatIllegalArgumentException().isThrownBy(() -> new RequestMatcherRedirectFilter(pathPattern("/**"), " "))
9995
.withMessage("redirectUrl cannot be empty");
10096
}
10197

0 commit comments

Comments
 (0)