Skip to content

Commit 539b009

Browse files
committed
Clean management context path if necessary
Various areas of the code expect the management's context path to not contain any trailing slash but nothing is enforcing it. We now make sure to remove any trailing slash, including the one for '/' and make that explicit via the Javadoc of the getter. Fixes gh-3553
1 parent 8fe9fc2 commit 539b009

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
import org.springframework.boot.context.properties.ConfigurationProperties;
2727
import org.springframework.security.config.http.SessionCreationPolicy;
2828
import org.springframework.util.ClassUtils;
29+
import org.springframework.util.StringUtils;
2930

3031
/**
3132
* Properties for the management server (e.g. port and path settings).
3233
*
3334
* @author Dave Syer
35+
* @author Stephane Nicoll
3436
* @see ServerProperties
3537
*/
3638
@ConfigurationProperties(prefix = "management", ignoreUnknownFields = true)
@@ -103,12 +105,22 @@ public void setAddress(InetAddress address) {
103105
this.address = address;
104106
}
105107

108+
/**
109+
* Return the context path with no trailing slash (i.e. the '/' root context is
110+
* represented as the empty string).
111+
* @return the context path (no trailing slash)
112+
*/
106113
public String getContextPath() {
107114
return this.contextPath;
108115
}
109116

110117
public void setContextPath(String contextPath) {
111-
this.contextPath = contextPath;
118+
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) {
119+
this.contextPath = contextPath.substring(0, contextPath.length() - 1);
120+
}
121+
else {
122+
this.contextPath = contextPath;
123+
}
112124
}
113125

114126
public Security getSecurity() {

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package org.springframework.boot.actuate.autoconfigure;
1818

1919
import org.junit.Test;
20-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
21-
import org.springframework.context.annotation.Bean;
22-
import org.springframework.context.annotation.Configuration;
2320

2421
import static org.hamcrest.Matchers.equalTo;
2522
import static org.hamcrest.Matchers.nullValue;
@@ -29,37 +26,38 @@
2926
* Tests for {@link ManagementServerPropertiesAutoConfiguration}.
3027
*
3128
* @author Phillip Webb
29+
* @author Stephane Nicoll
3230
*/
3331
public class ManagementServerPropertiesAutoConfigurationTests {
3432

3533
@Test
3634
public void defaultManagementServerProperties() {
37-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
38-
ManagementServerPropertiesAutoConfiguration.class);
39-
assertThat(context.getBean(ManagementServerProperties.class).getPort(),
40-
nullValue());
41-
context.close();
35+
ManagementServerProperties properties = new ManagementServerProperties();
36+
assertThat(properties.getPort(), nullValue());
37+
assertThat(properties.getContextPath(), equalTo(""));
4238
}
4339

4440
@Test
45-
public void definedManagementServerProperties() throws Exception {
46-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
47-
Config.class, ManagementServerPropertiesAutoConfiguration.class);
48-
assertThat(context.getBean(ManagementServerProperties.class).getPort(),
49-
equalTo(Integer.valueOf(123)));
50-
context.close();
41+
public void definedManagementServerProperties() {
42+
ManagementServerProperties properties = new ManagementServerProperties();
43+
properties.setPort(123);
44+
properties.setContextPath("/foo");
45+
assertThat(properties.getPort(), equalTo(123));
46+
assertThat(properties.getContextPath(), equalTo("/foo"));
5147
}
5248

53-
@Configuration
54-
public static class Config {
55-
56-
@Bean
57-
public ManagementServerProperties managementServerProperties() {
58-
ManagementServerProperties properties = new ManagementServerProperties();
59-
properties.setPort(123);
60-
return properties;
61-
}
49+
@Test
50+
public void trailingSlashOfContextPathIsRemoved() {
51+
ManagementServerProperties properties = new ManagementServerProperties();
52+
properties.setContextPath("/foo/");
53+
assertThat(properties.getContextPath(), equalTo("/foo"));
54+
}
6255

56+
@Test
57+
public void slashOfContextPathIsDefaultValue() {
58+
ManagementServerProperties properties = new ManagementServerProperties();
59+
properties.setContextPath("/");
60+
assertThat(properties.getContextPath(), equalTo(""));
6361
}
6462

6563
}

0 commit comments

Comments
 (0)