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
This commit is contained in:
Stephane Nicoll 2015-07-20 10:56:52 +02:00
parent 8fe9fc25b3
commit 539b009d12
2 changed files with 34 additions and 24 deletions

View File

@ -26,11 +26,13 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* Properties for the management server (e.g. port and path settings).
*
* @author Dave Syer
* @author Stephane Nicoll
* @see ServerProperties
*/
@ConfigurationProperties(prefix = "management", ignoreUnknownFields = true)
@ -103,12 +105,22 @@ public class ManagementServerProperties implements SecurityPrerequisite {
this.address = address;
}
/**
* Return the context path with no trailing slash (i.e. the '/' root context is
* represented as the empty string).
* @return the context path (no trailing slash)
*/
public String getContextPath() {
return this.contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) {
this.contextPath = contextPath.substring(0, contextPath.length() - 1);
}
else {
this.contextPath = contextPath;
}
}
public Security getSecurity() {

View File

@ -17,9 +17,6 @@
package org.springframework.boot.actuate.autoconfigure;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@ -29,37 +26,38 @@ import static org.junit.Assert.assertThat;
* Tests for {@link ManagementServerPropertiesAutoConfiguration}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public class ManagementServerPropertiesAutoConfigurationTests {
@Test
public void defaultManagementServerProperties() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
ManagementServerPropertiesAutoConfiguration.class);
assertThat(context.getBean(ManagementServerProperties.class).getPort(),
nullValue());
context.close();
ManagementServerProperties properties = new ManagementServerProperties();
assertThat(properties.getPort(), nullValue());
assertThat(properties.getContextPath(), equalTo(""));
}
@Test
public void definedManagementServerProperties() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
Config.class, ManagementServerPropertiesAutoConfiguration.class);
assertThat(context.getBean(ManagementServerProperties.class).getPort(),
equalTo(Integer.valueOf(123)));
context.close();
public void definedManagementServerProperties() {
ManagementServerProperties properties = new ManagementServerProperties();
properties.setPort(123);
properties.setContextPath("/foo");
assertThat(properties.getPort(), equalTo(123));
assertThat(properties.getContextPath(), equalTo("/foo"));
}
@Configuration
public static class Config {
@Bean
public ManagementServerProperties managementServerProperties() {
ManagementServerProperties properties = new ManagementServerProperties();
properties.setPort(123);
return properties;
}
@Test
public void trailingSlashOfContextPathIsRemoved() {
ManagementServerProperties properties = new ManagementServerProperties();
properties.setContextPath("/foo/");
assertThat(properties.getContextPath(), equalTo("/foo"));
}
@Test
public void slashOfContextPathIsDefaultValue() {
ManagementServerProperties properties = new ManagementServerProperties();
properties.setContextPath("/");
assertThat(properties.getContextPath(), equalTo(""));
}
}