cookie+intercepter实现自动登录

实现自动登录的关键在于,将用户的信息保存在cookie中,然后等再次访问该网站的时候首先使用拦截器进行访问请求进行拦截,拦截器首先会从cookie中获取用户的信息,如果cookie中保存的有用户的信息则进行用户想要的操作,否则要进行登陆。

主要的代码如下:

首先在jsp中有一个checkbox进行自动登录选择:

<input type="checkbox"  name="autologin" id="autologin"/>下次自动登录

在登录中进行判断,并保存用户信息到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;
 }

在拦截器中进行拦截判断:

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>

转载于:https://my.oschina.net/u/933395/blog/307073

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值