首页 > 代码库 > spring MVC使用Interceptor做用户登录判断

spring MVC使用Interceptor做用户登录判断

在任何一个项目中,我们必须要用到的就是用户登录,那么就少不了用户是否登录的判断,如果我们每一个请求都要去做一次判断,那么就会变得很麻烦,但我们复制粘贴的时候我们就要考虑我们的代码写的是不是有问题,是不是可以重构一下,这里借鉴一下项目中的这种模式:

package interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import logical.login.LoginUser;import org.apache.log4j.Logger;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public class SessionCheckInterceptor extends HandlerInterceptorAdapter {    private static Logger log = Logger.getLogger(SessionCheckInterceptor.class.toString());        public void afterCompletion(HttpServletRequest request,            HttpServletResponse response,            Object handler,            Exception ex) throws Exception {      }            @Override      public void postHandle(HttpServletRequest request,              HttpServletResponse response,            Object handler,            ModelAndView modelAndView) throws Exception {    }            @Override      public boolean preHandle(HttpServletRequest request,              HttpServletResponse response, Object handler) throws Exception {        //判斷此使用者是否可進此連結        request.setCharacterEncoding("UTF-8");        String url = request.getServletPath();            log.info("post URL:"+url);                /*{                    List<HttpSession> sessionList = HttpSessionListenerTest.getAllSession();            log.info("\n\n\n\n Session資料開始============================");            for( int i=0; i < sessionList.size(); i++){                Object ob = sessionList.get(i).getAttribute("loginUser");                if( ob == null ){                    continue;                }                LoginUser result = (LoginUser)ob;                System.out.println( result.getAccountName());            }            log.info("Session資料結束============================");        }        */        /*String headername = "";        for(Enumeration e = request.getHeaderNames(); e.hasMoreElements();){            headername = (String)e.nextElement();            String value = http://www.mamicode.com/request.getHeader(headername);":"+value);        }*/        if(this.isPassUrl(url)){            //判斷是否已登录            LoginUser loginUser = CssUtil.getLoginUser(request);            if(loginUser == null){                //無session則是未登录狀態                log.info(">>>未登录,請重新登录<<<");                response.sendRedirect("../login.jsp");                return false;            }        }        return true;    }    private boolean isPassUrl(String url){        if(!url.endsWith("/login/login")                 && !url.endsWith("/login/chnagePassEntry")                && !url.endsWith("/login/change_Login")                && !url.endsWith("/API/Service")                && !url.endsWith("/API/Service2")                && !url.endsWith("/province.txt")                && !url.endsWith("/city.txt")                && !url.endsWith("/area.txt")                && !url.endsWith(".xml")                 && !url.endsWith(".js")                 && !url.endsWith(".css") && !url.endsWith(".png")                && !url.endsWith(".CSS") && !url.endsWith(".CSS")                 && !url.endsWith(".jpg") && !url.endsWith(".gif")                && !url.endsWith(".JPG") && !url.endsWith(".GIF")){            return true;        }        return false;    }}