用户自动登录实现(Cookie)

自动登录勾选框jsp

<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <div class="checkbox">
            <label> <input type="checkbox"> 自动登录
            </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <label> <input
                type="checkbox"> 记住用户名
            </label>
        </div>
    </div>
</div>

访问的servlet用于封装cookie

//用户登录
public void login(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession();

    //获得输入的用户名和密码
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    //将用户名和密码传递给service层
    UserService service = new UserService();
    User user = null;
    try {
        user = service.login(username,password);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    //判断用户是否登录成功 user是否是null
    if(user!=null){
        //登录成功
        //***************判断用户是否勾选了自动登录*****************
        String autoLogin = request.getParameter("autoLogin");
        if("autoLogin".equals(autoLogin)){
            //要自动登录
            //创建存储用户名的cookie
            Cookie cookie_username = new Cookie("cookie_username",user.getUsername());
            cookie_username.setMaxAge(10*60);
            //创建存储密码的cookie
            Cookie cookie_password = new Cookie("cookie_password",user.getPassword());
            cookie_password.setMaxAge(10*60);

            response.addCookie(cookie_username);
            response.addCookie(cookie_password);

        }

        //***************************************************
        //将user对象存到session中
        session.setAttribute("user", user);

        //重定向到首页
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }else{
        request.setAttribute("loginError", "用户名或密码错误");
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }
}

自动登录的filter

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    
    //强转成HttpServletRequest
    HttpServletRequest req = (HttpServletRequest) request;
    
    User user = (User) req.getSession().getAttribute("user");
    
    if(user==null){
        String cookie_username = null;
        String cookie_password = null;
        
        //获取携带用户名和密码cookie
        Cookie[] cookies = req.getCookies();
        if(cookies!=null){
            for(Cookie cookie:cookies){
                //获得想要的cookie
                if("cookie_username".equals(cookie.getName())){
                    cookie_username = cookie.getValue();
                }
                if("cookie_password".equals(cookie.getName())){
                    cookie_password = cookie.getValue();
                }
            }
        }
        
        if(cookie_username!=null&&cookie_password!=null){
            //去数据库校验该用户名和密码是否正确
            UserService service = new UserService();
            try {
                user = service.login(cookie_username,cookie_password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            //完成自动登录 
            req.getSession().setAttribute("user", user);
            
        }
    }
      
    //放行
    chain.doFilter(req, response);
    
}

 

posted @ 2017-07-21 11:19  发福大叔  阅读(969)  评论(0编辑  收藏  举报