From afba8fed79068c58a7611d3a603258783b74361e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 Dec 2017 12:08:51 +0100 Subject: [PATCH] Harmonize management.server.context-path property This commit moves management.server.context-path to management.server.servlet.context-path to align with the configuration key for the application's main context path. Closes gh-11359 --- .../endpoint/web/WebEndpointProperties.java | 2 +- .../server/ManagementServerProperties.java | 75 +++++++++++-------- ...etManagementChildContextConfiguration.java | 3 +- ...itional-spring-configuration-metadata.json | 2 +- .../ManagementServerPropertiesTests.java | 14 ++-- .../appendix-application-properties.adoc | 4 +- .../asciidoc/production-ready-features.adoc | 3 +- ...AndPathSampleActuatorApplicationTests.java | 2 +- ...gementAddressActuatorApplicationTests.java | 2 +- 9 files changed, 61 insertions(+), 46 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java index bb1cd0a9e1..0ace13355c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java @@ -36,7 +36,7 @@ public class WebEndpointProperties { /** * Base path for Web endpoints. Relative to server.servlet.context-path or - * management.server.context-path if management.server.port is configured. + * management.server.servlet.context-path if management.server.port is configured. */ private String basePath = "/actuator"; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java index 6fc6a7a2f1..d487c06f79 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java @@ -43,20 +43,16 @@ public class ManagementServerProperties implements SecurityPrerequisite { */ private Integer port; - @NestedConfigurationProperty - private Ssl ssl; - /** * Network address that to which the management endpoints should bind to. Requires a * custom management.server.port. */ private InetAddress address; - /** - * Management endpoint context-path. For instance, '/actuator'. Requires a custom - * management.server.port. - */ - private String contextPath = ""; + private final Servlet servlet = new Servlet(); + + @NestedConfigurationProperty + private Ssl ssl; /** * Add the "X-Application-Context" HTTP header in each response. @@ -82,14 +78,6 @@ public class ManagementServerProperties implements SecurityPrerequisite { this.port = port; } - public Ssl getSsl() { - return this.ssl; - } - - public void setSsl(Ssl ssl) { - this.ssl = ssl; - } - public InetAddress getAddress() { return this.address; } @@ -98,25 +86,16 @@ public class ManagementServerProperties implements SecurityPrerequisite { this.address = address; } - /** - * Return the context path with no trailing slash (i.e. the '/' root context is - * represented as the empty string). - * @return the context path (no trailing slash) - */ - public String getContextPath() { - return this.contextPath; + public Ssl getSsl() { + return this.ssl; } - public void setContextPath(String contextPath) { - Assert.notNull(contextPath, "ContextPath must not be null"); - this.contextPath = cleanContextPath(contextPath); + public void setSsl(Ssl ssl) { + this.ssl = ssl; } - private String cleanContextPath(String contextPath) { - if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { - return contextPath.substring(0, contextPath.length() - 1); - } - return contextPath; + public Servlet getServlet() { + return this.servlet; } public boolean getAddApplicationContextHeader() { @@ -127,4 +106,38 @@ public class ManagementServerProperties implements SecurityPrerequisite { this.addApplicationContextHeader = addApplicationContextHeader; } + /** + * Servlet properties. + */ + public class Servlet { + + /** + * Management endpoint context-path. For instance, '/management'. Requires a + * custom management.server.port. + */ + private String contextPath = ""; + + /** + * Return the context path with no trailing slash (i.e. the '/' root context is + * represented as the empty string). + * @return the context path (no trailing slash) + */ + public String getContextPath() { + return this.contextPath; + } + + public void setContextPath(String contextPath) { + Assert.notNull(contextPath, "ContextPath must not be null"); + this.contextPath = cleanContextPath(contextPath); + } + + private String cleanContextPath(String contextPath) { + if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { + return contextPath.substring(0, contextPath.length() - 1); + } + return contextPath; + } + + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java index ddf32267f2..e6c1d9b79f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java @@ -103,7 +103,8 @@ class ServletManagementChildContextConfiguration { ServerProperties serverProperties) { super.customize(webServerFactory, managementServerProperties, serverProperties); - webServerFactory.setContextPath(managementServerProperties.getContextPath()); + webServerFactory.setContextPath( + managementServerProperties.getServlet().getContextPath()); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index b09b1a0bf6..ffbdb11b74 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1112,7 +1112,7 @@ "description": "Management endpoint context-path.", "defaultValue": "", "deprecation": { - "replacement": "management.server.context-path", + "replacement": "management.server.servlet.context-path", "level": "error" } }, diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java index 480ef736f9..e6cb872166 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java @@ -46,7 +46,7 @@ public class ManagementServerPropertiesTests { public void defaultManagementServerProperties() { ManagementServerProperties properties = new ManagementServerProperties(); assertThat(properties.getPort()).isNull(); - assertThat(properties.getContextPath()).isEqualTo(""); + assertThat(properties.getServlet().getContextPath()).isEqualTo(""); assertThat(properties.getAddApplicationContextHeader()).isEqualTo(false); } @@ -54,23 +54,23 @@ public class ManagementServerPropertiesTests { public void definedManagementServerProperties() { ManagementServerProperties properties = new ManagementServerProperties(); properties.setPort(123); - properties.setContextPath("/foo"); + properties.getServlet().setContextPath("/foo"); assertThat(properties.getPort()).isEqualTo(123); - assertThat(properties.getContextPath()).isEqualTo("/foo"); + assertThat(properties.getServlet().getContextPath()).isEqualTo("/foo"); } @Test public void trailingSlashOfContextPathIsRemoved() { ManagementServerProperties properties = new ManagementServerProperties(); - properties.setContextPath("/foo/"); - assertThat(properties.getContextPath()).isEqualTo("/foo"); + properties.getServlet().setContextPath("/foo/"); + assertThat(properties.getServlet().getContextPath()).isEqualTo("/foo"); } @Test public void slashOfContextPathIsDefaultValue() { ManagementServerProperties properties = new ManagementServerProperties(); - properties.setContextPath("/"); - assertThat(properties.getContextPath()).isEqualTo(""); + properties.getServlet().setContextPath("/"); + assertThat(properties.getServlet().getContextPath()).isEqualTo(""); } public ManagementServerProperties load(String... environment) { diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 7ee9aabfae..7dfe72d523 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1080,8 +1080,8 @@ content into your application. Rather, pick only the properties that you need. # MANAGEMENT HTTP SERVER ({sc-spring-boot-actuator-autoconfigure}/web/server/ManagementServerProperties.{sc-ext}[ManagementServerProperties]) management.server.add-application-context-header=false # Add the "X-Application-Context" HTTP header in each response. Requires a custom management.server.port. management.server.address= # Network address that to which the management endpoints should bind. Requires a custom management.server.port. - management.server.context-path= # Management endpoint context-path. For instance, `/actuator`. Requires a custom management.server.port management.server.port= # Management endpoint HTTP port. Uses the same port as the application by default. Configure a different port to use management-specific SSL. + management.server.servlet.context-path= # Management endpoint context-path. For instance, `/management`. Requires a custom management.server.port management.server.ssl.ciphers= # Supported SSL ciphers. Requires a custom management.port. management.server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store. Requires a custom management.server.port. management.server.ssl.enabled= # Whether to enable SSL support. Requires a custom management.server.port. @@ -1117,7 +1117,7 @@ content into your application. Rather, pick only the properties that you need. management.endpoints.web.enabled=true # Whether web endpoints are enabled management.endpoints.web.expose=info,health # Endpoint IDs that should be exposed or '*' for all. management.endpoints.web.exclude= # Endpoint IDs that should be excluded. - management.endpoints.web.base-path=/actuator # Base path for Web endpoints. Relative to server.servlet.context-path or management.server.context-path if management.server.port is configured. + management.endpoints.web.base-path=/actuator # Base path for Web endpoints. Relative to server.servlet.context-path or management.server.servlet.context-path if management.server.port is configured. management.endpoints.web.path-mapping= # Mapping between endpoint IDs and the path that should expose them. # ENDPOINTS CORS CONFIGURATION ({sc-spring-boot-actuator-autoconfigure}/endpoint/web/servlet/CorsEndpointProperties.{sc-ext}[CorsEndpointProperties]) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 8df4b73c71..32679adec9 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -691,7 +691,8 @@ NOTE: Unless the management port has been configured to <>, `management.endpoints.web.base-path` is relative to `server.servlet.context-path`. If `management.server.port` is configured, -`management.endpoints.web.base-path` is relative to `management.server.context-path`. +`management.endpoints.web.base-path` is relative to +`management.server.servlet.context-path`. diff --git a/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/ManagementPortAndPathSampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/ManagementPortAndPathSampleActuatorApplicationTests.java index 7005c84da2..de1c3e7637 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/ManagementPortAndPathSampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-custom-security/src/test/java/sample/actuator/customsecurity/ManagementPortAndPathSampleActuatorApplicationTests.java @@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { - "management.server.port=0", "management.server.context-path=/management" }) + "management.server.port=0", "management.server.servlet.context-path=/management" }) public class ManagementPortAndPathSampleActuatorApplicationTests { @LocalServerPort diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java index 14fb33a922..949b2f4fda 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/ManagementAddressActuatorApplicationTests.java @@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "management.server.port=0", "management.server.address=127.0.0.1", - "management.server.context-path:/admin" }) + "management.server.servlet.context-path:/admin" }) public class ManagementAddressActuatorApplicationTests { @LocalServerPort