Cookie由服务端来写并将httpOnly设置成为“true”,Cookie中设置了"HttpOnly"属性,那么通过程序(JS脚本、Applet等)将无法读取到Cookie信息,这样能有效的防止XSS攻击。看图加深理解。
此处省略账号密码校验逻辑,省略用户信息存缓存步骤。
1 @RequestMapping("/login") 2 @ResponseBody 3 public void login(HttpServletRequest request, HttpServletResponse response) throws IOException { 4 Cookie cookie = new Cookie("access_token", UUID.randomUUID().toString()); 5 cookie.setHttpOnly(true); 6 cookie.setPath("/"); 7 cookie.setDomain("localhost"); 8 response.addCookie(cookie); 9 response.sendRedirect("http://localhost:8088/index.html"); 10 }
原文:https://www.cnblogs.com/mao2080/p/9520185.html