Skip to content

Commit 3f1f830

Browse files
committed
Fail if management.server.address is set but actuator is on the same port
Closes gh-22187
1 parent a0afb73 commit 3f1f830

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,6 +73,7 @@ static class SameManagementContextConfiguration implements SmartInitializingSing
7373
@Override
7474
public void afterSingletonsInstantiated() {
7575
verifySslConfiguration();
76+
verifyAddressConfiguration();
7677
if (this.environment instanceof ConfigurableEnvironment) {
7778
addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.environment);
7879
}
@@ -84,6 +85,12 @@ private void verifySslConfiguration() {
8485
+ "server is not listening on a separate port");
8586
}
8687

88+
private void verifyAddressConfiguration() {
89+
Object address = this.environment.getProperty("management.server.address");
90+
Assert.state(address == null, "Management-specific server address cannot be configured as the management "
91+
+ "server is not listening on a separate port");
92+
}
93+
8794
/**
8895
* Add an alias for 'local.management.port' that actually resolves using
8996
* 'local.server.port'.

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
2525
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.AutoConfigurations;
27+
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
2728
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
2829
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2930
import org.springframework.boot.test.system.CapturedOutput;
@@ -54,6 +55,19 @@ void childManagementContextShouldStartForEmbeddedServer(CapturedOutput output) {
5455
.run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2)));
5556
}
5657

58+
@Test
59+
void givenSamePortManagementServerWhenManagementServerAddressIsConfiguredThenContextRefreshFails() {
60+
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
61+
AnnotationConfigServletWebServerApplicationContext::new)
62+
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
63+
ServletWebServerFactoryAutoConfiguration.class,
64+
ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
65+
EndpointAutoConfiguration.class, DispatcherServletAutoConfiguration.class));
66+
contextRunner.withPropertyValues("server.port=0", "management.server.address=127.0.0.1")
67+
.run((context) -> assertThat(context).getFailure()
68+
.hasMessageStartingWith("Management-specific server address cannot be configured"));
69+
}
70+
5771
private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) {
5872
return (charSequence) -> {
5973
int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);

0 commit comments

Comments
 (0)