本文需要对http协议中Cookies和Session有一定的了解
Cookies: 由服务器生成,通过响应头返回,默认包含SessionId字段,浏览器访问时请求头中包含的一段信息,有效期默认为浏览器关闭。
Session: 服务器保存的用户信息
思路:
浏览器第一次访问,提交表单,服务器返回包含id字段的cookie
第二次访问,如果携带的cookie包含id,则跳转到hellloWorld
若只含有sessionid,跳转到登陆界面
下面先来个cookies的测试
接受和处理Cookies
新建一个控制类
MainController.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.example.cookies;
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
@RestController public class MainController { @RequestMapping(value = "/set", method = RequestMethod.GET) public String setCookies(HttpServletResponse response){ Cookie cookie=new Cookie("sessionId","TestInfo"); response.addCookie(cookie); return "添加Cookies成功"; }
@RequestMapping("/test") public String testCookieValue(@CookieValue("sessionId") String sessionId ) { return "sessionId="+sessionId; } }
|
两个get方法分别对应设置cookies和分析cookies。利用Cookies的有效期可以验证:打开浏览器先set后test成功,重启浏览器直接test失败。
利用cookie的信息判断用户是否登录
思路:一个get请求”/local”,如果用户携带的cookie包含id字段,如果有,则返回登录后界面helloworld(login.ftl),如果没有,返回登录界面(index.ftl)
为了演示,我们只设置一个固定密码
123456
后续可以利用数据库实现注册功能
实现:注意这是新建了一个全新的项目,使用了Freemaker工具(参见《为数据库操作添加前端页面》
MainController.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.example.test;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@Controller public class MainController { @RequestMapping(value = "/set", method = RequestMethod.POST) public String setCookies(String id, String pwd, HttpServletResponse response){
if(pwd.equals("123456")){ Cookie cookie=new Cookie("id",id); response.addCookie(cookie); return "login"; } else{ return "index"; } } @RequestMapping("/local") public String local(HttpServletRequest request){ Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length > 1){ for(Cookie cookie : cookies) System.out.println(cookie.getValue()); return "login"; } else { return "index"; } }
@ResponseBody @RequestMapping(value = "/test",method = RequestMethod.GET) public String testCookieValue(@CookieValue("id") String id ) { return "id="+id; } }
|
index.ftl1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="/set" method="post"> <label> ID: </label><input type="text" name="id"><br/> <label>密码: </label><input type="password" name="pwd"><br/> <input type="submit" value="登录"> </form> </body> </html>
|
helloworld就不用我写了吧…
我们可以知道,sessionid是Tomcat帮我们自动生成并且是唯一的,我们可以利用数据库将session中的信息保存下来,设置cookies的有效期,即可实现免密登录(记住我)的功能。
这里面使用动态页面的原因是避免用户跳过验证直接登录,如果是静态页面,需要添加拦截器并重定向,这里不再赘述。