More care taken with management.contextPath

The management.contextPath property should now be respected in a
secure application, whether or not the management.port is different.

Added some test cases in the sample to verify.

Fixes gh-469
pull/470/head
Dave Syer 11 years ago
parent 44826812db
commit 6657e3ef84

@ -97,8 +97,11 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
public EndpointHandlerMapping endpointHandlerMapping() {
EndpointHandlerMapping mapping = new EndpointHandlerMapping(mvcEndpoints()
.getEndpoints());
mapping.setDisabled(ManagementServerPort.get(this.applicationContext) != ManagementServerPort.SAME);
boolean disabled = ManagementServerPort.get(this.applicationContext) != ManagementServerPort.SAME;
mapping.setDisabled(disabled);
if (!disabled) {
mapping.setPrefix(this.managementServerProperties.getContextPath());
}
return mapping;
}

@ -218,7 +218,7 @@ public class ManagementSecurityAutoConfiguration {
List<String> paths = new ArrayList<String>(endpoints.size());
for (MvcEndpoint endpoint : endpoints) {
if (endpoint.isSensitive() == secure) {
paths.add(endpoint.getPath());
paths.add(endpointHandlerMapping.getPrefix() + endpoint.getPath());
}
}
return paths.toArray(new String[paths.size()]);

@ -138,6 +138,13 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme
this.prefix = prefix;
}
/**
* @return the prefix used in mappings
*/
public String getPrefix() {
return this.prefix;
}
/**
* Sets if this mapping is disabled.
*/
@ -158,4 +165,5 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme
public Set<? extends MvcEndpoint> getEndpoints() {
return this.endpoints;
}
}

@ -16,6 +16,8 @@
package sample.actuator;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -41,8 +43,6 @@ import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
/**
* Integration tests for endpoints configuration.
*
@ -78,6 +78,12 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
testError();
}
@Test
public void testCustomContextPath() throws Exception {
start(SampleActuatorApplication.class, "--management.contextPath=/admin");
testHealth();
}
private void testError() {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate("user", "password").getForEntity(
@ -89,6 +95,18 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
assertEquals(999, body.get("status"));
}
private void testHealth() {
ResponseEntity<String> entity = getRestTemplate().getForEntity(
"http://localhost:8080/admin/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
String body = entity.getBody();
assertEquals("ok", body);
}
private RestTemplate getRestTemplate() {
return getRestTemplate(null, null);
}
private RestTemplate getRestTemplate(final String username, final String password) {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
@ -122,5 +140,4 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
return restTemplate;
}
}

@ -51,7 +51,7 @@ public class ManagementSampleActuatorApplicationTests {
@BeforeClass
public static void start() throws Exception {
final String[] args = new String[] { "--server.port=" + port,
"--management.port=" + managementPort, "--management.address=127.0.0.1" };
"--management.port=" + managementPort, "--management.address=127.0.0.1", "--management.contextPath=/admin" };
Future<ConfigurableApplicationContext> future = Executors
.newSingleThreadExecutor().submit(
new Callable<ConfigurableApplicationContext>() {
@ -82,7 +82,7 @@ public class ManagementSampleActuatorApplicationTests {
@Test
public void testHealth() throws Exception {
ResponseEntity<String> entity = getRestTemplate().getForEntity(
"http://localhost:" + managementPort + "/health", String.class);
"http://localhost:" + managementPort + "/admin/health", String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("ok", entity.getBody());
}

Loading…
Cancel
Save