Cookie除了key和value以外有几个属性。
httpOnly是否允许js读取cookiesecure是否仅仅在https的链接下,才提交cookiedomaincookie提交的域pathcookie提交的pathmaxAgecookie存活时间sameSite同站策略,枚举值:StrictLaxNone
其他的都很熟悉了,最后一个是 Chrome 51 开始,浏览器的 Cookie 新增加了一个 SameSite 属性,用来防止 CSRF 攻击和用户追踪。
关于
SameSite的详细解释 可以看 Cookie 的 SameSite 属性
在Javaweb应用中 ,设置 Cookie一般都是用 javax.servlet.http.Cookie,但是SameSite属性出来不久,Servlet库还没更新,所以没有设置SameSite的方法.
javax.servlet.http.Cookie 中定义的的属性
可以看到,还没有SameSite的定义
1// 2// The value of the cookie itself. 3// 4 5private String name; // NAME= ... "$Name" style is reserved 6private String value; // value of NAME 7 8// 9// Attributes encoded in the header's cookie fields. 10// 11 12private String comment; // ;Comment=VALUE ... describes cookie's use 13// ;Discard ... implied by maxAge < 0 14private String domain; // ;Domain=VALUE ... domain that sees cookie 15private int maxAge = -1; // ;Max-Age=VALUE ... cookies auto-expire 16private String path; // ;Path=VALUE ... URLs that see the cookie 17private boolean secure; // ;Secure ... e.g. use SSL 18private int version = 0; // ;Version=1 ... means RFC 2109++ style 19private boolean isHttpOnly = false;
通过 ResponseCookie 给客户端设置Cookie
本质上,Cookie也只是一个header。我们可以不使用Cookie对象,而通过自定义Header的方式来给客户端设置Cookie。
ResponseCookie 是Spring定义的一个Cookie构建工具类,极其简单
1import java.time.Duration; 2 3import javax.servlet.http.HttpServletRequest; 4import javax.servlet.http.HttpServletResponse; 5 6import org.springframework.http.HttpHeaders; 7import org.springframework.http.ResponseCookie; 8import org.springframework.web.bind.annotation.GetMapping; 9import org.springframework.web.bind.annotation.RequestMapping; 10import org.springframework.web.bind.annotation.RestController; 11 12@RestController 13@RequestMapping 14public class TestController { 15 16 @GetMapping("/test") 17 public Object test (HttpServletRequest request, 18 HttpServletResponse response) throws Exception { 19 20 ResponseCookie cookie = ResponseCookie.from("myCookie", "myCookieValue") // key & value 21 .httpOnly(true) // 禁止js读取 22 .secure(false) // 在http下也传输 23 .domain("localhost")// 域名 24 .path("/") // path 25 .maxAge(Duration.ofHours(1)) // 1个小时候过期 26 .sameSite("Lax") // 大多数情况也是不发送第三方 Cookie,但是导航到目标网址的 Get 请求除外 27 .build() 28 ; 29 30 // 设置Cookie Header 31 response.setHeader(HttpHeaders.SET_COOKIE, cookie.toString()); 32 33 return "ok"; 34 } 35} 36
响应给客户端的Cookie

所有属性都响应正确 √
HttpSession Cookie 的SameSite属性
HttpSession依赖一个名称叫做JSESSIONID(默认名称)的Cookie。
对于JSESSIONID Cookie 的设置,可以修改如下配置。但是,目前spring也没实现SameSite的配置项。
配置类 : org.springframework.boot.web.servlet.server.Cookie
1server.servlet.session.cookie.comment 2server.servlet.session.cookie.domain 3server.servlet.session.cookie.http-only 4server.servlet.session.cookie.max-age 5server.servlet.session.cookie.name 6server.servlet.session.cookie.path 7server.servlet.session.cookie.secure
通过修改容器的配置,对Session Cookie设置SameSite属性
Tomcat
1import org.apache.tomcat.util.http.Rfc6265CookieProcessor; 2import org.apache.tomcat.util.http.SameSiteCookies; 3import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6 7@Configuration 8public class TomcatConfiguration { 9 10 @Bean 11 public TomcatContextCustomizer sameSiteCookiesConfig() { 12 return context -> { 13 final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor(); 14 // 设置Cookie的SameSite 15 cookieProcessor.setSameSiteCookies(SameSiteCookies.LAX.getValue()); 16 context.setCookieProcessor(cookieProcessor); 17 }; 18 } 19}
Spring Session的SameSite属性
通过自定义 CookieSerializer 设置 SameSite属性
1import org.springframework.context.annotation.Bean; 2import org.springframework.context.annotation.Configuration; 3import org.springframework.session.web.http.CookieSerializer; 4 5import com.video.common.spring.session.DynamicCookieMaxAgeCookieSerializer; 6 7@Configuration 8public class SpringSessionConfiguration { 9 10 @Bean 11 public CookieSerializer cookieSerializer() { 12 DynamicCookieMaxAgeCookieSerializer serializer = new DynamicCookieMaxAgeCookieSerializer(); 13 serializer.setCookieName("JSESSIONID"); 14 serializer.setDomainName("localhost"); 15 serializer.setCookiePath("/"); 16 serializer.setCookieMaxAge(3600); 17 serializer.setSameSite("Lax"); // 设置SameSite属性 18 serializer.setUseHttpOnlyCookie(true); 19 serializer.setUseSecureCookie(false); 20 return serializer; 21 } 22} 23