我无法将 SameSite cookie 值设置为 None。
以下是我生成 ResponseCookie 对象的方式。
ResponseCookie cookie = ResponseCookie.from("Hb", cookieUserId)
.maxAge(!isEmpty(cookieUserId) ? MAX_COOKIE_DURATION : 0)
.domain("test.com")
.sameSite("None")
.secure(true)
.path("/")
.build();
response.addCookie(cookie)
向端点发出 Curl 请求
curl -X POST "localhost:8080/v1/user/v" --data "{}" -v -H 'Content-Type: application/json'
回应:
< set-cookie: Hb=00b7be31-fc6d-4891-a07c-46b5ef2b423c; Max-Age=7776000; Expires=Fri, 8 Nov 2019 17:23:52 GMT; Path=/; Domain=test.com; Secure
如您所见,Cookie 中缺少 SameSite 属性。
Spring Boot(版本:2.1.3.RELEASE)依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
最佳答案
我认为问题在于底层 javax.servlet.http.Cookie
不支持 SameSite
属性,更不用说新的 None
值了。
相反,您可以直接将其设置为 header ,假设您的响应是 javax.servlet.http.HttpServletResponse
的实例:
ResponseCookie cookie = ResponseCookie.from("Hb", cookieUserId)
.maxAge(!isEmpty(cookieUserId) ? MAX_COOKIE_DURATION : 0)
.domain("test.com")
.sameSite("None")
.secure(true)
.path("/")
.build();
response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString());
关于java - Spring:无法将 SameSite cookie 设置为 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57444334/