java - Spring:无法将 SameSite cookie 设置为 None

标签 java spring spring-boot cookies samesite

我无法将 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/

相关文章:

spring - org.springframework.web.bind.annotation.RequestMapping 缺少什么依赖项?

postgresql - Flyway - Flyway 架构迁移失败

java - Lombok 不生成 RequiredArgsConstructor 和 AllArgsConstructor

java - ActiveMQ 和嵌入式代理

javascript - 我如何动态更新我的CSS,即我想给用户一个选择字体大小、背景颜色的选项

java - java序列化过程中可以缓冲对象吗?

java - 使用计算值覆盖测试中的 Spring 属性

mongodb - 使用 ssl 从 spring boot 应用程序连接到 MongoDB

java - Jsoup 白名单 : Parsing non-english character

java - 想要使用共享的 @BeforeClass/@AfterClass 和 Spring App Context 运行许多 jUnit 测试类