Skip to content

Commit d5505ca

Browse files
committed
Polish 'Migrate from AntPathRequestMatcher to PathPatternRequestMatcher'
See gh-45163
1 parent 106e9f5 commit d5505ca

File tree

6 files changed

+31
-27
lines changed

6 files changed

+31
-27
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,15 @@ static class IgnoredCloudFoundryPathsWebSecurityCustomizer implements WebSecurit
185185

186186
@Override
187187
public void customize(WebSecurity web) {
188-
List<RequestMatcher> requestMatchers = new ArrayList<>();
189-
this.pathMappedEndpoints.getAllPaths()
190-
.forEach((path) -> requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(path + "/**")));
191-
requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(BASE_PATH));
192-
requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(BASE_PATH + "/"));
193-
web.ignoring().requestMatchers(new OrRequestMatcher(requestMatchers));
188+
List<RequestMatcher> matchers = new ArrayList<>();
189+
this.pathMappedEndpoints.getAllPaths().forEach((path) -> matchers.add(pathMatcher(path + "/**")));
190+
matchers.add(pathMatcher(BASE_PATH));
191+
matchers.add(pathMatcher(BASE_PATH + "/"));
192+
web.ignoring().requestMatchers(new OrRequestMatcher(matchers));
193+
}
194+
195+
private PathPatternRequestMatcher pathMatcher(String path) {
196+
return PathPatternRequestMatcher.withDefaults().matcher(path);
194197
}
195198

196199
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@
2828
* @author Madhura Bhave
2929
* @author Chris Bono
3030
*/
31-
class AntPathRequestMatcherProvider implements RequestMatcherProvider {
31+
class PathPatternRequestMatcherProvider implements RequestMatcherProvider {
3232

3333
private final Function<String, String> pathFactory;
3434

35-
AntPathRequestMatcherProvider(Function<String, String> pathFactory) {
35+
PathPatternRequestMatcherProvider(Function<String, String> pathFactory) {
3636
this.pathFactory = pathFactory;
3737
}
3838

3939
@Override
4040
public RequestMatcher getRequestMatcher(String pattern, HttpMethod httpMethod) {
41-
String path = this.pathFactory.apply(pattern);
42-
return PathPatternRequestMatcher.withDefaults().matcher(httpMethod, path);
41+
return PathPatternRequestMatcher.withDefaults().matcher(httpMethod, this.pathFactory.apply(pattern));
4342
}
4443

4544
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static class MvcRequestMatcherConfiguration {
5252
@ConditionalOnMissingBean
5353
@ConditionalOnClass(DispatcherServlet.class)
5454
public RequestMatcherProvider requestMatcherProvider(DispatcherServletPath servletPath) {
55-
return new AntPathRequestMatcherProvider(servletPath::getRelativePath);
55+
return new PathPatternRequestMatcherProvider(servletPath::getRelativePath);
5656
}
5757

5858
}
@@ -65,7 +65,7 @@ public static class JerseyRequestMatcherConfiguration {
6565

6666
@Bean
6767
public RequestMatcherProvider requestMatcherProvider(JerseyApplicationPath applicationPath) {
68-
return new AntPathRequestMatcherProvider(applicationPath::getRelativePath);
68+
return new PathPatternRequestMatcherProvider(applicationPath::getRelativePath);
6969
}
7070

7171
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ private FilterChainProxy getFilterChainProxy(Filter filter) {
211211
throw new IllegalStateException("No FilterChainProxy found");
212212
}
213213

214-
private static void testCloudFoundrySecurity(MockHttpServletRequest request, String servletPath,
214+
private static void testCloudFoundrySecurity(MockHttpServletRequest request, String requestUri,
215215
SecurityFilterChain chain) {
216-
request.setRequestURI(servletPath);
216+
request.setRequestURI(requestUri);
217217
assertThat(chain.matches(request)).isTrue();
218218
}
219219

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,24 +413,24 @@ private void matches(HttpServletRequest request) {
413413
assertThat(this.matcher.matches(request)).as("Matches " + getRequestPath(request)).isTrue();
414414
}
415415

416-
void doesNotMatch(String servletPath) {
417-
doesNotMatch(mockRequest(null, servletPath));
416+
void doesNotMatch(String requestUri) {
417+
doesNotMatch(mockRequest(null, requestUri));
418418
}
419419

420-
void doesNotMatch(HttpMethod httpMethod, String servletPath) {
421-
doesNotMatch(mockRequest(httpMethod, servletPath));
420+
void doesNotMatch(HttpMethod httpMethod, String requestUri) {
421+
doesNotMatch(mockRequest(httpMethod, requestUri));
422422
}
423423

424424
private void doesNotMatch(HttpServletRequest request) {
425425
assertThat(this.matcher.matches(request)).as("Does not match " + getRequestPath(request)).isFalse();
426426
}
427427

428-
private MockHttpServletRequest mockRequest(HttpMethod httpMethod, String servletPath) {
428+
private MockHttpServletRequest mockRequest(HttpMethod httpMethod, String requestUri) {
429429
MockServletContext servletContext = new MockServletContext();
430430
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
431431
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
432-
if (servletPath != null) {
433-
request.setRequestURI(servletPath);
432+
if (requestUri != null) {
433+
request.setRequestURI(requestUri);
434434
}
435435
if (httpMethod != null) {
436436
request.setMethod(httpMethod.name());

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ void configurationConditionalOnRequestMatcherClass() {
5959
@Test
6060
void registersRequestMatcherProviderIfMvcPresent() {
6161
this.contextRunner.withUserConfiguration(TestMvcConfiguration.class).run((context) -> {
62-
AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class);
62+
PathPatternRequestMatcherProvider matcherProvider = context
63+
.getBean(PathPatternRequestMatcherProvider.class);
6364
RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null);
6465
assertThat(requestMatcher).extracting("pattern")
6566
.isEqualTo(PathPatternParser.defaultInstance.parse("/custom/example"));
@@ -71,7 +72,8 @@ void registersRequestMatcherForJerseyProviderIfJerseyPresentAndMvcAbsent() {
7172
this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet"))
7273
.withUserConfiguration(TestJerseyConfiguration.class)
7374
.run((context) -> {
74-
AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class);
75+
PathPatternRequestMatcherProvider matcherProvider = context
76+
.getBean(PathPatternRequestMatcherProvider.class);
7577
RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null);
7678
assertThat(requestMatcher).extracting("pattern")
7779
.isEqualTo(PathPatternParser.defaultInstance.parse("/admin/example"));
@@ -81,28 +83,28 @@ void registersRequestMatcherForJerseyProviderIfJerseyPresentAndMvcAbsent() {
8183
@Test
8284
void mvcRequestMatcherProviderConditionalOnDispatcherServletClass() {
8385
this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet"))
84-
.run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class));
86+
.run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class));
8587
}
8688

8789
@Test
8890
void mvcRequestMatcherProviderConditionalOnDispatcherServletPathBean() {
8991
new WebApplicationContextRunner()
9092
.withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class))
91-
.run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class));
93+
.run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class));
9294
}
9395

9496
@Test
9597
void jerseyRequestMatcherProviderConditionalOnResourceConfigClass() {
9698
this.contextRunner.withClassLoader(new FilteredClassLoader("org.glassfish.jersey.server.ResourceConfig"))
97-
.run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class));
99+
.run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class));
98100
}
99101

100102
@Test
101103
void jerseyRequestMatcherProviderConditionalOnJerseyApplicationPathBean() {
102104
new WebApplicationContextRunner()
103105
.withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class))
104106
.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet"))
105-
.run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class));
107+
.run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class));
106108
}
107109

108110
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)