您的位置:首页 > 其它

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

2015-02-03 15:45 465 查看
首先,我们需要在登录界面下面做一个复选框,勾选上以后,则自动登录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"));…………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: