實現自動登陸的關鍵在於,將用戶的信息保存在cookie中,而後等再次訪問該網站的時候首先使用攔截器進行訪問請求進行攔截,攔截器首先會從cookie中獲取用戶的信息,若是cookie中保存的有用戶的信息則進行用戶想要的操做,不然要進行登錄。javascript
主要的代碼以下:java
首先在jsp中有一個checkbox進行自動登陸選擇:apache
<input type="checkbox" name="autologin" id="autologin"/>下次自動登陸
在登陸中進行判斷,並保存用戶信息到cookie中:cookie
//登陸 public String login(){ String account=model.getAccount(); JsonTip result=new JsonTip("succ",""); try{ String check=AccountUtil.isLegal(account); if(Checker.isEmpty(check)){ result.setType("fail"); result.setMsg("帳號不存在"); }else{ ExUser loginUser=exUserService.verifyLogon(account, model.getPwd()); if(Checker.isEmpty(loginUser)){ result.setType("fail"); result.setMsg("帳號或密碼錯誤"); }else{ String status=loginUser.getStatus(); if("N".equals(status)){ result.setType("fail"); result.setMsg("帳號還沒有激活<a id='reSendEmail' href='javascript:reSendEmail();'>點擊從新發送激活郵件</a>"); }else if("Y".equals(status)){ UserInfo lu=new UserInfo(loginUser.getId()+"", loginUser.getNickname()); getHttpRequest().getSession().setAttribute("USER_INFO",lu); String aultlogin = model.getAutologin(); if(!Checker.isEmpty(aultlogin)){ int seconds=30*24*60*60; Cookie cookie = new Cookie("exuser",account+"=="+model.getPwd()); cookie.setMaxAge(seconds); this.getHttpResponse().addCookie(cookie); //把登錄信息保存到cookie中 } } } } }catch (Exception e){ result.setType("fail"); result.setMsg("登陸失敗"); _log.error("login["+account+"]exp:"+e.getMessage()); } print(Json.toJson(result)); return null; }
在攔截器中進行攔截判斷:jsp
public String intercept(ActionInvocation invocation) throws Exception { ActionContext actionContext = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest) actionContext .get(org.apache.struts2.StrutsStatics.HTTP_REQUEST); HttpServletResponse response = (HttpServletResponse) actionContext .get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE); UserInfo user=(UserInfo)request.getSession().getAttribute("USER_INFO"); String url = request.getServletPath(); if(Checker.isEmpty(user)&&!url.startsWith("/login_out")){//用戶主動點擊退出的時候,再也不去cookie中拿值 getUserFromCookie(request); } if(url.startsWith("/login_out")){ Cookie cookie = new Cookie("exuser",null); cookie.setMaxAge(0); response.addCookie(cookie); //把登錄信息保存到cookie中 } log(request); return invocation.invoke(); } private void getUserFromCookie(HttpServletRequest request) throws Exception { Cookie[] cookies = request.getCookies(); String[] cooks = null; String username = null; String password = null; ExUser loginUser = null; if (cookies != null) { for (Cookie coo : cookies) { if ("exuser".equals(coo.getName())) { String values = coo.getValue(); cooks = values.split("=="); if (cooks.length == 2) { username = cooks[0]; password = cooks[1]; } break; } } if (null != username && null != password) { loginUser = exUserService.verifyLogon(username, password); } if (!Checker.isEmpty(loginUser)) { UserInfo lu = new UserInfo(loginUser.getId() + "", loginUser.getNickname()); request.getSession().setAttribute("USER_INFO", lu); } } }
在struts中進行配置網站
<package name="global" extends="struts-default"> <interceptors> <interceptor name="accessLogInterceptor" class="cn.gzjp.common.interceptor.AccessLogInterceptor"></interceptor> <interceptor-stack name="accessLogStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="accessLogInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="accessLogStack"></default-interceptor-ref> </package>