Skip to content

Commit d854c09

Browse files
author
Phillip Webb
committed
Add option to disable X-Application-Context
Add `management.add-application-context-header` option to disable the automatic adding of the `X-Application-Context` HTTP header. Fixes gh-1308
1 parent 621649d commit d854c09

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,40 @@ protected static class ApplicationContextFilterConfiguration {
246246

247247
@Bean
248248
public Filter applicationContextIdFilter(ApplicationContext context) {
249-
final String id = context.getId();
250-
return new OncePerRequestFilter() {
251-
252-
@Override
253-
protected void doFilterInternal(HttpServletRequest request,
254-
HttpServletResponse response, FilterChain filterChain)
255-
throws ServletException, IOException {
256-
response.addHeader("X-Application-Context", id);
257-
filterChain.doFilter(request, response);
258-
}
259-
};
249+
return new ApplicationContextHeaderFilter(context);
260250
}
251+
252+
}
253+
254+
/**
255+
* {@link OncePerRequestFilter} to add the {@literal X-Application-Context} if
256+
* required.
257+
*/
258+
private static class ApplicationContextHeaderFilter extends OncePerRequestFilter {
259+
260+
private final ApplicationContext applicationContext;
261+
262+
private ManagementServerProperties properties;
263+
264+
public ApplicationContextHeaderFilter(ApplicationContext applicationContext) {
265+
this.applicationContext = applicationContext;
266+
}
267+
268+
@Override
269+
protected void doFilterInternal(HttpServletRequest request,
270+
HttpServletResponse response, FilterChain filterChain)
271+
throws ServletException, IOException {
272+
if (this.properties == null) {
273+
this.properties = this.applicationContext
274+
.getBean(ManagementServerProperties.class);
275+
}
276+
if (this.properties.getAddApplicationContextHeader()) {
277+
response.addHeader("X-Application-Context",
278+
this.applicationContext.getId());
279+
}
280+
filterChain.doFilter(request, response);
281+
}
282+
261283
}
262284

263285
protected static enum ManagementServerPort {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class ManagementServerProperties implements SecurityPrequisite {
6060
@NotNull
6161
private String contextPath = "";
6262

63+
private boolean addApplicationContextHeader = true;
64+
6365
private final Security security = maybeCreateSecurity();
6466

6567
/**
@@ -99,6 +101,14 @@ public Security getSecurity() {
99101
return this.security;
100102
}
101103

104+
public boolean getAddApplicationContextHeader() {
105+
return this.addApplicationContextHeader;
106+
}
107+
108+
public void setAddApplicationContextHeader(boolean addApplicationContextHeader) {
109+
this.addApplicationContextHeader = addApplicationContextHeader;
110+
}
111+
102112
/**
103113
* Security configuration.
104114
*/

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
import static org.hamcrest.Matchers.equalTo;
5858
import static org.hamcrest.Matchers.not;
5959
import static org.hamcrest.Matchers.notNullValue;
60+
import static org.junit.Assert.assertFalse;
6061
import static org.junit.Assert.assertThat;
62+
import static org.junit.Assert.assertTrue;
6163

6264
/**
6365
* Tests for {@link EndpointWebMvcAutoConfiguration}.
@@ -92,6 +94,19 @@ public void onSamePort() throws Exception {
9294
assertContent("/endpoint", ports.get().server, "endpointoutput");
9395
assertContent("/controller", ports.get().management, null);
9496
assertContent("/endpoint", ports.get().management, null);
97+
assertTrue(hasHeader("/endpoint", ports.get().server, "X-Application-Context"));
98+
this.applicationContext.close();
99+
assertAllClosed();
100+
}
101+
102+
@Test
103+
public void onSamePortWithoutHeader() throws Exception {
104+
EnvironmentTestUtils.addEnvironment(this.applicationContext,
105+
"management.add-application-context-header:false");
106+
this.applicationContext.register(RootConfig.class, BaseConfiguration.class,
107+
ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class);
108+
this.applicationContext.refresh();
109+
assertFalse(hasHeader("/endpoint", ports.get().server, "X-Application-Context"));
95110
this.applicationContext.close();
96111
assertAllClosed();
97112
}
@@ -244,6 +259,14 @@ public void assertContent(String url, int port, Object expected) throws Exceptio
244259
}
245260
}
246261

262+
public boolean hasHeader(String url, int port, String header) throws Exception {
263+
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
264+
ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI(
265+
"http://localhost:" + port + url), HttpMethod.GET);
266+
ClientHttpResponse response = request.execute();
267+
return response.getHeaders().containsKey(header);
268+
}
269+
247270
private static class Ports {
248271

249272
int server = SocketUtils.findAvailableTcpPort();

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ content into your application; rather pick only the properties that you need.
313313
management.port= # defaults to 'server.port'
314314
management.address= # bind to a specific NIC
315315
management.contextPath= # default to '/'
316+
management.add-application-context-header= # default to true
316317
317318
# ENDPOINTS ({sc-spring-boot-actuator}/endpoint/AbstractEndpoint.{sc-ext}[AbstractEndpoint] subclasses)
318319
endpoints.autoconfig.id=autoconfig

0 commit comments

Comments
 (0)