tomcat容器设置谷歌浏览器cookie
首先cookie需要加上httponly、Secure、Samesit等属性
@GetMapping("/setCokie")
public String setCokies(HttpServletResponse response){
Cookie cookie1 = new Cookie("userName1","zhangsan1");
cookie1.setMaxAge(360000);
cookie1.setHttpOnly(true);
cookie1.setSecure(true);
Cookie cookie2 = new Cookie("userName2","zhangsan2");
cookie2.setMaxAge(360000);
cookie2.setHttpOnly(true);
cookie2.setSecure(true);
Cookie cookie3 = new Cookie("userName3","zhangsan3");
cookie3.setMaxAge(360000);
cookie3.setHttpOnly(true);
cookie3.setSecure(true);
response.addCookie(cookie1);
response.addCookie(cookie2);
response.addCookie(cookie3);
return "server is running...";
}
servlet中无法设置Samesite属性,但是tomcat中可以
@Bean
public TomcatContextCustomizer sameSiteCookiesConfig() {
return context -> {
final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor();
// 设置Cookie的SameSite
cookieProcessor.setSameSiteCookies(SameSiteCookies.NONE.getValue());
context.setCookieProcessor(cookieProcessor);
};
}

浙公网安备 33010602011771号