Details the problem of setting static resources not to be intercepted in SpringMVC

  • 2020-06-03 06:29:06
  • OfStack

In these two days, SpringMVC is used for login. The registered small demo USES springmvc interceptor to intercept login operation and verify url. If the validation passes, then handler processing can be performed if the validation does not pass by directly jumping back or redirecting to the specified page.

One of the big problems with doing this is that static resources in project demo get blocked on every request! When you jump back to the page, it doesn't look like you thought it would look.

I thought I had configured the interceptor and static resource configuration in springmvc.xml, so I looked again at the previous browser Settings, and again at the css file, js file. Locking the target last is a static resource problem, which is solved by using defaultServlet directly in web.xml.

Here is my interceptor code:

public boolean preHandle(HttpServletRequest request, 
  HttpServletResponse response, Object handler) throws Exception { 
 String url=request.getRequestURI(); 
 String method=request.getMethod(); 
  // Determine if you are logging in  
   return true; 
    // Determine if you are doing a registration operation  
  return true; 
  // If the user logs in at time, the user can proceed 1 Step operation   return true 
 HttpSession session=request.getSession(); 
 User user=(User) session.getAttribute("user"); 
       // Login actions and registration actions cannot be used after login  
   request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response); 
   return false; 
  return true; 
 // Other operations go directly back to the login page  
 request.getRequestDispatcher("/WEB-INF/jsp/user/login.jsp").forward(request, response); 
 return false; 

Here's my springmvc configuration:

<!--  Can scan controller , service , here let scan controller , specify controller The package  --> 
 <context:component-scan base-package=""/> 
  <!--  use   Annotation drivers can load processor adapters and processor maps as well json The interpreter does not have to add the configuration of the processor adapter and the core processor map separately  --> 
 <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> 
 <!--  Static resource resolution includes   : js , css , img ,  --> 
 <!--  Configure interceptors  --> 
   <mvc:mapping path="/**"/> 
   <bean class=""></bean> 

I set all the static resources to what form, but the results still do not return the correct answer.

Here is the file after configuring ES35en.xml:

 <!-- springmvc Front controller  --> 
  <!-- contextConfigLocation configuration springmvc The configuration file to load   (Configure processor mapper, adapters, and so on)   If not configured contextConfigLocation .  
    The default is to load /WEB-INF/servlet The name of the -serlvet.xml ( springmvc-servlet.xml )  --> 

Note that our configuration of defaultServlet1 must not take effect until it is configured to the front controller of springmvc (DispatcherServlet).

The above is what I use for login verification, just for reference. At present, only 1 solution has been made for this problem. There are 3 solutions mentioned on the Internet. The first solution is the one I mentioned above

The second is the way it is configured in springmvc, except that I configured it unsuccessfully. The other is also configured using defaultServlet using annotations.

Related articles: