Skip to content

Commit d307eba

Browse files
committed
Add property to configure base-path for web endpoints.
Also, move properties corresponding to management server under `management.server.*`. Closes gh-10230
1 parent 68db43c commit d307eba

File tree

29 files changed

+184
-96
lines changed

29 files changed

+184
-96
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.stream.Collectors;
2222

2323
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointProvider;
24-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2524
import org.springframework.boot.actuate.endpoint.EndpointInfo;
2625
import org.springframework.boot.actuate.endpoint.web.WebEndpointOperation;
2726
import org.springframework.util.Assert;
@@ -36,12 +35,12 @@ public class DefaultEndpointPathProvider implements EndpointPathProvider {
3635

3736
private final Collection<EndpointInfo<WebEndpointOperation>> endpoints;
3837

39-
private final String contextPath;
38+
private final String basePath;
4039

4140
public DefaultEndpointPathProvider(EndpointProvider<WebEndpointOperation> provider,
42-
ManagementServerProperties managementServerProperties) {
41+
WebEndpointProperties webEndpointProperties) {
4342
this.endpoints = provider.getEndpoints();
44-
this.contextPath = managementServerProperties.getContextPath();
43+
this.basePath = webEndpointProperties.getBasePath();
4544
}
4645

4746
@Override
@@ -57,7 +56,7 @@ public String getPath(String id) {
5756
}
5857

5958
private String getPath(EndpointInfo<WebEndpointOperation> endpointInfo) {
60-
return this.contextPath + "/" + endpointInfo.getId();
59+
return this.basePath + "/" + endpointInfo.getId();
6160
}
6261

6362
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.endpoint.web;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for web management endpoints.
23+
*
24+
* @author Madhura Bhave
25+
* @since 2.0.0
26+
*/
27+
@ConfigurationProperties(prefix = "management.endpoints.web")
28+
public class WebEndpointProperties {
29+
30+
/**
31+
* The base-path for the web endpoints. Relative to `server.context-path` or
32+
* `management.server.context-path`, if `management.server.port` is different.
33+
*/
34+
private String basePath = "/application";
35+
36+
public String getBasePath() {
37+
return this.basePath;
38+
}
39+
40+
public void setBasePath(String basePath) {
41+
this.basePath = basePath;
42+
}
43+
44+
}
45+
46+

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointProvider;
2424
import org.springframework.boot.actuate.autoconfigure.endpoint.web.DefaultEndpointPathProvider;
2525
import org.springframework.boot.actuate.autoconfigure.endpoint.web.EndpointPathProvider;
26+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2627
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
27-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2828
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
2929
import org.springframework.boot.actuate.endpoint.web.WebEndpointOperation;
3030
import org.springframework.boot.actuate.endpoint.web.jersey.JerseyEndpointResourceFactory;
@@ -55,19 +55,19 @@ class JerseyWebEndpointManagementContextConfiguration {
5555
@Bean
5656
public ResourceConfigCustomizer webEndpointRegistrar(
5757
EndpointProvider<WebEndpointOperation> provider,
58-
ManagementServerProperties managementServerProperties) {
58+
WebEndpointProperties webEndpointProperties) {
5959
return (resourceConfig) -> resourceConfig.registerResources(
6060
new HashSet<>(new JerseyEndpointResourceFactory().createEndpointResources(
61-
new EndpointMapping(managementServerProperties.getContextPath()),
61+
new EndpointMapping(webEndpointProperties.getBasePath()),
6262
provider.getEndpoints())));
6363
}
6464

6565
@Bean
6666
@ConditionalOnMissingBean
6767
public EndpointPathProvider endpointPathProvider(
6868
EndpointProvider<WebEndpointOperation> provider,
69-
ManagementServerProperties managementServerProperties) {
70-
return new DefaultEndpointPathProvider(provider, managementServerProperties);
69+
WebEndpointProperties webEndpointProperties) {
70+
return new DefaultEndpointPathProvider(provider, webEndpointProperties);
7171
}
7272

7373
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointProvider;
2020
import org.springframework.boot.actuate.autoconfigure.endpoint.web.DefaultEndpointPathProvider;
2121
import org.springframework.boot.actuate.autoconfigure.endpoint.web.EndpointPathProvider;
22+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2223
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
23-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2424
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
2525
import org.springframework.boot.actuate.endpoint.web.WebEndpointOperation;
2626
import org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping;
@@ -45,18 +45,18 @@ public class WebFluxEndpointManagementContextConfiguration {
4545
@ConditionalOnMissingBean
4646
public WebFluxEndpointHandlerMapping webEndpointReactiveHandlerMapping(
4747
EndpointProvider<WebEndpointOperation> provider,
48-
ManagementServerProperties managementServerProperties) {
48+
WebEndpointProperties webEndpointProperties) {
4949
return new WebFluxEndpointHandlerMapping(
50-
new EndpointMapping(managementServerProperties.getContextPath()),
50+
new EndpointMapping(webEndpointProperties.getBasePath()),
5151
provider.getEndpoints());
5252
}
5353

5454
@Bean
5555
@ConditionalOnMissingBean
5656
public EndpointPathProvider endpointPathProvider(
5757
EndpointProvider<WebEndpointOperation> provider,
58-
ManagementServerProperties managementServerProperties) {
59-
return new DefaultEndpointPathProvider(provider, managementServerProperties);
58+
WebEndpointProperties webEndpointProperties) {
59+
return new DefaultEndpointPathProvider(provider, webEndpointProperties);
6060
}
6161

6262
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointProvider;
2020
import org.springframework.boot.actuate.autoconfigure.endpoint.web.DefaultEndpointPathProvider;
2121
import org.springframework.boot.actuate.autoconfigure.endpoint.web.EndpointPathProvider;
22+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2223
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
2324
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2425
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@@ -48,17 +49,17 @@
4849
@ConditionalOnClass(DispatcherServlet.class)
4950
@ConditionalOnBean(DispatcherServlet.class)
5051
@EnableConfigurationProperties({ CorsEndpointProperties.class,
51-
ManagementServerProperties.class })
52+
WebEndpointProperties.class, ManagementServerProperties.class })
5253
public class WebMvcEndpointManagementContextConfiguration {
5354

5455
@Bean
5556
@ConditionalOnMissingBean
5657
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
5758
EndpointProvider<WebEndpointOperation> provider,
5859
CorsEndpointProperties corsProperties,
59-
ManagementServerProperties managementServerProperties) {
60+
WebEndpointProperties webEndpointProperties) {
6061
WebMvcEndpointHandlerMapping handlerMapping = new WebMvcEndpointHandlerMapping(
61-
new EndpointMapping(managementServerProperties.getContextPath()),
62+
new EndpointMapping(webEndpointProperties.getBasePath()),
6263
provider.getEndpoints(), getCorsConfiguration(corsProperties));
6364
return handlerMapping;
6465
}
@@ -67,8 +68,8 @@ public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
6768
@ConditionalOnMissingBean
6869
public EndpointPathProvider endpointPathProvider(
6970
EndpointProvider<WebEndpointOperation> provider,
70-
ManagementServerProperties managementServerProperties) {
71-
return new DefaultEndpointPathProvider(provider, managementServerProperties);
71+
WebEndpointProperties webEndpointProperties) {
72+
return new DefaultEndpointPathProvider(provider, webEndpointProperties);
7273
}
7374

7475
private CorsConfiguration getCorsConfiguration(CorsEndpointProperties properties) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/jolokia/JolokiaManagementContextConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public JolokiaManagementContextConfiguration(
6969

7070
@Bean
7171
public ServletRegistrationBean<AgentServlet> jolokiaServlet() {
72-
String path = this.managementServletContext.getContextPath()
72+
String path = this.managementServletContext.getServletPath()
7373
+ this.properties.getPath();
7474
String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
7575
ServletRegistrationBean<AgentServlet> registration = new ServletRegistrationBean<>(

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.actuate.autoconfigure.web.server;
1818

1919
import org.springframework.beans.factory.SmartInitializingSingleton;
20+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2021
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
2122
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
2223
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
@@ -42,8 +43,8 @@
4243

4344
/**
4445
* {@link EnableAutoConfiguration Auto-configuration} for the management context. If the
45-
* {@code management.port} is the same as the {@code server.port} the management context
46-
* will be the same as the main application context. If the {@code management.port} is
46+
* {@code management.server.port} is the same as the {@code server.port} the management context
47+
* will be the same as the main application context. If the {@code management.server.port} is
4748
* different to the {@code server.port} the management context will be a separate context
4849
* that has the main application context as its parent.
4950
*
@@ -52,7 +53,7 @@
5253
*/
5354
@Configuration
5455
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
55-
@EnableConfigurationProperties(ManagementServerProperties.class)
56+
@EnableConfigurationProperties({ WebEndpointProperties.class, ManagementServerProperties.class })
5657
public class ManagementContextAutoConfiguration {
5758

5859
@Configuration
@@ -76,7 +77,7 @@ public void afterSingletonsInstantiated() {
7677
}
7778

7879
private void verifySslConfiguration() {
79-
Boolean enabled = this.environment.getProperty("management.ssl.enabled",
80+
Boolean enabled = this.environment.getProperty("management.server.ssl.enabled",
8081
Boolean.class, false);
8182
Assert.state(!enabled,
8283
"Management-specific SSL cannot be configured as the management "

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementPortType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum ManagementPortType {
4343

4444
static ManagementPortType get(Environment environment) {
4545
Integer serverPort = getPortProperty(environment, "server.");
46-
Integer managementPort = getPortProperty(environment, "management.");
46+
Integer managementPort = getPortProperty(environment, "management.server.");
4747
if (managementPort != null && managementPort < 0) {
4848
return DISABLED;
4949
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* @since 2.0.0
3636
* @see ServerProperties
3737
*/
38-
@ConfigurationProperties(prefix = "management", ignoreUnknownFields = true)
38+
@ConfigurationProperties(prefix = "management.server", ignoreUnknownFields = true)
3939
public class ManagementServerProperties implements SecurityPrerequisite {
4040

4141
/**
@@ -54,7 +54,7 @@ public class ManagementServerProperties implements SecurityPrerequisite {
5454
/**
5555
* Management endpoint context-path.
5656
*/
57-
private String contextPath = "/application";
57+
private String contextPath = "";
5858

5959
/**
6060
* Add the "X-Application-Context" HTTP header in each response.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
* Provides information about the management servlet context for MVC controllers to use.
2121
*
2222
* @author Phillip Webb
23+
* @author Madhura Bhave
2324
* @since 2.0.0
2425
*/
2526
@FunctionalInterface
2627
public interface ManagementServletContext {
2728

2829
/**
29-
* Return the context path of the management server.
30-
* @return the context path
30+
* Return the servlet path of the management server.
31+
* @return the servlet path
3132
*/
32-
String getContextPath();
33+
String getServletPath();
3334

3435
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected void customize(ConfigurableServletWebServerFactory webServerFactory,
102102
ServerProperties serverProperties) {
103103
super.customize(webServerFactory, managementServerProperties,
104104
serverProperties);
105-
webServerFactory.setContextPath("");
105+
webServerFactory.setContextPath(managementServerProperties.getContextPath());
106106
}
107107

108108
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import javax.servlet.Servlet;
2020

21-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
21+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2222
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -48,8 +48,8 @@ public ServletManagementContextFactory servletWebChildContextFactory() {
4848

4949
@Bean
5050
public ManagementServletContext managementServletContext(
51-
ManagementServerProperties properties) {
52-
return () -> properties.getContextPath();
51+
WebEndpointProperties properties) {
52+
return () -> properties.getBasePath();
5353
}
5454

5555
// Put Servlets and Filters in their own nested class so they don't force early

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/DefaultEndpointPathProviderTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.mockito.MockitoAnnotations;
2727

2828
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointProvider;
29-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
3029
import org.springframework.boot.actuate.endpoint.DefaultEnablement;
3130
import org.springframework.boot.actuate.endpoint.EndpointInfo;
3231
import org.springframework.boot.actuate.endpoint.web.WebEndpointOperation;
@@ -88,10 +87,10 @@ private DefaultEndpointPathProvider createProvider(String contextPath) {
8887
endpoints.add(new EndpointInfo<>("bar", DefaultEnablement.ENABLED,
8988
Collections.emptyList()));
9089
given(this.endpointProvider.getEndpoints()).willReturn(endpoints);
91-
ManagementServerProperties managementServerProperties = new ManagementServerProperties();
92-
managementServerProperties.setContextPath(contextPath);
90+
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
91+
webEndpointProperties.setBasePath(contextPath);
9392
return new DefaultEndpointPathProvider(this.endpointProvider,
94-
managementServerProperties);
93+
webEndpointProperties);
9594
}
9695

9796
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.endpoint.web;
18+
19+
import org.junit.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link WebEndpointProperties}.
25+
*
26+
* @author Madhura Bhave
27+
*/
28+
public class WebEndpointPropertiesTests {
29+
30+
@Test
31+
public void defaultBasePathShouldBeApplication() throws Exception {
32+
WebEndpointProperties properties = new WebEndpointProperties();
33+
assertThat(properties.getBasePath()).isEqualTo("/application");
34+
}
35+
36+
}
37+

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/WebMvcEndpointIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,24 @@ public void endpointsAreSecureByDefault() throws Exception {
7474
}
7575

7676
@Test
77-
public void endpointsAreSecureByDefaultWithCustomContextPath() throws Exception {
77+
public void endpointsAreSecureByDefaultWithCustomBasePath() throws Exception {
7878
this.context = new AnnotationConfigWebApplicationContext();
7979
this.context.register(SecureConfiguration.class);
80-
TestPropertyValues.of("management.context-path:/management")
80+
TestPropertyValues.of("management.endpoints.web.base-path:/management")
8181
.applyTo(this.context);
8282
MockMvc mockMvc = createSecureMockMvc();
8383
mockMvc.perform(get("/management/beans").accept(MediaType.APPLICATION_JSON))
8484
.andExpect(status().isUnauthorized());
8585
}
8686

8787
@Test
88-
public void endpointsAreSecureWithActuatorRoleWithCustomContextPath()
88+
public void endpointsAreSecureWithActuatorRoleWithCustomBasePath()
8989
throws Exception {
9090
TestSecurityContextHolder.getContext().setAuthentication(
9191
new TestingAuthenticationToken("user", "N/A", "ROLE_ACTUATOR"));
9292
this.context = new AnnotationConfigWebApplicationContext();
9393
this.context.register(SecureConfiguration.class);
94-
TestPropertyValues.of("management.context-path:/management",
94+
TestPropertyValues.of("management.endpoints.web.base-path:/management",
9595
"endpoints.default.web.enabled=true").applyTo(this.context);
9696
MockMvc mockMvc = createSecureMockMvc();
9797
mockMvc.perform(get("/management/beans")).andExpect(status().isOk());

0 commit comments

Comments
 (0)