From 2df03d6316ce982528f5c8258dd2eb2aff47f9ba Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 13 Oct 2014 16:12:14 -0400 Subject: [PATCH] Add interceptor to resourceHandlerMapping The resourceHandlerMapping in the MVC Java config is not configured with any interceptors, and in particular those added through the InterceptorRegistry, which are otherwise added to all other handler mapping beans created by the config. This means that the ResourceUrlProviderExposingInterceptor (added in 4.0) is also not used for resource requests. This change ensures the ResourceUrlProviderExposingInterceptor is configured on the resourceHandlerMapping. Issue: SPR-12279 --- .../annotation/WebMvcConfigurationSupport.java | 13 ++++++++++--- .../WebMvcConfigurationSupportExtensionTests.java | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 85f29b65577..a52668c41d7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -68,6 +68,7 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.handler.AbstractHandlerMapping; @@ -379,9 +380,15 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv addResourceHandlers(registry); AbstractHandlerMapping handlerMapping = registry.getHandlerMapping(); - handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping()); - handlerMapping.setPathMatcher(mvcPathMatcher()); - handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); + if (handlerMapping != null) { + handlerMapping.setPathMatcher(mvcPathMatcher()); + handlerMapping.setUrlPathHelper(mvcUrlPathHelper()); + handlerMapping.setInterceptors(new HandlerInterceptor[] { + new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())}); + } + else { + handlerMapping = new EmptyHandlerMapping(); + } return handlerMapping; } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java index 89daebb68ce..8b3ec60ef08 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java @@ -140,6 +140,9 @@ public class WebMvcConfigurationSupportExtensionTests { assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif")); assertNotNull(chain.getHandler()); + assertEquals(Arrays.toString(chain.getInterceptors()), 2, chain.getInterceptors().length); + // PathExposingHandlerInterceptor at chain.getInterceptors()[0] + assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[1].getClass()); handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping(); handlerMapping.setApplicationContext(this.context);