使用Cookie实现自动登录技术

1.页面勾选保存密码按钮

			
			

2.创建一个Cookie储存提交过来的账号密码(这段代码中包括了登录验证等等)

 
  
		
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session = request.getSession();
		//这段代码是登录时候运行的代码,这段代码会返回一个User对象存储用户的账号密码
		//获取数据
		String username = request.getParameter("login_userName");//中文(例:张三)
		String password = request.getParameter("login_password");
		
		UserService service = new UserService();//这里是用于判断账号密码是否正确的
		User user = null;
		try {
			user = service.login(username,password);
			//System.out.println(user.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}//账号密码正确会返回"user"这个对象
		

		//下面这段代码是自动登录的代码(会使用到上面用户存储的账号密码)(用户登录成功的跳转页面也在下面(重定向))
		if(user!=null){
			//登录成功
			//判断用户是否勾选自动登录
			String autoLogin = request.getParameter("autoLogin");
			if(autoLogin!=null){
				//System.out.println("username"+username+"password111"+password);
				//对中文张三进行编码(cookie不能存储中文)
				String username_code = URLEncoder.encode(username, "UTF-8");// %AE4%kfj
				//System.out.println("username_code="+username_code);
				Cookie cookie_username = new Cookie("cookie_username",username_code);//创建cookie
				Cookie cookie_password = new Cookie("cookie_password",password);
				//设置cookie的持久化时间
				cookie_username.setMaxAge(60*60);//单位秒
				cookie_password.setMaxAge(60*60);
				//设置cookie的携带路径
				cookie_username.setPath(request.getContextPath());//设置为当前项目下都携带这个cookie
				cookie_password.setPath(request.getContextPath());
				//发送cookie
				response.addCookie(cookie_username);//向客户端发送cookie
				response.addCookie(cookie_password);
				//System.out.println("000000");//这段代码会运行
			}
			
			//将登录的用户的user对象存到session中
			session.setAttribute("user", user);//使每个页面都能获得账号的相关信息。
			//重定向到首页
			response.sendRedirect(request.getContextPath());
			
		}else{
			//失败 转发到登录页面 提出提示信息
			request.setAttribute("login_userName", username);
			//request.setAttribute("login_password", password);
			request.setAttribute("loginInfo", "用户名或密码错误");
			request.getRequestDispatcher("Login/login.jsp").forward(request, response);
		}



3.创建一个过滤器,获取Cookie并判断Cookie中是否已有储存号的账号密码,并实现登录

                HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		//获得cookie中用户名和密码 进行登录的操作
		//定义cookie_username
		String cookie_username = null;
		//定义cookie_password
		String cookie_password = null;
		//获得cookie
		Cookie[] cookies = req.getCookies();
		//System.out.println(cookies[0].getName()+" "+cookies[1].getName());//直面只存了SSSIONID
		if(cookies!=null){
			for(Cookie cookie : cookies){
				//获得名字是cookie_username和cookie_password
				if("cookie_username".equals(cookie.getName())){
					cookie_username = cookie.getValue();
					//恢复中文用户名
					cookie_username = URLDecoder.decode(cookie_username, "UTF-8");
					//System.out.println("22222222");//会运行
				}
				if("cookie_password".equals(cookie.getName())){
					cookie_password = cookie.getValue();
					//System.out.println("wwwwwwwwwwwwwww");
				}
			}
		}
		//判断username和password是否是null(这里与上面那段是连续的,上面的复制,这里的验证登录)
		if(cookie_username!=null&&cookie_password!=null){
			//System.out.println("eeeeeeeeeeeeeeeeee");
			//登录的代码
			UserService service = new UserService();
			User user = null;
			try {
				user = service.login(cookie_username,cookie_password);
				//System.out.println("1111111111");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//将登录的用户的user对象存到session中
			session.setAttribute("user", user);
		}
		
		//放行
		chain.doFilter(req, resp);
		

4.在主页面的退出按钮那里写好清除Cookie中存储的账号密码的代码

		//1.清空cookie中存储的值
		Cookie cookie_username = new Cookie("cookie_username"," ");
		Cookie cookie_password = new Cookie("cookie_password"," "); 
		cookie_username.setMaxAge(0);
		cookie_password.setMaxAge(0);
		cookie_username.setPath(request.getContextPath());
		cookie_password.setPath(request.getContextPath());
		response.addCookie(cookie_username);
		response.addCookie(cookie_password);
		HttpSession session = request.getSession();
		
		//2.清空session中储存的账号密码
		session.setAttribute("user", null);
		
		response.sendRedirect(request.getContextPath());

你可能感兴趣的:(JAVA)