From 9cc6bff177e368fb7da0409f649123c65998e6ba Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 16 Jun 2014 17:26:54 +0100 Subject: [PATCH] Increase priority of EndpointHandlerMapping The Actuator endpoints ought to take precedence over others (since they are important and users can isolate them using management.contextPath if necessary). This change puts the @Order at -100 (well before the default order of the RequestMappingHandlerMapping at 0). Fixes gh-1107 --- .../endpoint/mvc/EndpointHandlerMapping.java | 3 +- .../EndpointMvcIntegrationTests.java | 106 ++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java index 6b325daaec..59a3339101 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java @@ -64,7 +64,8 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme public EndpointHandlerMapping(Collection endpoints) { this.endpoints = new HashSet(endpoints); // By default the static resource handler mapping is LOWEST_PRECEDENCE - 1 - setOrder(LOWEST_PRECEDENCE - 2); + // and the RequestMappingHandlerMapping is 0 (we ideally want to be before both) + setOrder(-100); } @Override diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java new file mode 100644 index 0000000000..e2d02a96bf --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointMvcIntegrationTests.java @@ -0,0 +1,106 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.Collections; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.autoconfigure.EndpointMvcIntegrationTests.Application; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration; +import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; +import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebAppConfiguration +@IntegrationTest("server.port=0") +@DirtiesContext +public class EndpointMvcIntegrationTests { + + @Value("${local.server.port}") + private int port; + + @Test + public void envEndpointNotHidden() { + String body = new TestRestTemplate().getForObject("http://localhost:" + this.port + + "/env/user.dir", String.class); + assertNotNull(body); + assertTrue("Wrong body: \n" + body, body.contains("spring-boot-actuator")); + } + + @Target(ElementType.TYPE) + @Retention(RetentionPolicy.RUNTIME) + @Documented + @Import({ EmbeddedServletContainerAutoConfiguration.class, + ServerPropertiesAutoConfiguration.class, + DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class, + HttpMessageConvertersAutoConfiguration.class, + ErrorMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class }) + protected static @interface MinimalWebConfiguration { + } + + @Configuration + @MinimalWebConfiguration + @Import({ ManagementServerPropertiesAutoConfiguration.class, + EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class }) + @RestController + protected static class Application { + + @RequestMapping("/{name}/{env}/{bar}") + public Map master(@PathVariable String name, + @PathVariable String env, @PathVariable String label) { + return Collections.singletonMap("foo", (Object) "bar"); + } + + @RequestMapping("/{name}/{env}") + public Map master(@PathVariable String name, + @PathVariable String env) { + return Collections.singletonMap("foo", (Object) "bar"); + } + + } + +}