登录实现"记住我"功能

创建cookie

前端表单代码:

<form class="loginForm" id="loginForm" action="${ctx}/sys/user/loginUser" method="post">
  <div class="form-group">
	  <input class="form-control" name="name" id="name" type="text" placeholder="请输入用户名">
  </div>
  <div class="form-group">
	  <input class="form-control" name="password" id="password" type="password" placeholder="请输入密码">
  </div>
  <div class="form-group">
	  <label for="remember" style="color: #fff;font-size: 16px;">
		<input type="checkbox" name="remember" id="remember" value="0"/> 记住我
	  </label>
  </div>
  <div class="form-group">
	  <button class="btn btn-primary" type="submit">登录</button>
  </div>
</form>

controller层代码:

/**
 * 登录
 * remember 是否勾选"记住我"复选框,没有选中为null
 */
@RequestMapping(value = {"loginUser"})
public String loginUser(User user,String remember,Model model,HttpSession session,HttpServletRequest request, HttpServletResponse response){
	try {
		user = userService.userNamelogin(user);
		if (user != null) {
			if (StringUtils.isNotBlank(remember)) {
				CookieUtil.addCookie(user.getName(), user.getPassword(), response, request);
			}
			model.addAttribute("user", user);
			session.setAttribute("USER", user);
			return "index";
		}else{
			model.addAttribute("message", "用户名或密码不正确。");
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return "login";
}

CookieUtil 工具类:

import java.io.UnsupportedEncodingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * 记住我
 */
public class CookieUtil {
	//加入cookie中
	public static void addCookie(String name,String password, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
		if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(password)){
			//创建Cookie
			Cookie nameCookie=new Cookie("name",java.net.URLEncoder.encode(name,"utf-8"));
			Cookie passwordCookie=new Cookie("password",password);
			
			//设置Cookie的父路径
			nameCookie.setPath(request.getContextPath()+"/");
			passwordCookie.setPath(request.getContextPath()+"/");
			//设置cookie的有效时间(天数*24小时*分*秒)
			nameCookie.setMaxAge(7*24*60*60);
			passwordCookie.setMaxAge(7*24*60*60);
			//加入Cookie到响应头
			response.addCookie(nameCookie);
			response.addCookie(passwordCookie);
		}
	}
}

这样cookie就记住了我们的信息,将信息存储到了浏览器中,以后每次打开页面的时候就不用登录了。
setMaxAge(-1)表示当关闭浏览器后cookie就会消失。

销毁cookie

如果要销毁cookie,比如说退出登录,注销登录的时候
点击退出,可以在后台将cookie的值设置为null,有效期设置为0

Cookie name = new Cookie("name",null);
Cookie pwd = new Cookie("password", null);
name.setMaxAge(0);
name.setPath(request.getContextPath()+"/");
response.addCookie(name); 
pwd.setMaxAge(0);
pwd.setPath(request.getContextPath()+"/");
response.addCookie(pwd);
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

符华-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值