Skip to content

Commit 2df03d6

Browse files
committed
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
1 parent 2f54b27 commit 2df03d6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
6969
import org.springframework.web.servlet.HandlerAdapter;
7070
import org.springframework.web.servlet.HandlerExceptionResolver;
71+
import org.springframework.web.servlet.HandlerInterceptor;
7172
import org.springframework.web.servlet.HandlerMapping;
7273
import org.springframework.web.servlet.ViewResolver;
7374
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
@@ -379,9 +380,15 @@ public HandlerMapping resourceHandlerMapping() {
379380
addResourceHandlers(registry);
380381

381382
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
382-
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
383-
handlerMapping.setPathMatcher(mvcPathMatcher());
384-
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
383+
if (handlerMapping != null) {
384+
handlerMapping.setPathMatcher(mvcPathMatcher());
385+
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
386+
handlerMapping.setInterceptors(new HandlerInterceptor[] {
387+
new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider())});
388+
}
389+
else {
390+
handlerMapping = new EmptyHandlerMapping();
391+
}
385392
return handlerMapping;
386393
}
387394

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public void handlerMappings() throws Exception {
140140
assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass());
141141
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
142142
assertNotNull(chain.getHandler());
143+
assertEquals(Arrays.toString(chain.getInterceptors()), 2, chain.getInterceptors().length);
144+
// PathExposingHandlerInterceptor at chain.getInterceptors()[0]
145+
assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[1].getClass());
143146

144147
handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
145148
handlerMapping.setApplicationContext(this.context);

0 commit comments

Comments
 (0)