1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class AutoLoginFilter implements Filter { @Override public void destroy() { }
@Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; String requestURI = request.getRequestURI(); System.out.println("0 requestURI:" + requestURI); if (requestURI.contains("login")) { chain.doFilter(req, resp); } else { Userinfo existUser = (Userinfo) request.getSession().getAttribute("existUser"); System.out.println("1 existUser:" + existUser); if (null == existUser) { Cookie cookie = CookieUtils.getCookie(request.getCookies(), "autoLogin"); if (null == cookie) { request.getRequestDispatcher("/login.jsp").forward(request, resp); } else { String[] infos = cookie.getValue().split("-"); String username = infos[0]; String password = infos[1]; System.out.println("3 username="+username+",password="+password); if ("root".equals(username) && "1234".equals(password)) { System.out.println("自动登陆成功"); existUser = new Userinfo(); existUser.setUsername(username); existUser.setPassword(password); request.getSession().setAttribute("existUser", existUser); chain.doFilter(req, resp); } else { System.out.println("自动登陆失败"); request.getRequestDispatcher("/login.jsp").forward(request, resp); } } } else { chain.doFilter(req, resp); } } }
@Override public void init(FilterConfig config) throws ServletException {
} }
|