Commit afba8fed authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 53285084
...@@ -36,7 +36,7 @@ public class WebEndpointProperties { ...@@ -36,7 +36,7 @@ public class WebEndpointProperties {
/** /**
* Base path for Web endpoints. Relative to server.servlet.context-path or * 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"; private String basePath = "/actuator";
......
...@@ -43,20 +43,16 @@ public class ManagementServerProperties implements SecurityPrerequisite { ...@@ -43,20 +43,16 @@ public class ManagementServerProperties implements SecurityPrerequisite {
*/ */
private Integer port; private Integer port;
@NestedConfigurationProperty
private Ssl ssl;
/** /**
* Network address that to which the management endpoints should bind to. Requires a * Network address that to which the management endpoints should bind to. Requires a
* custom management.server.port. * custom management.server.port.
*/ */
private InetAddress address; private InetAddress address;
/** private final Servlet servlet = new Servlet();
* Management endpoint context-path. For instance, '/actuator'. Requires a custom
* management.server.port. @NestedConfigurationProperty
*/ private Ssl ssl;
private String contextPath = "";
/** /**
* Add the "X-Application-Context" HTTP header in each response. * Add the "X-Application-Context" HTTP header in each response.
...@@ -82,14 +78,6 @@ public class ManagementServerProperties implements SecurityPrerequisite { ...@@ -82,14 +78,6 @@ public class ManagementServerProperties implements SecurityPrerequisite {
this.port = port; this.port = port;
} }
public Ssl getSsl() {
return this.ssl;
}
public void setSsl(Ssl ssl) {
this.ssl = ssl;
}
public InetAddress getAddress() { public InetAddress getAddress() {
return this.address; return this.address;
} }
...@@ -98,25 +86,16 @@ public class ManagementServerProperties implements SecurityPrerequisite { ...@@ -98,25 +86,16 @@ public class ManagementServerProperties implements SecurityPrerequisite {
this.address = address; this.address = address;
} }
/** public Ssl getSsl() {
* Return the context path with no trailing slash (i.e. the '/' root context is return this.ssl;
* represented as the empty string).
* @return the context path (no trailing slash)
*/
public String getContextPath() {
return this.contextPath;
} }
public void setContextPath(String contextPath) { public void setSsl(Ssl ssl) {
Assert.notNull(contextPath, "ContextPath must not be null"); this.ssl = ssl;
this.contextPath = cleanContextPath(contextPath);
} }
private String cleanContextPath(String contextPath) { public Servlet getServlet() {
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { return this.servlet;
return contextPath.substring(0, contextPath.length() - 1);
}
return contextPath;
} }
public boolean getAddApplicationContextHeader() { public boolean getAddApplicationContextHeader() {
...@@ -127,4 +106,38 @@ public class ManagementServerProperties implements SecurityPrerequisite { ...@@ -127,4 +106,38 @@ public class ManagementServerProperties implements SecurityPrerequisite {
this.addApplicationContextHeader = addApplicationContextHeader; 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;
}
}
} }
...@@ -103,7 +103,8 @@ class ServletManagementChildContextConfiguration { ...@@ -103,7 +103,8 @@ class ServletManagementChildContextConfiguration {
ServerProperties serverProperties) { ServerProperties serverProperties) {
super.customize(webServerFactory, managementServerProperties, super.customize(webServerFactory, managementServerProperties,
serverProperties); serverProperties);
webServerFactory.setContextPath(managementServerProperties.getContextPath()); webServerFactory.setContextPath(
managementServerProperties.getServlet().getContextPath());
} }
} }
......
...@@ -1112,7 +1112,7 @@ ...@@ -1112,7 +1112,7 @@
"description": "Management endpoint context-path.", "description": "Management endpoint context-path.",
"defaultValue": "", "defaultValue": "",
"deprecation": { "deprecation": {
"replacement": "management.server.context-path", "replacement": "management.server.servlet.context-path",
"level": "error" "level": "error"
} }
}, },
......
...@@ -46,7 +46,7 @@ public class ManagementServerPropertiesTests { ...@@ -46,7 +46,7 @@ public class ManagementServerPropertiesTests {
public void defaultManagementServerProperties() { public void defaultManagementServerProperties() {
ManagementServerProperties properties = new ManagementServerProperties(); ManagementServerProperties properties = new ManagementServerProperties();
assertThat(properties.getPort()).isNull(); assertThat(properties.getPort()).isNull();
assertThat(properties.getContextPath()).isEqualTo(""); assertThat(properties.getServlet().getContextPath()).isEqualTo("");
assertThat(properties.getAddApplicationContextHeader()).isEqualTo(false); assertThat(properties.getAddApplicationContextHeader()).isEqualTo(false);
} }
...@@ -54,23 +54,23 @@ public class ManagementServerPropertiesTests { ...@@ -54,23 +54,23 @@ public class ManagementServerPropertiesTests {
public void definedManagementServerProperties() { public void definedManagementServerProperties() {
ManagementServerProperties properties = new ManagementServerProperties(); ManagementServerProperties properties = new ManagementServerProperties();
properties.setPort(123); properties.setPort(123);
properties.setContextPath("/foo"); properties.getServlet().setContextPath("/foo");
assertThat(properties.getPort()).isEqualTo(123); assertThat(properties.getPort()).isEqualTo(123);
assertThat(properties.getContextPath()).isEqualTo("/foo"); assertThat(properties.getServlet().getContextPath()).isEqualTo("/foo");
} }
@Test @Test
public void trailingSlashOfContextPathIsRemoved() { public void trailingSlashOfContextPathIsRemoved() {
ManagementServerProperties properties = new ManagementServerProperties(); ManagementServerProperties properties = new ManagementServerProperties();
properties.setContextPath("/foo/"); properties.getServlet().setContextPath("/foo/");
assertThat(properties.getContextPath()).isEqualTo("/foo"); assertThat(properties.getServlet().getContextPath()).isEqualTo("/foo");
} }
@Test @Test
public void slashOfContextPathIsDefaultValue() { public void slashOfContextPathIsDefaultValue() {
ManagementServerProperties properties = new ManagementServerProperties(); ManagementServerProperties properties = new ManagementServerProperties();
properties.setContextPath("/"); properties.getServlet().setContextPath("/");
assertThat(properties.getContextPath()).isEqualTo(""); assertThat(properties.getServlet().getContextPath()).isEqualTo("");
} }
public ManagementServerProperties load(String... environment) { public ManagementServerProperties load(String... environment) {
......
...@@ -1080,8 +1080,8 @@ content into your application. Rather, pick only the properties that you need. ...@@ -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 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.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.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.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.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.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. 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. ...@@ -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.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.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.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. 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]) # ENDPOINTS CORS CONFIGURATION ({sc-spring-boot-actuator-autoconfigure}/endpoint/web/servlet/CorsEndpointProperties.{sc-ext}[CorsEndpointProperties])
......
...@@ -691,7 +691,8 @@ NOTE: Unless the management port has been configured to ...@@ -691,7 +691,8 @@ NOTE: Unless the management port has been configured to
<<production-ready-customizing-management-server-port,expose endpoints using a different <<production-ready-customizing-management-server-port,expose endpoints using a different
HTTP port>>, `management.endpoints.web.base-path` is relative to HTTP port>>, `management.endpoints.web.base-path` is relative to
`server.servlet.context-path`. If `management.server.port` is configured, `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`.
......
...@@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { @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 { public class ManagementPortAndPathSampleActuatorApplicationTests {
@LocalServerPort @LocalServerPort
......
...@@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.server.port=0", "management.server.address=127.0.0.1", "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 { public class ManagementAddressActuatorApplicationTests {
@LocalServerPort @LocalServerPort
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment