首页 > 解决方案 > 如何在 Spring Boot 中将 SAMESite cookie 设置为 none?

问题描述

我在两 (2) 个不同的服务器上部署了带有 reactjs(frontEnd) 和 springboot(Backend) 的 Web 应用程序。

它们都是http 服务器而不是 HTTPS

我搜索了所有与此相关的 stackoverflow 问题,但似乎没有一个有效。

没有使用spring-session依赖或Spring-security依赖。

为了维护用户会话,我只是让我的豆子@SessionScoped,如果一切都在同一台服务器上,它工作正常,但在不同的服务器上它已经坏了

浏览器给出了这个错误,因为can't set samesite=none, over http for this https is required

那么这个问题的解决方法是什么。因为我现在不能让我的服务器成为 https 。

标签: javaspring-bootsessionsamesite

解决方案


我使用 ResponseCookie 解决了这个问题。见下文:

final ResponseCookie responseCookie = ResponseCookie
                .from("test", "test")
                .secure(true)
                .httpOnly(true)
                .path("/")
                .maxAge(12345)
                .sameSite("None")
                .build();
        response.addHeader(HttpHeaders.SET_COOKIE, responseCookie.toString());

Cookie 名称:test 和值:test。

需要设置安全 true 才能使用 sameSite("None")。

这应该可以保存您的 Cookie


推荐阅读