一,springmvc的配置
<!-- 访问拦截 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**/**"/> <bean class="com.fh.interceptor.LoginHandlerInterceptor"/> </mvc:interceptor> </mvc:interceptors>
二,拦截类
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub String path = request.getServletPath(); if(path.matches(Const.NO_INTERCEPTOR_PATH)){ return true; }else{ User user = (User)Jurisdiction.getSession().getAttribute(Const.SESSION_USER); if(user!=null){ path = path.substring(1, path.length()); boolean b = Jurisdiction.hasJurisdiction(path); //访问权限校验 if(!b){ response.sendRedirect(request.getContextPath() + Const.LOGIN); } return b; }else{ //登陆过滤 response.sendRedirect(request.getContextPath() + Const.LOGIN); return false; } } } }
三,静态类
public static final String NO_INTERCEPTOR_PATH = ".*/((login)|(logout)|(code)|(app)|(weixin)|(static)|(main)|(websocket)).*"; //不对匹配该值的访问路径拦截(正则)
四,解释
只要进入Controller之前都会进入这个拦截器