Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude static resources in InterceptorRegistration in Spring MVC #32758

Open
jakubkrolikowski opened this issue May 4, 2024 · 2 comments
Open
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@jakubkrolikowski
Copy link

jakubkrolikowski commented May 4, 2024

I use the "HandlerInterceptor" to set some attributes to "request" at the invocation of each controller and check the URL parameters. I would like to easily exclude this interceptor from all static resources. For various reasons (including the migration of the application from another framework), I have static resources placed in several folders.

Currently I have to do it this way:

@Configuration
public class WebAppConfigExample implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/packed/**","/app/img/**","/app/css/**");
  }
  
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    InterceptorRegistration r = registry.addInterceptor(new HandlerInterceptor() {
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         request.setAttribute("some_specific_attribute", "value");
         if (request.getParameter("param") != null) {
           // do something
         }
         // other operations on request, 
         // or response.sendRedirect is some cases
         return true;
      }
    });
    r.addPathPatterns("/**");

    //  Here "excludeStaticResources" method would be very helpful!
    r.excludePathPatterns("/packed/**","/app/img/**","/app/css/**");
        
  }

}

It would be very useful to have an excludeStaticResources() or excludeResource() method in InterceptorRegistration, so that I don't have to repeat the list of folders with static resources.
Or the includeControllersOnly method, which would set the Interceptor exclusively for MVC Spring controllers.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 4, 2024
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label May 4, 2024
@aditya78910
Copy link

can i try contribute?

@aditya78910
Copy link

aditya78910 commented May 8, 2024

I was exploring how we can provide an option to the developer to configure a HandlerInterceptor which will be invoked only for all RestControllers. One way, this can be achieved is by checking the type of the handler (whether it is RestController annotated).

The option to configure this can be added in the InterceptorRegistration and then at runtime we can add the related mappedinterceptor to the handlerexecutionchain accordingly depending on the handler being ( RestController annotated). We can do this by introducing a check in the MappedInterceptor here

Requesting the esteemed advice and feedback from the spring team if this thinking is in logical direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

4 participants