Skip to content

Commit 461202b

Browse files
committed
EndpointRequest uses empty servlet path if not available
Fixes gh-13399
1 parent 7cbbd95 commit 461202b

File tree

2 files changed

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

2 files changed

+35
-6
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,27 @@ protected final boolean matches(HttpServletRequest request,
137137

138138
private RequestMatcher createDelegate(WebApplicationContext context) {
139139
try {
140-
RequestMatcherFactory requestMatcherFactory = new RequestMatcherFactory(
141-
context.getBean(DispatcherServletPathProvider.class)
142-
.getServletPath());
140+
String servletPath = getServletPath(context);
141+
RequestMatcherFactory requestMatcherFactory = (StringUtils
142+
.hasText(servletPath) ? new RequestMatcherFactory(servletPath)
143+
: RequestMatcherFactory.withEmptyServletPath());
143144
return createDelegate(context, requestMatcherFactory);
144145
}
145146
catch (NoSuchBeanDefinitionException ex) {
146147
return EMPTY_MATCHER;
147148
}
148149
}
149150

151+
private String getServletPath(WebApplicationContext context) {
152+
try {
153+
return context.getBean(DispatcherServletPathProvider.class)
154+
.getServletPath();
155+
}
156+
catch (NoSuchBeanDefinitionException ex) {
157+
return "";
158+
}
159+
}
160+
150161
protected abstract RequestMatcher createDelegate(WebApplicationContext context,
151162
RequestMatcherFactory requestMatcherFactory);
152163

@@ -279,18 +290,25 @@ private static class RequestMatcherFactory {
279290

280291
private final String servletPath;
281292

293+
private static final RequestMatcherFactory EMPTY_SERVLET_PATH = new RequestMatcherFactory(
294+
"");
295+
282296
RequestMatcherFactory(String servletPath) {
283297
this.servletPath = servletPath;
284298
}
285299

286-
public RequestMatcher antPath(String... parts) {
300+
RequestMatcher antPath(String... parts) {
287301
String pattern = (this.servletPath.equals("/") ? "" : this.servletPath);
288302
for (String part : parts) {
289303
pattern += part;
290304
}
291305
return new AntPathRequestMatcher(pattern);
292306
}
293307

308+
static RequestMatcherFactory withEmptyServletPath() {
309+
return EMPTY_SERVLET_PATH;
310+
}
311+
294312
}
295313

296314
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public void toAnyEndpointWhenServletPathNotEmptyShouldMatch() {
8686
assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("", "/actuator/foo");
8787
}
8888

89+
@Test
90+
public void toAnyEndpointWhenDispatcherServletPathProviderNotAvailableUsesEmptyPath() {
91+
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
92+
assertMatcher(matcher, "/actuator", null).matches("/actuator/foo");
93+
assertMatcher(matcher, "/actuator", null).matches("/actuator/bar");
94+
assertMatcher(matcher, "/actuator", null).matches("/actuator");
95+
assertMatcher(matcher, "/actuator", null).doesNotMatch("/actuator/baz");
96+
}
97+
8998
@Test
9099
public void toEndpointClassShouldMatchEndpointPath() {
91100
RequestMatcher matcher = EndpointRequest.to(FooEndpoint.class);
@@ -245,8 +254,10 @@ private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
245254
properties.setBasePath(pathMappedEndpoints.getBasePath());
246255
}
247256
}
248-
DispatcherServletPathProvider pathProvider = () -> servletPath;
249-
context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider);
257+
if (servletPath != null) {
258+
DispatcherServletPathProvider pathProvider = () -> servletPath;
259+
context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider);
260+
}
250261
return assertThat(new RequestMatcherAssert(context, matcher));
251262
}
252263

0 commit comments

Comments
 (0)