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:


@Override 
public boolean preHandle(HttpServletRequest request, 
  HttpServletResponse response, Object handler) throws Exception { 
 String url=request.getRequestURI(); 
 System.out.println(url); 
 String method=request.getMethod(); 
  // Determine if you are logging in  
 if(url.indexOf("/login")>=0&&method.equalsIgnoreCase("post")){ 
   return true; 
 } 
    // Determine if you are doing a registration operation  
    if(url.indexOf("/toAddUserPage")>=0){ 
  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"); 
 if(user!=null){ 
       // Login actions and registration actions cannot be used after login  
       if(url.indexOf("/toLogin")>=0||url.indexOf("/toAddUserPage")>=0){ 
   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="com.ipro.shopping.controller"/> 
  <!--  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 ,  --> 
 <!-- <MVC:RESOURCES LOCATION="/" MAPPING="**.HTML"/> 
 <MVC:RESOURCES LOCATION="/" MAPPING="**.JS"/> 
 <MVC:RESOURCES LOCATION="/" MAPPING="**.CSS"/> 
 <MVC:RESOURCES LOCATION="/" MAPPING="**.PNG"/> 
 <MVC:RESOURCES LOCATION="/" MAPPING="**.GIF"/> 
 <MVC:RESOURCES LOCATION="/" MAPPING="**.JPG"/> --> 
  
 <!--  Configure interceptors  --> 
 <mvc:interceptors> 
  <mvc:interceptor> 
   <mvc:mapping path="/**"/> 
   <bean class="com.ipro.shopping.interceptor.LoginInterceptor"></bean> 
  </mvc:interceptor> 
 </mvc:interceptors> 

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:


<servlet-mapping> 
  <servlet-name>default</servlet-name> 
  <url-pattern>*.jpg</url-pattern> 
 </servlet-mapping> 
 <servlet-mapping> 
  <servlet-name>default</servlet-name> 
  <url-pattern>*.gif</url-pattern> 
 </servlet-mapping> 
 <servlet-mapping> 
  <servlet-name>default</servlet-name> 
  <url-pattern>*.js</url-pattern> 
 </servlet-mapping> 
 <servlet-mapping> 
  <servlet-name>default</servlet-name> 
  <url-pattern>*.css</url-pattern> 
 </servlet-mapping> 
 <servlet-mapping> 
  <servlet-name>default</servlet-name> 
  <url-pattern>*.png</url-pattern> 
 </servlet-mapping> 
  
 <!-- springmvc Front controller  --> 
 <servlet> 
  <servlet-name>springmvc</servlet-name> 
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
  <!-- 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 )  --> 
  <init-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>classpath:springmvc.xml</param-value> 
  </init-param> 
 </servlet> 

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: