springboot之cookie操作
1.cookie相关知识
https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
https://javascript.ruanyifeng.com/bom/cookie.html#toc5
2.cookie相关操作
2.1.获取
通过HttpServletRequest查询
private String getCookie(HttpServletRequest request,String name){ Cookie[] cookies = request.getCookies(); if(cookies != null){ for(Cookie cookie : cookies){ if(cookie.getName().equals(name)){ return cookie.getValue(); } } } return null; }
通过@CookieValue查询
@RequestMapping("/a/a") String home1(@CookieValue(name = "token", required = false) String token) { System.err.println(token); return ""; }
2.2.新增
Cookie c = new Cookie("token", "b"); response.addCookie(c);
2.3.修改
如果服务器想改变一个早先设置的 Cookie,必须同时满足四个条件:Cookie 的key
、domain
、path
和secure
都匹配。
Cookie c = new Cookie("token", "a"+1); response.addCookie(c);
2.4.删除
将cookie的Max-Age设置为0
Cookie c = new Cookie("token", "a"+1); c.setMaxAge(0); response.addCookie(c);
posted on 2020-07-20 14:46 SmilingEye 阅读(2052) 评论(0) 编辑 收藏 举报