Safari 设置 cookie 不适用于第一方 cookie

Safari set-cookies not working for first party cookie

当我登录时,我return到浏览器:

Overview
URL: https://subdomain.domain.de:8444/api/auth/login
Status: 200
Source: Network
Adresse: xxx.xxx.x.xx:8444
Initiator: 
xhr.js:177


Request
POST /api/auth/login HTTP/1.1
Accept: application/json, text/plain, */*
Content-Type: application/json;charset=utf-8
Origin: https://subdomain.domain.de
Content-Length: 62
Accept-Language: de-de
Host: subdomain.domain.de:8444
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15
Referer: https://subdomain.domain.de/login
Accept-Encoding: gzip, deflate, br
Connection: keep-alive

Response
HTTP/1.1 200
Access-Control-Allow-Origin: https://subdomain.domain.de
Content-Type: application/json;charset=UTF-8
Pragma: no-cache
Set-Cookie: accessToken=FycxgaSUgHnBlzMqYn/qsBEm5YBcmX52/eYbm+daUHPP1Fa7edawdawdawO1EdJlz9nyP5FrlPYnh/b//SZJRDs0Am8sGF+UZ+XffvPra8awdawd9+RbHiN0WcL+9T4xLlueMxd5bNVRVKHqeTonSK02Ym0cLxfALOeHrmbdqLS95uNOlzFYbjOuGV7bhwLGk5bavNPv9IWKqNAILAbkkw+gdawdawduM+BXdGE7KFbUgxvGmDw==; Path=/; Domain=subdomain.domain.de; Max-Age=PT448343981H30M29S; Expires=Sat, 16 Apr 2072 22:57:46 GMT; Secure; HttpOnly;SameSite=Lax
Set-Cookie: refreshToken=FycxgaSUgHnBlzMqYn/qsBEm5YBawdawdadawdupnO1EdJlz9nyP5FrlPYnh/b//SZJRDs0Am8sGF+UZ+XffvPra84jWTk9+RbHiM1+aNElVA8jXewqlexh7tGKuawdawdv4pxzC/RsDoGS/Jc8Xkzg133dYMCr7mRHlkU7jijoJrPYUAayiewVIMPUh/IE8sGUqIMKbiGoqAJAawdawdawdawdawdaw03GS4XgbwFj76V2AAAw==; Path=/; Domain=subdomain.domain.de; Max-Age=PT450502981H30M31S; Expires=Fri, 15 Jul 2072 21:57:46 GMT; Secure; HttpOnly;SameSite=Lax
X-XSS-Protection: 1; mode=block
Expires: 0
Transfer-Encoding: Identity
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Date: Mon, 22 Feb 2021 22:58:53 GMT
Access-Control-Allow-Credentials: true
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
Strict-Transport-Security: max-age=31536000 ; includeSubDomains

Request data
MIME-Typ: application/json
Codierung: utf-8
Anfragedaten: 

我也看到了,响应中的cookie:

但是cookies并没有保存在浏览器中。这是我在 spring 后端创建的第一方 cookie。

在 Spring 引导中,我这样创建 cookie:

import org.springframework.http.HttpCookie;
import org.springframework.http.ResponseCookie;


@Component
public class CookieUtil {

    public HttpCookie createAccessTokenCookie(String token, Long duration) {
        return ResponseCookie.from("accessToken", token).maxAge(duration).httpOnly(true).path("/").build();
    }

    public HttpCookie createRefreshTokenCookie(String token, Long duration) {
        return ResponseCookie.from("refreshToken", token).maxAge(duration).httpOnly(true).path("/").build();
    }
}

在 Safari 中,您必须请求用户允许使用您的 cookie。

As of ITP 2.1, Safari uses its machine-learning magic to identify which first-party cookies can be used for tracking. Then, it blocks cookies unless you use the Storage Access API to ask users to allow the use of your cookie.

您可以在此处进一步阅读如何使用它 https://developer.mozilla.org/en-US/docs/Web/API/Document/requestStorageAccess

存在大量与 Safari 和 cookie 的使用相关的问题,如果您查找与该问题相关的信息,您会发现多个错误和解决方案,一个适用于某些情况,另一个适用于其他情况。

虽然 Lax 更可取(请参阅 this great article),但您可以尝试的一件事是将 cookie SameSite 属性设置为 None。请注意,此更改可能相关并会影响其他浏览器中的应用程序行为,尤其是 Chrome.

您可以尝试的另一件事是将 cookie 的域设置为 .domain.dedomain.de 之类的东西,以避免任何可能的子域相关问题。

最后请注意,在您的屏幕截图中,max age 的值似乎没有正确打印。可能不是,但对于您指出的相同版本的 Safari,可能有类似的问题,已在 中的 SO 上报告:OP 通过调整 max age cookie 属性的值来解决问题。请为该信息尝试不同的值,也许它有效。

根据您的意见,为了将来参考,在某种程度上,问题似乎实际上与 cookie max age 相关:从 cookie 中删除 max age 值似乎是该问题的临时解决方法。