Skip to content

Commit 1614e1d

Browse files
committed
Copy customization of main (parent) server in management context
If the actuator endpoints are configured on a different port then there are some settings in the main ServerProperties that we would like to re-use (e.g. the access log). The easiest way to do that is to just configure the management server using the same ServerProperties instance and then overwrite the things that are different (and stored in ManagementServerProperties). Fixes gh-1581
1 parent 5d5db8a commit 1614e1d

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure;
1818

19+
import java.util.Collections;
1920
import java.util.HashSet;
2021
import java.util.Set;
2122

@@ -36,6 +37,7 @@
3637
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
3738
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
3839
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
40+
import org.springframework.boot.autoconfigure.web.ServerProperties;
3941
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
4042
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
4143
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
@@ -75,13 +77,23 @@ protected static class ServerCustomization implements
7577
// instances get their callback very early in the context lifecycle.
7678
private ManagementServerProperties managementServerProperties;
7779

80+
private ServerProperties server;
81+
7882
@Override
7983
public void customize(ConfigurableEmbeddedServletContainer container) {
8084
if (this.managementServerProperties == null) {
8185
this.managementServerProperties = BeanFactoryUtils
8286
.beanOfTypeIncludingAncestors(this.beanFactory,
8387
ManagementServerProperties.class);
88+
this.server = BeanFactoryUtils
89+
.beanOfTypeIncludingAncestors(this.beanFactory,
90+
ServerProperties.class);
8491
}
92+
// Customize as per the parent context first (so e.g. the access logs go to the same place)
93+
server.customize(container);
94+
// Then reset the error pages
95+
container.setErrorPages(Collections.<ErrorPage>emptySet());
96+
// and add the management-specific bits
8597
container.setPort(this.managementServerProperties.getPort());
8698
container.setAddress(this.managementServerProperties.getAddress());
8799
container.setContextPath(this.managementServerProperties.getContextPath());

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
3636
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3737
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
38+
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
3839
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
3940
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
4041
import org.springframework.boot.test.EnvironmentTestUtils;
@@ -222,6 +223,7 @@ public void portPropertiesOnDifferentPort() throws Exception {
222223
assertThat(localServerPort, notNullValue());
223224
assertThat(localManagementPort, notNullValue());
224225
assertThat(localServerPort, not(equalTo(localManagementPort)));
226+
assertThat(applicationContext.getBean(ServerPortConfig.class).getCount(), equalTo(2));
225227
this.applicationContext.close();
226228
assertAllClosed();
227229
}
@@ -303,10 +305,22 @@ public TestEndpoint testEndpoint() {
303305

304306
@Configuration
305307
public static class ServerPortConfig {
308+
309+
private int count = 0;
310+
311+
public int getCount() {
312+
return count;
313+
}
306314

307315
@Bean
308316
public ServerProperties serverProperties() {
309-
ServerProperties properties = new ServerProperties();
317+
ServerProperties properties = new ServerProperties() {
318+
@Override
319+
public void customize(ConfigurableEmbeddedServletContainer container) {
320+
count++;
321+
super.customize(container);
322+
}
323+
};
310324
properties.setPort(ports.get().server);
311325
return properties;
312326
}

spring-boot-samples/spring-boot-sample-actuator/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
logging.file: /tmp/logs/app.log
22
logging.level.org.springframework.security: INFO
33
management.address: 127.0.0.1
4+
#management.port: 8181
45
endpoints.shutdown.enabled: true
56
server.tomcat.basedir: target/tomcat
67
server.tomcat.access_log_enabled: true

0 commit comments

Comments
 (0)