Skip to content

Commit 8f535b2

Browse files
YongGoosephilwebb
authored andcommitted
Fail fast when base path and an endpoint mapping are set to '/'
Throw an exception if actuator is running on the main server port and the base path and an individual mapping are set to '/'. See gh-45251 Signed-off-by: yongjunhong <dev.yongjunh@gmail.com>
1 parent c46d19f commit 8f535b2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
*
7070
* @author Andy Wilkinson
7171
* @author Phillip Webb
72+
* @author Yongjun Hong
7273
* @since 2.0.0
7374
*/
7475
@ManagementContextConfiguration(proxyBeanMethods = false)
@@ -93,6 +94,18 @@ public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpoint
9394
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
9495
String basePath = webEndpointProperties.getBasePath();
9596
EndpointMapping endpointMapping = new EndpointMapping(basePath);
97+
98+
if (basePath.isEmpty() && ManagementPortType.get(environment).equals(ManagementPortType.SAME)) {
99+
for (ExposableWebEndpoint endpoint : webEndpoints) {
100+
if ("/".equals(endpoint.getRootPath())) {
101+
throw new IllegalStateException(
102+
"Management endpoints and endpoint path are both mapped to '/' on the server port which will "
103+
+ "block access to other endpoints. Please use a different path for management endpoints or "
104+
+ "map them to a dedicated management port.");
105+
}
106+
}
107+
}
108+
96109
boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
97110
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
98111
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package smoketest.actuator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import org.springframework.beans.factory.BeanCreationException;
6+
import org.springframework.boot.SpringApplication;
7+
8+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
9+
10+
/**
11+
* Verifies that an exception is thrown when management and server endpoint paths
12+
* conflict.
13+
*
14+
* @author Yongjun Hong
15+
*/
16+
class ManagementEndpointConflictSmokeTest {
17+
18+
@Test
19+
void shouldThrowExceptionWhenManagementAndServerPathsConflict() {
20+
assertThatThrownBy(() -> {
21+
SpringApplication.run(SampleActuatorApplication.class, "--management.endpoints.web.base-path=/",
22+
"--management.endpoints.web.path-mapping.health=/");
23+
}).isInstanceOf(BeanCreationException.class)
24+
.hasMessageContaining("Management endpoints and endpoint path are both mapped to '/'");
25+
}
26+
27+
}

0 commit comments

Comments
 (0)