The springmvc interceptor intercepts static resources

  • 2020-06-01 09:39:51
  • OfStack

springmvc interceptor interceptors

The springmvc interceptor's ability to intercept the requested resource path greatly simplifies the writing of the interceptor. However, there is one important point to note: the release of static resources.

The code:

<mvc:resources mapping="/resources/**" location="/static/resources" /> 
  <mvc:resources mapping="/static/css/**" location="/static/css/" /> 
  <mvc:resources mapping="/static/images/**" location="/static/images/" /> 
  <mvc:resources mapping="/static/js/**" location="/static/js/" /> 

    <!--  use bean define 1 a Interceptor Is directly defined in mvc:interceptors The root of the following Interceptor All requests will be intercepted   
    <bean class="com.myTree.interceptor.LoginInterceptor" />--> 
      <mvc:mapping path="/**" /> 
      <!--  Address to exclude interception  -->  
      <mvc:exclude-mapping path="/Login"/>  
      <mvc:exclude-mapping path="/login"/>  
      <mvc:exclude-mapping path="/sattic/**"/>  
      <!--  Defined in the mvc:interceptor The following representation is for specific requests to be intercepted  --> 
      <beans:bean class="com.myTree.interceptor.LoginInterceptor" > 

The problem is that when you request the jsp page, your static resource access will still be blocked by the custom interceptor, which will cause the program to run in a much less efficient way and will constantly jump to the logic of the interceptor. The main reason is that

<mvc:mapping path="/**" /> 

All requested resources will be intercepted, even though the static resource has been eliminated.

How do you solve this bug?

There are three main ways:

1. Modify the url address of the request.

If the requested url address ends in *.do, then the configuration in the interceptor can be changed to intercept the do ending resource, and the static resource will not be intercepted naturally.
2. Judge the resource in the custom interceptor and release it if it meets the need to exclude the resource.

<!--  Interceptor configuration  -->  
   <!-- session timeout  -->  
    <mvc:mapping path="/*/*"/>  
    <bean class="com.myTree.interceptor.LoginInterceptor">  
     <property name="allowUrls">  
       <!--  If the request contains the following path, it is not intercepted  -->  

Set the non-intercepting property in the interceptor:

 *  Handle the login interceptor  
public class LoginInterceptor implements HandlerInterceptor{  
  public String[] allowUrls;// You haven't found a resource that you can configure directly without intercepting, so you can exclude it in your code   
  public void setAllowUrls(String[] allowUrls) {  
    this.allowUrls = allowUrls;  
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response,  
      Object handler) throws Exception {<pre name="code" class="java"> <span style="white-space:pre"> </span>String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");   
    if(null != allowUrls && allowUrls.length>=1){  
      for(String url : allowUrls) {   
        if(requestUrl.contains(url)) {   
          return true;   

3. Set the default interceptor in web.xml to not intercept static resources

It is configured in Dispatcher of springmvc < mvc:default-servlet-handler / > The default Servlet name of the Web application server is "default", so here we activate defaultServlet of Tomcat to handle static files. The following code can be configured in web.xml :)

<!-- the servlet for tomcat,jetty Isocontainer supply , Map static resources from / Instead of /static/ Directory as originally accessed http://localhost/foo.css, now http://localhost/static/foo.css -->  
<!--  Do not intercept static files  -->  

Related articles: