Skip to content

Commit 4a61e45

Browse files
committed
Require separate management port to use management context path of /
Closes gh-9898
1 parent fb3d79c commit 4a61e45

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public void afterSingletonsInstantiated() {
143143
}
144144
}
145145
if (managementPort == ManagementServerPort.SAME) {
146+
String contextPath = environment.getProperty("management.context-path");
147+
if ("".equals(contextPath) || "/".equals(contextPath)) {
148+
throw new IllegalStateException("A management context path of '"
149+
+ contextPath + "' requires the management server to be "
150+
+ "listening on a separate port");
151+
}
146152
if (environment.getProperty("management.ssl.enabled", Boolean.class, false)) {
147153
throw new IllegalStateException(
148154
"Management-specific SSL cannot be configured as the management "

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public void setUp() {
121121
Ports values = new Ports();
122122
ports.set(values);
123123
TestPropertyValues
124-
.of("management.context-path=", "management.security.enabled=false",
125-
"server.servlet.context-path=",
124+
.of("management.security.enabled=false", "server.servlet.context-path=",
126125
"server.port=" + ports.get().server)
127126
.applyTo(this.applicationContext);
128127
}
@@ -140,9 +139,9 @@ public void onSamePort() throws Exception {
140139
BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class);
141140
this.applicationContext.refresh();
142141
assertContent("/controller", ports.get().server, "controlleroutput");
143-
assertContent("/endpoint", ports.get().server, "endpointoutput");
144-
assertThat(hasHeader("/endpoint", ports.get().server, "X-Application-Context"))
145-
.isFalse();
142+
assertContent("/application/endpoint", ports.get().server, "endpointoutput");
143+
assertThat(hasHeader("/application/endpoint", ports.get().server,
144+
"X-Application-Context")).isFalse();
146145
assertThat(this.applicationContext.containsBean("applicationContextIdFilter"))
147146
.isFalse();
148147
}
@@ -171,6 +170,26 @@ public void onDifferentPort() throws Exception {
171170
assertContent("/controller", ports.get().server, "controlleroutput");
172171
assertContent("/endpoint", ports.get().server, null);
173172
assertContent("/controller", ports.get().management, null);
173+
assertContent("/application/endpoint", ports.get().management, "endpointoutput");
174+
assertContent("/error", ports.get().management, startsWith("{"));
175+
ApplicationContext managementContext = this.applicationContext
176+
.getBean(ManagementContextResolver.class).getApplicationContext();
177+
List<?> interceptors = (List<?>) ReflectionTestUtils.getField(
178+
managementContext.getBean(EndpointHandlerMapping.class), "interceptors");
179+
assertThat(interceptors).hasSize(2);
180+
}
181+
182+
@Test
183+
public void onDifferentPortAndRootContext() throws Exception {
184+
TestPropertyValues.of("management.port=" + ports.get().management,
185+
"management.context-path=/").applyTo(this.applicationContext);
186+
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
187+
DifferentPortConfig.class, BaseConfiguration.class,
188+
EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
189+
this.applicationContext.refresh();
190+
assertContent("/controller", ports.get().server, "controlleroutput");
191+
assertContent("/endpoint", ports.get().server, null);
192+
assertContent("/controller", ports.get().management, null);
174193
assertContent("/endpoint", ports.get().management, "endpointoutput");
175194
assertContent("/error", ports.get().management, startsWith("{"));
176195
ApplicationContext managementContext = this.applicationContext
@@ -191,7 +210,7 @@ public void onDifferentPortWithSpecificServer() throws Exception {
191210
assertContent("/controller", ports.get().server, "controlleroutput");
192211
assertContent("/endpoint", ports.get().server, null);
193212
assertContent("/controller", ports.get().management, null);
194-
assertContent("/endpoint", ports.get().management, "endpointoutput");
213+
assertContent("/application/endpoint", ports.get().management, "endpointoutput");
195214
assertContent("/error", ports.get().management, startsWith("{"));
196215
ApplicationContext managementContext = this.applicationContext
197216
.getBean(ManagementContextResolver.class).getApplicationContext();
@@ -310,7 +329,7 @@ public void specificPortsViaProperties() throws Exception {
310329
assertContent("/controller", ports.get().server, "controlleroutput");
311330
assertContent("/endpoint", ports.get().server, null);
312331
assertContent("/controller", ports.get().management, null);
313-
assertContent("/endpoint", ports.get().management, "endpointoutput");
332+
assertContent("/application/endpoint", ports.get().management, "endpointoutput");
314333
}
315334

316335
@Test
@@ -508,7 +527,8 @@ public void managementSpecificSslUsingDifferentPort() throws Exception {
508527
assertContent("/controller", ports.get().server, "controlleroutput");
509528
assertContent("/endpoint", ports.get().server, null);
510529
assertHttpsContent("/controller", ports.get().management, null);
511-
assertHttpsContent("/endpoint", ports.get().management, "endpointoutput");
530+
assertHttpsContent("/application/endpoint", ports.get().management,
531+
"endpointoutput");
512532
assertHttpsContent("/error", ports.get().management, startsWith("{"));
513533
ApplicationContext managementContext = this.applicationContext
514534
.getBean(ManagementContextResolver.class).getApplicationContext();
@@ -537,6 +557,19 @@ public void managementSpecificSslUsingSamePortFails() throws Exception {
537557
this.applicationContext.refresh();
538558
}
539559

560+
@Test
561+
public void rootManagementContextPathUsingSamePortFails() throws Exception {
562+
TestPropertyValues.of("management.context-path=/")
563+
.applyTo(this.applicationContext);
564+
this.applicationContext.register(RootConfig.class, EndpointConfig.class,
565+
BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class,
566+
ErrorMvcAutoConfiguration.class);
567+
this.thrown.expect(IllegalStateException.class);
568+
this.thrown.expectMessage("A management context path of '/' requires the"
569+
+ " management server to be listening on a separate port");
570+
this.applicationContext.refresh();
571+
}
572+
540573
@Test
541574
public void samePortCanBeUsedWhenManagementSslIsExplicitlyDisabled()
542575
throws Exception {
@@ -562,7 +595,7 @@ public void managementServerCanDisableSslWhenUsingADifferentPort() throws Except
562595
assertHttpsContent("/controller", ports.get().server, "controlleroutput");
563596
assertHttpsContent("/endpoint", ports.get().server, null);
564597
assertContent("/controller", ports.get().management, null);
565-
assertContent("/endpoint", ports.get().management, "endpointoutput");
598+
assertContent("/application/endpoint", ports.get().management, "endpointoutput");
566599
assertContent("/error", ports.get().management, startsWith("{"));
567600
ApplicationContext managementContext = this.applicationContext
568601
.getBean(ManagementContextResolver.class).getApplicationContext();

0 commit comments

Comments
 (0)