Skip to content

Commit 3b515e6

Browse files
committed
Add fail-fast mechanism when both management base path and endpoint mapping are set to '/'
- Issue spring-projects#43885 Signed-off-by: yongjunhong <[email protected]>
1 parent 5ae53e1 commit 3b515e6

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
*
7070
* @author Andy Wilkinson
7171
* @author Phillip Webb
72+
* @author yongjunhong
7273
* @since 2.0.0
7374
*/
7475
@ManagementContextConfiguration(proxyBeanMethods = false)
@@ -93,6 +94,19 @@ 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+
}
109+
96110
boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
97111
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
98112
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
@@ -154,7 +168,7 @@ static EndpointObjectMapperWebMvcConfigurer endpointObjectMapperWebMvcConfigurer
154168
static class EndpointObjectMapperWebMvcConfigurer implements WebMvcConfigurer {
155169

156170
private static final List<MediaType> MEDIA_TYPES = Collections
157-
.unmodifiableList(Arrays.asList(MediaType.APPLICATION_JSON, new MediaType("application", "*+json")));
171+
.unmodifiableList(Arrays.asList(MediaType.APPLICATION_JSON, new MediaType("application", "*+json")));
158172

159173
private final EndpointObjectMapper endpointObjectMapper;
160174

@@ -181,3 +195,4 @@ private void configure(MappingJackson2HttpMessageConverter converter) {
181195
}
182196

183197
}
198+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 conflict.
12+
*
13+
* @author yongjunhong
14+
*/
15+
class ManagementEndpointConflictSmokeTest {
16+
17+
@Test
18+
void shouldThrowExceptionWhenManagementAndServerPathsConflict() {
19+
assertThatThrownBy(() -> {
20+
SpringApplication.run(SampleActuatorApplication.class,
21+
"--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+
}

0 commit comments

Comments
 (0)