diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index ec81ef731e..b09372e02e 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -143,6 +143,12 @@ public class EndpointWebMvcAutoConfiguration } } if (managementPort == ManagementServerPort.SAME) { + String contextPath = environment.getProperty("management.context-path"); + if ("".equals(contextPath) || "/".equals(contextPath)) { + throw new IllegalStateException("A management context path of '" + + contextPath + "' requires the management server to be " + + "listening on a separate port"); + } if (environment.getProperty("management.ssl.enabled", Boolean.class, false)) { throw new IllegalStateException( "Management-specific SSL cannot be configured as the management " diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java index 5e0146dae3..f0d56fd4e8 100755 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java @@ -121,8 +121,7 @@ public class EndpointWebMvcAutoConfigurationTests { Ports values = new Ports(); ports.set(values); TestPropertyValues - .of("management.context-path=", "management.security.enabled=false", - "server.servlet.context-path=", + .of("management.security.enabled=false", "server.servlet.context-path=", "server.port=" + ports.get().server) .applyTo(this.applicationContext); } @@ -140,9 +139,9 @@ public class EndpointWebMvcAutoConfigurationTests { BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); - assertContent("/endpoint", ports.get().server, "endpointoutput"); - assertThat(hasHeader("/endpoint", ports.get().server, "X-Application-Context")) - .isFalse(); + assertContent("/application/endpoint", ports.get().server, "endpointoutput"); + assertThat(hasHeader("/application/endpoint", ports.get().server, + "X-Application-Context")).isFalse(); assertThat(this.applicationContext.containsBean("applicationContextIdFilter")) .isFalse(); } @@ -171,6 +170,26 @@ public class EndpointWebMvcAutoConfigurationTests { assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); + assertContent("/application/endpoint", ports.get().management, "endpointoutput"); + assertContent("/error", ports.get().management, startsWith("{")); + ApplicationContext managementContext = this.applicationContext + .getBean(ManagementContextResolver.class).getApplicationContext(); + List interceptors = (List) ReflectionTestUtils.getField( + managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); + assertThat(interceptors).hasSize(2); + } + + @Test + public void onDifferentPortAndRootContext() throws Exception { + TestPropertyValues.of("management.port=" + ports.get().management, + "management.context-path=/").applyTo(this.applicationContext); + this.applicationContext.register(RootConfig.class, EndpointConfig.class, + DifferentPortConfig.class, BaseConfiguration.class, + EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); + this.applicationContext.refresh(); + assertContent("/controller", ports.get().server, "controlleroutput"); + assertContent("/endpoint", ports.get().server, null); + assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext @@ -191,7 +210,7 @@ public class EndpointWebMvcAutoConfigurationTests { assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); - assertContent("/endpoint", ports.get().management, "endpointoutput"); + assertContent("/application/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); @@ -310,7 +329,7 @@ public class EndpointWebMvcAutoConfigurationTests { assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); - assertContent("/endpoint", ports.get().management, "endpointoutput"); + assertContent("/application/endpoint", ports.get().management, "endpointoutput"); } @Test @@ -508,7 +527,8 @@ public class EndpointWebMvcAutoConfigurationTests { assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertHttpsContent("/controller", ports.get().management, null); - assertHttpsContent("/endpoint", ports.get().management, "endpointoutput"); + assertHttpsContent("/application/endpoint", ports.get().management, + "endpointoutput"); assertHttpsContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); @@ -537,6 +557,19 @@ public class EndpointWebMvcAutoConfigurationTests { this.applicationContext.refresh(); } + @Test + public void rootManagementContextPathUsingSamePortFails() throws Exception { + TestPropertyValues.of("management.context-path=/") + .applyTo(this.applicationContext); + this.applicationContext.register(RootConfig.class, EndpointConfig.class, + BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, + ErrorMvcAutoConfiguration.class); + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("A management context path of '/' requires the" + + " management server to be listening on a separate port"); + this.applicationContext.refresh(); + } + @Test public void samePortCanBeUsedWhenManagementSslIsExplicitlyDisabled() throws Exception { @@ -562,7 +595,7 @@ public class EndpointWebMvcAutoConfigurationTests { assertHttpsContent("/controller", ports.get().server, "controlleroutput"); assertHttpsContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); - assertContent("/endpoint", ports.get().management, "endpointoutput"); + assertContent("/application/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext();