From d5505ca3f40a23f03bf681d0fd984881ca1fe5a5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 11 Apr 2025 17:23:44 -0700 Subject: [PATCH] Polish 'Migrate from AntPathRequestMatcher to PathPatternRequestMatcher' See gh-45163 --- .../CloudFoundryActuatorAutoConfiguration.java | 15 +++++++++------ ...ava => PathPatternRequestMatcherProvider.java} | 7 +++---- ...estMatchersManagementContextConfiguration.java | 4 ++-- ...loudFoundryActuatorAutoConfigurationTests.java | 4 ++-- .../security/servlet/EndpointRequestTests.java | 14 +++++++------- ...tchersManagementContextConfigurationTests.java | 14 ++++++++------ 6 files changed, 31 insertions(+), 27 deletions(-) rename spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/{AntPathRequestMatcherProvider.java => PathPatternRequestMatcherProvider.java} (86%) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java index b1c95992be..ab4405f3b6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java @@ -185,12 +185,15 @@ public class CloudFoundryActuatorAutoConfiguration { @Override public void customize(WebSecurity web) { - List requestMatchers = new ArrayList<>(); - this.pathMappedEndpoints.getAllPaths() - .forEach((path) -> requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(path + "/**"))); - requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(BASE_PATH)); - requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(BASE_PATH + "/")); - web.ignoring().requestMatchers(new OrRequestMatcher(requestMatchers)); + List matchers = new ArrayList<>(); + this.pathMappedEndpoints.getAllPaths().forEach((path) -> matchers.add(pathMatcher(path + "/**"))); + matchers.add(pathMatcher(BASE_PATH)); + matchers.add(pathMatcher(BASE_PATH + "/")); + web.ignoring().requestMatchers(new OrRequestMatcher(matchers)); + } + + private PathPatternRequestMatcher pathMatcher(String path) { + return PathPatternRequestMatcher.withDefaults().matcher(path); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/AntPathRequestMatcherProvider.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/PathPatternRequestMatcherProvider.java similarity index 86% rename from spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/AntPathRequestMatcherProvider.java rename to spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/PathPatternRequestMatcherProvider.java index 16e21d9b75..091cff1807 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/AntPathRequestMatcherProvider.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/PathPatternRequestMatcherProvider.java @@ -28,18 +28,17 @@ import org.springframework.security.web.util.matcher.RequestMatcher; * @author Madhura Bhave * @author Chris Bono */ -class AntPathRequestMatcherProvider implements RequestMatcherProvider { +class PathPatternRequestMatcherProvider implements RequestMatcherProvider { private final Function pathFactory; - AntPathRequestMatcherProvider(Function pathFactory) { + PathPatternRequestMatcherProvider(Function pathFactory) { this.pathFactory = pathFactory; } @Override public RequestMatcher getRequestMatcher(String pattern, HttpMethod httpMethod) { - String path = this.pathFactory.apply(pattern); - return PathPatternRequestMatcher.withDefaults().matcher(httpMethod, path); + return PathPatternRequestMatcher.withDefaults().matcher(httpMethod, this.pathFactory.apply(pattern)); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java index 260620f34f..142854c76f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java @@ -52,7 +52,7 @@ public class SecurityRequestMatchersManagementContextConfiguration { @ConditionalOnMissingBean @ConditionalOnClass(DispatcherServlet.class) public RequestMatcherProvider requestMatcherProvider(DispatcherServletPath servletPath) { - return new AntPathRequestMatcherProvider(servletPath::getRelativePath); + return new PathPatternRequestMatcherProvider(servletPath::getRelativePath); } } @@ -65,7 +65,7 @@ public class SecurityRequestMatchersManagementContextConfiguration { @Bean public RequestMatcherProvider requestMatcherProvider(JerseyApplicationPath applicationPath) { - return new AntPathRequestMatcherProvider(applicationPath::getRelativePath); + return new PathPatternRequestMatcherProvider(applicationPath::getRelativePath); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java index e9bb84386d..d5dbe6f481 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java @@ -211,9 +211,9 @@ class CloudFoundryActuatorAutoConfigurationTests { throw new IllegalStateException("No FilterChainProxy found"); } - private static void testCloudFoundrySecurity(MockHttpServletRequest request, String servletPath, + private static void testCloudFoundrySecurity(MockHttpServletRequest request, String requestUri, SecurityFilterChain chain) { - request.setRequestURI(servletPath); + request.setRequestURI(requestUri); assertThat(chain.matches(request)).isTrue(); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java index 3d90fa4343..1f1cea6886 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java @@ -413,24 +413,24 @@ class EndpointRequestTests { assertThat(this.matcher.matches(request)).as("Matches " + getRequestPath(request)).isTrue(); } - void doesNotMatch(String servletPath) { - doesNotMatch(mockRequest(null, servletPath)); + void doesNotMatch(String requestUri) { + doesNotMatch(mockRequest(null, requestUri)); } - void doesNotMatch(HttpMethod httpMethod, String servletPath) { - doesNotMatch(mockRequest(httpMethod, servletPath)); + void doesNotMatch(HttpMethod httpMethod, String requestUri) { + doesNotMatch(mockRequest(httpMethod, requestUri)); } private void doesNotMatch(HttpServletRequest request) { assertThat(this.matcher.matches(request)).as("Does not match " + getRequestPath(request)).isFalse(); } - private MockHttpServletRequest mockRequest(HttpMethod httpMethod, String servletPath) { + private MockHttpServletRequest mockRequest(HttpMethod httpMethod, String requestUri) { MockServletContext servletContext = new MockServletContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); MockHttpServletRequest request = new MockHttpServletRequest(servletContext); - if (servletPath != null) { - request.setRequestURI(servletPath); + if (requestUri != null) { + request.setRequestURI(requestUri); } if (httpMethod != null) { request.setMethod(httpMethod.name()); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java index c08f6e30cd..44e609536e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java @@ -59,7 +59,8 @@ class SecurityRequestMatchersManagementContextConfigurationTests { @Test void registersRequestMatcherProviderIfMvcPresent() { this.contextRunner.withUserConfiguration(TestMvcConfiguration.class).run((context) -> { - AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class); + PathPatternRequestMatcherProvider matcherProvider = context + .getBean(PathPatternRequestMatcherProvider.class); RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null); assertThat(requestMatcher).extracting("pattern") .isEqualTo(PathPatternParser.defaultInstance.parse("/custom/example")); @@ -71,7 +72,8 @@ class SecurityRequestMatchersManagementContextConfigurationTests { this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet")) .withUserConfiguration(TestJerseyConfiguration.class) .run((context) -> { - AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class); + PathPatternRequestMatcherProvider matcherProvider = context + .getBean(PathPatternRequestMatcherProvider.class); RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null); assertThat(requestMatcher).extracting("pattern") .isEqualTo(PathPatternParser.defaultInstance.parse("/admin/example")); @@ -81,20 +83,20 @@ class SecurityRequestMatchersManagementContextConfigurationTests { @Test void mvcRequestMatcherProviderConditionalOnDispatcherServletClass() { this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet")) - .run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class)); + .run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class)); } @Test void mvcRequestMatcherProviderConditionalOnDispatcherServletPathBean() { new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class)) - .run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class)); + .run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class)); } @Test void jerseyRequestMatcherProviderConditionalOnResourceConfigClass() { this.contextRunner.withClassLoader(new FilteredClassLoader("org.glassfish.jersey.server.ResourceConfig")) - .run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class)); + .run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class)); } @Test @@ -102,7 +104,7 @@ class SecurityRequestMatchersManagementContextConfigurationTests { new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class)) .withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet")) - .run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class)); + .run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class)); } @Configuration(proxyBeanMethods = false)