Spring MVC - Exclude Assets from All Interceptors

I am trying to figure out how to avoid excluding static content from all interceptors instead of specifying an exclude-mapping tag for each interceptor that should ignore them. Part of my context.xml is as follows:

 <mvc:resources mapping="/assets/**" location="/assets/"/> <mvc:interceptors> <bean class="com.myapp.security.interceptor.SecurityInterceptor" /> <mvc:interceptor> <mvc:mapping path="/**" /> <mvc:exclude-mapping path="/assets/**" /> <bean class="com.myapp.interceptor.MessageInterceptor" /> </mvc:interceptor> </mvc:interceptors> 

As you can see, I pointed /assets/ to an exception from MessageInterceptor. However, I also want SecurityInterceptor to also exclude resources. After DRY, it seems unreasonable to specify exclude-mapping for each interceptor (since there should never be a reason to intercept resources).

I searched around and could not find any solutions. Any help is appreciated!

thanks

+4
source share
1 answer

I'm struggling to come up with some kind of method to achieve what you want in the configuration. Maybe someone else can offer a solution.

A quick code solution that achieves what you want will be something like this:

 public abstract class ResourceExcludingHandlerInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { if (!isResourceHandler(handler)) { doAfterCompletion(request, response, handler, ex); } } public abstract void doAfterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception; public abstract void doPostHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; public abstract boolean doPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; /** * <p> * Determine if the request is about to be handled by a mapping configured * by <mvc:resources> * </p> * * @param handler * - the handler to inspect * @return - true if this is a <mvc:resources> mapped request, false * otherwise */ private boolean isResourceHandler(Object handler) { return handler instanceof ResourceHttpRequestHandler; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (!isResourceHandler(handler)) { doPostHandle(request, response, handler, modelAndView); } } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return isResourceHandler(handler) ? true : doPreHandle(request, response, handler); } } 

After that, you could extend the implementation of this abstract class in HandlerInterceptor implementations. As <mvc: resources> is basically a short way to register ResourceHttpRequestHandler instances, this ensures that HandlerInterceptor implementations simply ignore any requests that are displayed for processing by one.

+4
source

Source: https://habr.com/ru/post/1498615/


All Articles