Skip to content

Document options to add interceptors to ResourceHandlerRegistry handler mapping [SPR-10655] #15283

Closed

Description

@spring-projects-issues

Dave Syer opened SPR-10655 and commented

Our use case is this: we need to add a handler interceptor to a resource handler so it can set the content type header for some static resources. We would like to test this with Spring MVC Test, so we want to have this happen in the DispatcherServlet handling chain (i.e. a Filter isn't a great solution).


Referenced from: commits spring-attic/spring-framework-issues@fa0a295

4 votes, 7 watchers

Activity

  1. spring-projects-issues commented on Jun 21, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    HandlerInterceptor's you mean? ResourceHandlerRegistry creates a SimpleUrlHandlerMapping with one or more ResourceHttpRequestHandler mapped by pattern. You can get a hold of this SimpleUrlHandlerMapping by extending WebMvcConfigurationSupport and then set some interceptors on it. Those can be of type HandlerInterceptor or MappedInterceptor if you want them limited to specific patterns.

  2. spring-projects-issues commented on Jul 3, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Dave Syer commented

    What if your application already has a WebMvcConfigurationSupport and you don't want to (or can't) modify it? WHat is the reason for the existing handler mapping not using interceptors, or why can't we change it?

  3. spring-projects-issues commented on Jul 3, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Okay I see what you mean now. Yeah we could add an InterceptorRegistry to ResourceHandlerRegistry and set the resulting interceptors on the HandlerMapping it creates.

  4. spring-projects-issues commented on Jul 3, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    I've set the fix version to 4.0 backlog since I probably won't get to it for M2 but feel free to send a PR if you need it sooner.

  5. spring-projects-issues commented on May 1, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Dave Syer, just confirming this is still needed or if you perhaps found another way? Also, I am wondering would a mapped interceptor meet your use case? A MappedInterceptor declared as a bean is auto-detected in AbstracHandlerMapping.

  6. spring-projects-issues commented on Aug 14, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Julian Ramirez commented

    Hi! In case you still need this, here is my fully functional code. The key part is the overriding of the resourceHandlerMapping method of the WebMvcConfigurationSupport class(this is spring 3.2) :

    	public void addResourceHandlers( ResourceHandlerRegistry registry )
        {		
    		registry.addResourceHandler( "/desktop/partials/public/**" )
    					.addResourceLocations( "/desktop/partials/public/" )
    					.setCachePeriod( Integer.valueOf( environment.getProperty( "application.desktop.partials.cacheInSeconds" ) ) );
    		
    		registry.addResourceHandler( "/desktop/partials/secured/**" )
    					.addResourceLocations( "/desktop/partials/secured/" )
    					.setCachePeriod( Integer.valueOf( environment.getProperty( "application.desktop.partials.cacheInSeconds" ) ) );
        }
    	
        @Override
    	@Bean
    	public HandlerMapping resourceHandlerMapping() 
        {
        	AbstractHandlerMapping mapping = ( AbstractHandlerMapping )super.resourceHandlerMapping();
        	mapping.setInterceptors( getInterceptors() );
    
    		return mapping;
    	}
        
    	public void addInterceptors( InterceptorRegistry registry )
        {		    
    		WebContentInterceptor noCacheInterceptor = new WebContentInterceptor();
    		
    			noCacheInterceptor.setCacheSeconds( 0 );
    			noCacheInterceptor.setUseExpiresHeader( true );
    			noCacheInterceptor.setUseCacheControlHeader( true );
    			noCacheInterceptor.setUseCacheControlNoStore( true );
    		    
    		    
    		registry.addInterceptor( new SubstringMatcherInterceptorWrapper( "services", noCacheInterceptor ) );		
        	registry.addInterceptor( new SubstringMatcherInterceptorWrapper( "partials/secured", new TokenInterceptor() ) );
    	}
  7. spring-projects-issues commented on Oct 13, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Note that you can also declare a MappedInterceptor bean for the resource URLs.

  8. spring-projects-issues commented on Nov 24, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Fleur Kelpin commented

    Adding a MappedInterceptor bean works brilliantly, thanks.

    It did cost us the better part of a day to debug this problem though!
    It is very counterintuitive that

    • addInterceptors and addResourceHandlers don't play nice together
    • there is no way (that we found) to fix this by overriding the methods already present in the WebMvcConfigurationSupport
      It would be nice if this were documented better or (even better) simply fixed.
  9. spring-projects-issues commented on Nov 24, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Well, simply plugging in all configured interceptors into the ResourceHandler mapping at this stage will almost certainly cause a regression somewhere. It's also not unreasonable to me that interceptors for static resources should be separate from interceptors for other reqests.

    I'm turning this into a documentation task since this is indeed not obvious at all. I've modified the title accordingly (was "Add support for interceptors in ResourceHandlerRegistry handler mapping").

  10. spring-projects-issues commented on Dec 2, 2014

    @spring-projects-issues
    CollaboratorAuthor
  11. added
    in: test
    in: web
    type: task
    on Jan 11, 2019
  12. added this to the 4.1.3 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testin: webtype: task

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions