通过SSH的过滤器及Cookie实现自动登录2周不退出

 首先,我们需要在登录界面下面做一个复选框,勾选上以后,则自动登录2周,除非点退出后,才取消自动登录

 

看登录的Action代码

…………
if (!(lf.get("auto_login") == null)) {
				int times = 1000 * 60 * 60 * 24 * 14;
				AutoLoginModel alm = new AutoLoginModel();
				alm.setUsername(username);
				alm.setSessionid(hs.getId());
				alm.setTimes(new Date().getTime() + times);
				Cookie cookie = new Cookie("auto_users", username);
				cookie.setPath("/");
				cookie.setMaxAge(times);
				response.addCookie(cookie);
				cookie = new Cookie("auto_id", hs.getId());
				cookie.setPath("/");
				cookie.setMaxAge(times);
				response.addCookie(cookie);
				us.addLoginInfo(alm);
			}
			return mapping.findForward("to_login_suc");

…………


如上代码片段

1、判断是否选中自动登录,如果是则把用户名,这次登录的SESSIONID及到期时间封装存到数据库中,然后生成用户名及SESSIONID的Cookie

2、注意一定要setPath("/");为什么呢?因为不这样做的话,cookie的域不一样,其他页面则无法取到cookie

这样,登录时的Cookie就已经记录好了,然后怎么判断自动登录呢?

我们需要写一个过滤器,用来监听所有请求,然后当发现有Cookie存在的时候,并且里面有记录用户名和密码

然后我们把Cookie取出来,同时我们需要判断此时session里有没有用户登录信息,如果有,则什么都不做,如果没有,则继续步骤,再从数据库中取出上次记录的信息,然后对比,如果Cookie中的用户名和SESSIONID与从数据库读出的最新记录的一样以及和当前时间的毫秒数做对比如果没过期,则取出用户信息,并保存在SESSION中,这样,自动登录就完成了!

我们看看拦截器的代码

…………	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			if (cookies.length > 1) {
				HttpSession hs = request.getSession();
				UsersModel um = (UsersModel) hs.getAttribute("users_info");
				if (um == null) {
					String username = "";
					String sessionid = "";
					for (int i = 0; i < cookies.length; i++) {
						Cookie cookie = cookies[i];
						if (cookie.getName().equalsIgnoreCase("auto_users")) {
							username = cookie.getValue(); // 得到cookie的用户名
						}
						if (cookie.getName().equalsIgnoreCase("auto_id")) {
							sessionid = cookie.getValue(); // 得到cookie的sessionid
						}
					}
					if (!(username.equals("") || sessionid.equals(""))) {

						String store_path = request.getServletContext()
								.getRealPath("/WEB-INF/");
						ApplicationContext context = new FileSystemXmlApplicationContext(
								store_path + "/applicationContext.xml");
						UsersService us = (UsersService) context
								.getBean("usersService");
						AutoLoginModel alm = us.getLoginInfo(username);
						if (alm.getUsername().equals(username)
								&& alm.getSessionid().equals(sessionid)
								&& new Date().getTime() < alm.getTimes()) {
							hs.setAttribute("users_info", us.getUsers(username));
						} else {
							HttpServletResponse response = (HttpServletResponse) arg1;
							for (Cookie cookie : cookies) {
								if ("auto_users".equals(cookie.getName())) {
									cookie = new Cookie("auto_users", "");
									cookie.setPath("/");
									cookie.setMaxAge(0);
									response.addCookie(cookie);
								}
								if ("auto_id".equals(cookie.getName())) {
									cookie = new Cookie("auto_id", "");
									cookie.setPath("/");
									cookie.setMaxAge(0);
									response.addCookie(cookie);
								}
							}
						}
					}
				}
			}
		}
		arg2.doFilter(arg0, arg1);
	}………………

至于过滤器如何配置,这里就不多讲了

 

最后在退出的时候
清除Cookie,删除数据库中保存的记录

…………HttpSession hs = request.getSession();
		UsersModel um = (UsersModel) hs.getAttribute("users_info");
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if ("auto_users".equals(cookie.getName())) {
					cookie = new Cookie("auto_users", "");
					cookie.setMaxAge(0);
					cookie.setPath("/");
					response.addCookie(cookie);
				}
				if ("auto_id".equals(cookie.getName())) {
					cookie = new Cookie("auto_id", "");
					cookie.setMaxAge(0);
					cookie.setPath("/");
					response.addCookie(cookie);
				}
			}
			us.delLoginInfo(um.getUsername());
		}
		hs.invalidate();
		return (mapping.findForward("to_login"));…………


自动登录的功能就完成了,思考了一天的结果,呵呵



 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值