SpringMVC configured the interceptor to implement methods of login control

  • 2020-06-15 09:08:56
  • OfStack

SpringMVC reads Cookie to determine whether a user is logged in or not, and determines each action. Previously, the jstl tag was used on the page to determine if session was not logged in and the following code was used to jump to the login page.


<c:if test="${sessionScope.login == null || sessionScope.login == false}">
 <!--  Not logged in  -->
 <c:redirect url="/login"/>
</c:if>
<c:if test="${sessionScope.login}">
 <!--  Is logged in  -->
</c:if>

But the tests found that if session expired, the page rendering stopped and did not jump to the login page. So try to use interceptors for login judgment.

The interceptor configuration file is shown below


<!-- <mvc:mapping path="/**" />  If only write 1 a * Can not intercept similar /*/* The request. Requests for static resources need to be judged not to be intercepted  -->
<mvc:interceptors>
 <mvc:interceptor>
  <mvc:mapping path="/**" />
  <bean class="com.ts.settle.tools.interceptor.LoginInterceptor">
   <property name="excludedUrls">
    <list>
     <value>/login</value>
     <value>/static/</value>
    </list>
   </property>
  </bean>
 </mvc:interceptor>
</mvc:interceptors>

The interceptor implementation class is as follows


public class LoginInterceptor implements HandlerInterceptor {
 private AvatarLogger logger = AvatarLoggerFactory.getLogger(this.getClass());

 private List<String> excludedUrls;

 /**
  *  in DispatcherServlet Called after the request is fully processed 
  *  When an interceptor throws an exception , Will still execute the interceptor back from the current interceptor afterCompletion()
  */
 public void afterCompletion(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception exception)
   throws Exception {

 }

 // After the business processor has processed the request execution , Actions performed before the view is generated 
 public void postHandle(HttpServletRequest request, HttpServletResponse response,
       Object handler, ModelAndView modelAndView) throws Exception {

 }

 /**
  *  Is invoked before the business processor processes the request 
  *  If the return false  Exits this interceptor, the following interceptor postHandle with afterCompletion No longer perform 
  */
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {

  String requestUri = request.getRequestURI();
  for (String url : excludedUrls) {
   if (requestUri.contains(url)) {
    return true;
   }
  }

  HttpSession session = request.getSession();
  Boolean login = (Boolean) session.getAttribute("login");
  if (login == null || !login) {
   //System.out.println(request.getContextPath());
   logger.info("Pedirect to login page");
   response.sendRedirect(request.getContextPath() + "/login");
  }
  return true;
 }

 public void setExcludedUrls(List<String> excludedUrls) {
  this.excludedUrls = excludedUrls;
 }
}


Related articles: