spring boot cookie存取登录信息

//登录

@RequestMapping("login")
 public String login(Users user,HttpSession session,Model model,HttpServletResponse response,String pwdcokie) {
	List<Users> u = dser.ulist(user);
	if(u.size()>0) {
		//把用户信息存入session
		session.setAttribute("user", u.get(0));
		
		if(pwdcokie!=null) {
		//把用户信息存入cookie
		//从对象中取出名字 密码
		String name=user.getName();
		String pwd=user.getPwd();
		// new cookie 空间
	    Cookie cname = new Cookie("name", name);
	    Cookie cpwd = new Cookie("pwd", pwd);
		//设置cookie过期时间
	    cname.setMaxAge(60*60);
		cpwd.setMaxAge(60*60);
		//设置全路径
	    cname.setPath("/");
	    cpwd.setPath("/");
	    //响应请求 存入cookie
		response.addCookie(cname);
		response.addCookie(cpwd);
		}else {
			String name=user.getName();
			String pwd=user.getPwd();
			// new cookie 空间
			Cookie cname = new Cookie("name", name);
			Cookie cpwd = new Cookie("pwd", pwd);
			//设置cookie过期时间
			cname.setMaxAge(0);
			cpwd.setMaxAge(0);
			//设置全路径
			cname.setPath("/");
			cpwd.setPath("/");
			//响应请求 存入cookie
			response.addCookie(cname);
			response.addCookie(cpwd);

		}
		
		//把用户名放入session
		session.setAttribute("p", pwdcokie);
	    session.setAttribute("name", u.get(0).getName());
	    return "main";
	}
	return "login";
}

前台<%java%>

 <%
 String name=request.getParameter("");    
 String pwd=request.getParameter("");
 Cookie[] cookies=request.getCookies();
 for(int i=0;i<cookies.length;i++){
 if(cookies[i].getName().equals("name"))
 {
   name=cookies[i].getValue();
 }
 }
  for(int i=0;i<cookies.length;i++){
  if(cookies[i].getName().equals("pwd"))
 {
   pwd=cookies[i].getValue();
 }
 }
 
 //自动登录(跳转)
 // response.sendRedirect("main.jsp");

 %>

form表单

 <form action="/login" method="post">
 <table>
<tr>
<td>用户名:<input type="text" name="name" value="        <%if(name!=null)out.print(name); %>"> </td>
</tr>
 <tr>
 <td>密码:<input type="password" name="pwd" value="   <%if(pwd!=null)out.print(pwd); %>"> </td>
</tr>
<tr>
 <td><input type="checkbox" name="pwdcokie" value="1" ${p=='1'?'checked':''}>记   住密码</td>
</tr>
<tr>
 <td><input type="submit" value="登录" > </td>
</tr>
 </table>
 </form>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值