Spring:无法将 SameSite cookie 设置为无

Ahm*_*ood 8 java cookies spring spring-boot samesite

我无法将 SameSite cookie 值设置为无。

以下是我如何生成 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)
Run Code Online (Sandbox Code Playgroud)

对端点的卷曲请求

curl -X POST "localhost:8080/v1/user/v" --data "{}" -v -H 'Content-Type: application/json'
Run Code Online (Sandbox Code Playgroud)

回复:

< 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
Run Code Online (Sandbox Code Playgroud)

如您所见,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>
Run Code Online (Sandbox Code Playgroud)

row*_*n_m 7

我认为问题在于底层javax.servlet.http.Cookie不支持该SameSite属性,更不用说新None值了。

相反,您可以将其直接设置为标头,假设您的响应是一个实例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());
Run Code Online (Sandbox Code Playgroud)