Browse Source

Polish 'Migrate from AntPathRequestMatcher to PathPatternRequestMatcher'

See gh-45163
pull/45173/head
Phillip Webb 8 months ago
parent
commit
d5505ca3f4
  1. 15
      spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java
  2. 7
      spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/PathPatternRequestMatcherProvider.java
  3. 4
      spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java
  4. 4
      spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java
  5. 14
      spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java
  6. 14
      spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfigurationTests.java

15
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 @Override
public void customize(WebSecurity web) { public void customize(WebSecurity web) {
List<RequestMatcher> requestMatchers = new ArrayList<>(); List<RequestMatcher> matchers = new ArrayList<>();
this.pathMappedEndpoints.getAllPaths() this.pathMappedEndpoints.getAllPaths().forEach((path) -> matchers.add(pathMatcher(path + "/**")));
.forEach((path) -> requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(path + "/**"))); matchers.add(pathMatcher(BASE_PATH));
requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(BASE_PATH)); matchers.add(pathMatcher(BASE_PATH + "/"));
requestMatchers.add(PathPatternRequestMatcher.withDefaults().matcher(BASE_PATH + "/")); web.ignoring().requestMatchers(new OrRequestMatcher(matchers));
web.ignoring().requestMatchers(new OrRequestMatcher(requestMatchers)); }
private PathPatternRequestMatcher pathMatcher(String path) {
return PathPatternRequestMatcher.withDefaults().matcher(path);
} }
} }

7
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/AntPathRequestMatcherProvider.java → 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 Madhura Bhave
* @author Chris Bono * @author Chris Bono
*/ */
class AntPathRequestMatcherProvider implements RequestMatcherProvider { class PathPatternRequestMatcherProvider implements RequestMatcherProvider {
private final Function<String, String> pathFactory; private final Function<String, String> pathFactory;
AntPathRequestMatcherProvider(Function<String, String> pathFactory) { PathPatternRequestMatcherProvider(Function<String, String> pathFactory) {
this.pathFactory = pathFactory; this.pathFactory = pathFactory;
} }
@Override @Override
public RequestMatcher getRequestMatcher(String pattern, HttpMethod httpMethod) { public RequestMatcher getRequestMatcher(String pattern, HttpMethod httpMethod) {
String path = this.pathFactory.apply(pattern); return PathPatternRequestMatcher.withDefaults().matcher(httpMethod, this.pathFactory.apply(pattern));
return PathPatternRequestMatcher.withDefaults().matcher(httpMethod, path);
} }
} }

4
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 @ConditionalOnMissingBean
@ConditionalOnClass(DispatcherServlet.class) @ConditionalOnClass(DispatcherServlet.class)
public RequestMatcherProvider requestMatcherProvider(DispatcherServletPath servletPath) { public RequestMatcherProvider requestMatcherProvider(DispatcherServletPath servletPath) {
return new AntPathRequestMatcherProvider(servletPath::getRelativePath); return new PathPatternRequestMatcherProvider(servletPath::getRelativePath);
} }
} }
@ -65,7 +65,7 @@ public class SecurityRequestMatchersManagementContextConfiguration {
@Bean @Bean
public RequestMatcherProvider requestMatcherProvider(JerseyApplicationPath applicationPath) { public RequestMatcherProvider requestMatcherProvider(JerseyApplicationPath applicationPath) {
return new AntPathRequestMatcherProvider(applicationPath::getRelativePath); return new PathPatternRequestMatcherProvider(applicationPath::getRelativePath);
} }
} }

4
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"); throw new IllegalStateException("No FilterChainProxy found");
} }
private static void testCloudFoundrySecurity(MockHttpServletRequest request, String servletPath, private static void testCloudFoundrySecurity(MockHttpServletRequest request, String requestUri,
SecurityFilterChain chain) { SecurityFilterChain chain) {
request.setRequestURI(servletPath); request.setRequestURI(requestUri);
assertThat(chain.matches(request)).isTrue(); assertThat(chain.matches(request)).isTrue();
} }

14
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(); assertThat(this.matcher.matches(request)).as("Matches " + getRequestPath(request)).isTrue();
} }
void doesNotMatch(String servletPath) { void doesNotMatch(String requestUri) {
doesNotMatch(mockRequest(null, servletPath)); doesNotMatch(mockRequest(null, requestUri));
} }
void doesNotMatch(HttpMethod httpMethod, String servletPath) { void doesNotMatch(HttpMethod httpMethod, String requestUri) {
doesNotMatch(mockRequest(httpMethod, servletPath)); doesNotMatch(mockRequest(httpMethod, requestUri));
} }
private void doesNotMatch(HttpServletRequest request) { private void doesNotMatch(HttpServletRequest request) {
assertThat(this.matcher.matches(request)).as("Does not match " + getRequestPath(request)).isFalse(); 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(); MockServletContext servletContext = new MockServletContext();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
MockHttpServletRequest request = new MockHttpServletRequest(servletContext); MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
if (servletPath != null) { if (requestUri != null) {
request.setRequestURI(servletPath); request.setRequestURI(requestUri);
} }
if (httpMethod != null) { if (httpMethod != null) {
request.setMethod(httpMethod.name()); request.setMethod(httpMethod.name());

14
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 @Test
void registersRequestMatcherProviderIfMvcPresent() { void registersRequestMatcherProviderIfMvcPresent() {
this.contextRunner.withUserConfiguration(TestMvcConfiguration.class).run((context) -> { 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); RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null);
assertThat(requestMatcher).extracting("pattern") assertThat(requestMatcher).extracting("pattern")
.isEqualTo(PathPatternParser.defaultInstance.parse("/custom/example")); .isEqualTo(PathPatternParser.defaultInstance.parse("/custom/example"));
@ -71,7 +72,8 @@ class SecurityRequestMatchersManagementContextConfigurationTests {
this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet")) this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet"))
.withUserConfiguration(TestJerseyConfiguration.class) .withUserConfiguration(TestJerseyConfiguration.class)
.run((context) -> { .run((context) -> {
AntPathRequestMatcherProvider matcherProvider = context.getBean(AntPathRequestMatcherProvider.class); PathPatternRequestMatcherProvider matcherProvider = context
.getBean(PathPatternRequestMatcherProvider.class);
RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null); RequestMatcher requestMatcher = matcherProvider.getRequestMatcher("/example", null);
assertThat(requestMatcher).extracting("pattern") assertThat(requestMatcher).extracting("pattern")
.isEqualTo(PathPatternParser.defaultInstance.parse("/admin/example")); .isEqualTo(PathPatternParser.defaultInstance.parse("/admin/example"));
@ -81,20 +83,20 @@ class SecurityRequestMatchersManagementContextConfigurationTests {
@Test @Test
void mvcRequestMatcherProviderConditionalOnDispatcherServletClass() { void mvcRequestMatcherProviderConditionalOnDispatcherServletClass() {
this.contextRunner.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet")) 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 @Test
void mvcRequestMatcherProviderConditionalOnDispatcherServletPathBean() { void mvcRequestMatcherProviderConditionalOnDispatcherServletPathBean() {
new WebApplicationContextRunner() new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class)) .withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class))
.run((context) -> assertThat(context).doesNotHaveBean(AntPathRequestMatcherProvider.class)); .run((context) -> assertThat(context).doesNotHaveBean(PathPatternRequestMatcherProvider.class));
} }
@Test @Test
void jerseyRequestMatcherProviderConditionalOnResourceConfigClass() { void jerseyRequestMatcherProviderConditionalOnResourceConfigClass() {
this.contextRunner.withClassLoader(new FilteredClassLoader("org.glassfish.jersey.server.ResourceConfig")) 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 @Test
@ -102,7 +104,7 @@ class SecurityRequestMatchersManagementContextConfigurationTests {
new WebApplicationContextRunner() new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class)) .withConfiguration(AutoConfigurations.of(SecurityRequestMatchersManagementContextConfiguration.class))
.withClassLoader(new FilteredClassLoader("org.springframework.web.servlet.DispatcherServlet")) .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) @Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save