| package com.matrix.system.common.interceptor; | 
|   | 
| import com.matrix.core.constance.MatrixConstance; | 
| import com.matrix.core.tools.WebUtil; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.web.servlet.HandlerInterceptor; | 
| import org.springframework.web.servlet.ModelAndView; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import javax.servlet.http.HttpServletResponse; | 
|   | 
| /** | 
|  * 身份认证拦截器 | 
|  *  | 
|  * @author JIANGYOUYAO | 
|  * @email 935090232@qq.com | 
|  * @date 2017年12月8日 | 
|  */ | 
| @Component | 
| public class UserLoginInterceptor implements HandlerInterceptor { | 
|   | 
|     private static final String LOGIN_TIME_OUT = "loginTimeOut..."; | 
|     private static final String X_REQUESTED_WITH = "X-Requested-With"; | 
|     private static final String DO_COMMON_REDIRECT_LOGIN = "/common/toLogin"; | 
|     private static final String MOBILE_REDIRECT_LOGIN = "/common/hmlogin"; | 
|     private static final String DEVELOPER = "/developer/"; | 
|     private static final String CUSTOMER = "/customer/"; | 
|     private static final String SUPER = "/super/"; | 
|     private static final String ADMIN = "/admin/"; | 
|     /** | 
|      * 检查管理员是否登陆 | 
|      */ | 
|     @Override | 
|     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { | 
|   | 
|          String requestUrl = request.getRequestURI(); | 
|         // 如果访问特殊的路径需要验证管理员的登录权限 | 
|         if (requestUrl.indexOf(ADMIN) != -1 || requestUrl.indexOf(SUPER) != -1 | 
|                 || requestUrl.indexOf(DEVELOPER) != -1 || requestUrl.indexOf(CUSTOMER) != -1) { | 
|   | 
|             if (WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY) == null) { | 
|                 // 判断是否为异步请求 | 
|                 String requestType = request.getHeader(X_REQUESTED_WITH); | 
|                 if (requestType == null) { | 
|                     if(requestUrl.contains("/mobile/")){ | 
|                         response.sendRedirect(request.getContextPath() + MOBILE_REDIRECT_LOGIN); | 
|                     }else{ | 
|                         response.sendRedirect(request.getContextPath() + DO_COMMON_REDIRECT_LOGIN); | 
|                     } | 
|   | 
|                 } else { | 
|                     response.getWriter().write(LOGIN_TIME_OUT); | 
|                 } | 
|                 return false; | 
|             } else { | 
|                 return true; | 
|             } | 
|         } else { | 
|             return true; | 
|         } | 
|     } | 
|     @Override | 
|     public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) | 
|             throws Exception { | 
|     } | 
|     @Override | 
|     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) | 
|             throws Exception { | 
|     } | 
| } |