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 { @@ -185,12 +185,15 @@ public class CloudFoundryActuatorAutoConfiguration {
@Override
public void customize(WebSecurity web) {
List<RequestMatcher> 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<RequestMatcher> 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);
}
}

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; @@ -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<String, String> pathFactory;
AntPathRequestMatcherProvider(Function<String, String> pathFactory) {
PathPatternRequestMatcherProvider(Function<String, String> 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));
}
}

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 { @@ -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 { @@ -65,7 +65,7 @@ public class SecurityRequestMatchersManagementContextConfiguration {
@Bean
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 { @@ -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();
}

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 { @@ -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());

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 { @@ -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 { @@ -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 { @@ -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 { @@ -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)

Loading…
Cancel
Save