springmvc controls users to jump to the login page when session fails

  • 2020-05-27 05:34:10
  • OfStack

springmvc controls the login user session to jump to the login page after the failure of session. The details are as follows:

Step 1, configure web.xml


Step 2, configure spring-mvc.xml

<!-- Session The failure to intercept  --> 
    <!--  Define interceptors  --> 
        <!--  Matching is url Path,   If not configured or /**, Will intercept all Controller -->  
        <mvc:mapping path="/**" />  
        <!--  Addresses that do not need to be intercepted  --> 
        <mvc:exclude-mapping path="/login.do" /> 
        <bean class="com.cm.contract.controller.annotation.GEISSSessionTimeoutInterceptor"></bean>   

Step 3, write the interceptor SystemSessionInterceptor method

public class SystemSessionInterceptor implements HandlerInterceptor { 
  private static final String LOGIN_URL="/jsp/sessionrun.jsp"; 
  public void postHandle(HttpServletRequest request, 
      HttpServletResponse response, Object handler, 
      ModelAndView modelAndView) throws Exception { 
  public void afterCompletion(HttpServletRequest request, 
      HttpServletResponse response, Object handler, Exception ex) 
      throws Exception { 
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
      Object handler) throws Exception { 
    HttpSession session=request.getSession(true); 
    //session Get the user name information in  
    Object obj = session.getAttribute(CMConstant.LOGINUSER); 
    if (obj==null||"".equals(obj.toString())) { 
         return false;
      return true;

Step 5: configure the friendly tips page sessionrun.jsp

  <SCRIPT language="JavaScript"> 
    alert(" The user has logged in elsewhere, please log in again. "); 
    setTimeout(function () { 

This is the end of springMvc intercept session post-failure processing.

Related articles: