From 461202bc25943826617d61c8e323a9c9ca3eda9a Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 6 Jun 2018 10:36:01 -0700 Subject: [PATCH] EndpointRequest uses empty servlet path if not available Fixes gh-13399 --- .../security/servlet/EndpointRequest.java | 26 ++++++++++++++++--- .../servlet/EndpointRequestTests.java | 15 +++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java index 3509740e40..f79289ca6d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java @@ -137,9 +137,10 @@ public final class EndpointRequest { private RequestMatcher createDelegate(WebApplicationContext context) { try { - RequestMatcherFactory requestMatcherFactory = new RequestMatcherFactory( - context.getBean(DispatcherServletPathProvider.class) - .getServletPath()); + String servletPath = getServletPath(context); + RequestMatcherFactory requestMatcherFactory = (StringUtils + .hasText(servletPath) ? new RequestMatcherFactory(servletPath) + : RequestMatcherFactory.withEmptyServletPath()); return createDelegate(context, requestMatcherFactory); } catch (NoSuchBeanDefinitionException ex) { @@ -147,6 +148,16 @@ public final class EndpointRequest { } } + private String getServletPath(WebApplicationContext context) { + try { + return context.getBean(DispatcherServletPathProvider.class) + .getServletPath(); + } + catch (NoSuchBeanDefinitionException ex) { + return ""; + } + } + protected abstract RequestMatcher createDelegate(WebApplicationContext context, RequestMatcherFactory requestMatcherFactory); @@ -279,11 +290,14 @@ public final class EndpointRequest { private final String servletPath; + private static final RequestMatcherFactory EMPTY_SERVLET_PATH = new RequestMatcherFactory( + ""); + RequestMatcherFactory(String servletPath) { this.servletPath = servletPath; } - public RequestMatcher antPath(String... parts) { + RequestMatcher antPath(String... parts) { String pattern = (this.servletPath.equals("/") ? "" : this.servletPath); for (String part : parts) { pattern += part; @@ -291,6 +305,10 @@ public final class EndpointRequest { return new AntPathRequestMatcher(pattern); } + static RequestMatcherFactory withEmptyServletPath() { + return EMPTY_SERVLET_PATH; + } + } } 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 2b59db9c28..12cf156d55 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 @@ -86,6 +86,15 @@ public class EndpointRequestTests { assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("", "/actuator/foo"); } + @Test + public void toAnyEndpointWhenDispatcherServletPathProviderNotAvailableUsesEmptyPath() { + RequestMatcher matcher = EndpointRequest.toAnyEndpoint(); + assertMatcher(matcher, "/actuator", null).matches("/actuator/foo"); + assertMatcher(matcher, "/actuator", null).matches("/actuator/bar"); + assertMatcher(matcher, "/actuator", null).matches("/actuator"); + assertMatcher(matcher, "/actuator", null).doesNotMatch("/actuator/baz"); + } + @Test public void toEndpointClassShouldMatchEndpointPath() { RequestMatcher matcher = EndpointRequest.to(FooEndpoint.class); @@ -245,8 +254,10 @@ public class EndpointRequestTests { properties.setBasePath(pathMappedEndpoints.getBasePath()); } } - DispatcherServletPathProvider pathProvider = () -> servletPath; - context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider); + if (servletPath != null) { + DispatcherServletPathProvider pathProvider = () -> servletPath; + context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider); + } return assertThat(new RequestMatcherAssert(context, matcher)); }