Skip to content

Commit 469372c

Browse files
committed
Account for servlet path "/" in EndpointRequest
See gh-12934
1 parent f5f3af7 commit 469372c

File tree

2 files changed

+20
-6
lines changed
  • spring-boot-project/spring-boot-actuator-autoconfigure/src

2 files changed

+20
-6
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private RequestMatcher createDelegate(PathMappedEndpoints pathMappedEndpoints,
198198
if (this.includeLinks
199199
&& StringUtils.hasText(pathMappedEndpoints.getBasePath())) {
200200
delegateMatchers.add(new AntPathRequestMatcher(
201-
servletPath + pathMappedEndpoints.getBasePath()));
201+
computePath(servletPath, pathMappedEndpoints.getBasePath())));
202202
}
203203
return new OrRequestMatcher(delegateMatchers);
204204
}
@@ -229,10 +229,17 @@ private String getEndpointId(Class<?> source) {
229229
private List<RequestMatcher> getDelegateMatchers(String servletPath,
230230
Set<String> paths) {
231231
return paths.stream()
232-
.map((path) -> new AntPathRequestMatcher(servletPath + path + "/**"))
232+
.map((path) -> new AntPathRequestMatcher(computePath(servletPath, path) + "/**"))
233233
.collect(Collectors.toList());
234234
}
235235

236+
private String computePath(String servletPath, String path) {
237+
if (servletPath.equals("/")) {
238+
return path;
239+
}
240+
return servletPath + path;
241+
}
242+
236243
@Override
237244
protected boolean matches(HttpServletRequest request,
238245
Supplier<WebApplicationContext> context) {
@@ -272,11 +279,18 @@ protected void initialized(
272279
private RequestMatcher createDelegate(String path,
273280
WebEndpointProperties properties) {
274281
if (StringUtils.hasText(properties.getBasePath())) {
275-
return new AntPathRequestMatcher(path + properties.getBasePath());
282+
return new AntPathRequestMatcher(computePath(path, properties.getBasePath()));
276283
}
277284
return EMPTY_MATCHER;
278285
}
279286

287+
private String computePath(String servletPath, String path) {
288+
if (servletPath.equals("/")) {
289+
return path;
290+
}
291+
return servletPath + path;
292+
}
293+
280294
@Override
281295
protected boolean matches(HttpServletRequest request,
282296
Supplier<WebApplicationContext> context) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public class EndpointRequestTests {
5252
@Test
5353
public void toAnyEndpointShouldMatchEndpointPath() {
5454
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
55-
assertMatcher(matcher).matches("/actuator/foo");
56-
assertMatcher(matcher).matches("/actuator/bar");
57-
assertMatcher(matcher).matches("/actuator");
55+
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo");
56+
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar");
57+
assertMatcher(matcher, "/actuator", "/").matches("/actuator");
5858
}
5959

6060
@Test

0 commit comments

Comments
 (0)