From 469372c5ef15fe6eee96d4cea536058f9d32dd49 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Fri, 4 May 2018 18:03:18 -0700 Subject: [PATCH] Account for servlet path "/" in EndpointRequest See gh-12934 --- .../security/servlet/EndpointRequest.java | 20 ++++++++++++++++--- .../servlet/EndpointRequestTests.java | 6 +++--- 2 files changed, 20 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 a57a5fdbf0..bee0dedb86 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 @@ -198,7 +198,7 @@ public final class EndpointRequest { if (this.includeLinks && StringUtils.hasText(pathMappedEndpoints.getBasePath())) { delegateMatchers.add(new AntPathRequestMatcher( - servletPath + pathMappedEndpoints.getBasePath())); + computePath(servletPath, pathMappedEndpoints.getBasePath()))); } return new OrRequestMatcher(delegateMatchers); } @@ -229,10 +229,17 @@ public final class EndpointRequest { private List getDelegateMatchers(String servletPath, Set paths) { return paths.stream() - .map((path) -> new AntPathRequestMatcher(servletPath + path + "/**")) + .map((path) -> new AntPathRequestMatcher(computePath(servletPath, path) + "/**")) .collect(Collectors.toList()); } + private String computePath(String servletPath, String path) { + if (servletPath.equals("/")) { + return path; + } + return servletPath + path; + } + @Override protected boolean matches(HttpServletRequest request, Supplier context) { @@ -272,11 +279,18 @@ public final class EndpointRequest { private RequestMatcher createDelegate(String path, WebEndpointProperties properties) { if (StringUtils.hasText(properties.getBasePath())) { - return new AntPathRequestMatcher(path + properties.getBasePath()); + return new AntPathRequestMatcher(computePath(path, properties.getBasePath())); } return EMPTY_MATCHER; } + private String computePath(String servletPath, String path) { + if (servletPath.equals("/")) { + return path; + } + return servletPath + path; + } + @Override protected boolean matches(HttpServletRequest request, Supplier context) { 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 9c4dd82429..603855861b 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 @@ -52,9 +52,9 @@ public class EndpointRequestTests { @Test public void toAnyEndpointShouldMatchEndpointPath() { RequestMatcher matcher = EndpointRequest.toAnyEndpoint(); - assertMatcher(matcher).matches("/actuator/foo"); - assertMatcher(matcher).matches("/actuator/bar"); - assertMatcher(matcher).matches("/actuator"); + assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo"); + assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar"); + assertMatcher(matcher, "/actuator", "/").matches("/actuator"); } @Test