java web session+cookie实现用户自动登录
在之前的博文中介绍了项目中防止用户重复登录的方案及解决非法退出异常的处理方法——监听浏览器关闭事件onbeforeunload,发送ajax请求到服务器端执行正常退出程序,以避免用户被锁死的情况。然后在实际的测试中发现,有些浏览器如火狐是无法监听到beforeunload事件的,除此以外,在本地和内部测试服务器都能成功监听到,部署到项目的电信服务器上就经常出现无法监听的情况,这样在使用时非常影响用户体验。因此,考虑使用session和cookie实现用户自动登录的方案,这样即使用户不小心关闭浏览器,再次输入项目地址时能直接登入系统,从而避免了用户直接关闭浏览器后无法再次登录。
首先,在项目默认页index.jsp,进行重定向操作至logincookie.action。
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; response.sendRedirect(basePath+"logincookie.action"); %>在logincookie.action方法中,获取当前的cookie和session,如果cookie中存在用户信息(username)且session中存放有已登录用户的信息(logicId,username,logintime等),则自动跳转至登录成功页面(action跳转),否则进入用户登录页面。
public String loginCookie() throws Exception{ ActionContext acx=ActionContext.getContext(); Cookie[] cookies=this.request.getCookies(); if(cookies==null){ return "login"; } HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session=request.getSession(false); String sessionId = session.getId(); if(ifCookieUserAnddiffSession(sessionId)!=null) { //清理loginUserMap中的用户信息 username=ifCookieUserAnddiffSession(sessionId).getValue(); userlogicId=userManageService.findbyUsername(username).getLogicId(); loginUserMap = (Map<Long, String>) acx.getApplication().get(WebConstant.LOGIN_USER_MAP); if(loginUserMap!=null&&loginUserMap.containsKey(userlogicId)) { loginUserMap.remove(userlogicId); session.getServletContext().setAttribute(WebConstant.LOGIN_USER_MAP, loginUserMap); } } //如果session发生变化(过期),进入登录页面 for(Cookie cookie:cookies) { if(cookie.getName().equals("JSESSIONID")) { if(!cookie.getValue().equals(sessionId)){ return "login"; } } } //自动登录 for(Cookie cookie2:cookies) { if(cookie2.getName().equals("user")&&cookie2.getValue()!=null){ username=cookie2.getValue(); userlogicId=userManageService.findbyUsername(username).getLogicId(); try{ if(acx.getSession().get(WebConstant.USER_ID).toString().equals(username)) return "mainframe"; else { return "login"; } }catch(NullPointerException e) { return "login"; } } } return "login"; }值得一提的是,当用户拦截器拦截非法注入等操作的时候,也将跳回index.jsp,这时的sessionId发生变化,需先清除loginUserMap并进入登录页。
当用户登录时,新建一个cookie,将username作为一个属性存放在cookie中,设置cookie的失效时间,然后将sessionId存放到cookie的JsessionId属性中,添加cookie。
//添加cookie Cookie cookie=new Cookie("user",username); cookie.setMaxAge(10*60*60); //设置cookie失效时间 cookie.setPath("/"); response.addCookie(cookie); if(cookies!=null) { for(Cookie cookie2:cookies) { if(cookie2.getName().equals("JSESSIONID")) { cookie2.setValue(sessionId); cookie2.setMaxAge(10*60*60); cookie2.setPath("/"); response.addCookie(cookie2); } } }
当用户执行安全退出时,清除cookie。
// 清除cookie Cookie[] cookies=request.getCookies(); if(cookies!=null) { for(Cookie cookie: cookies){ if(cookie.getValue().equals(username)) { cookie.setValue(null); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); } } }这样就能实现session结合cookie的自动登录。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: java web 用户单点登录的方案的基本实现
- 下一篇:没有了