我想在多个域上共享用户同意状态。我试图通过发送多个fetch请求来完成这个任务,这些请求包含作为body的值。每个处理程序都应该简单地接受请求,并将主体存储为cookie-value。
客户端:
fetch(data.handler, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'include',
headers: {
'Content-Type': 'text/plain', // if we use application/json, a OPTIONS request (preflight) will be sent, that causes problems
},
redirect: 'follow',
body: JSON.stringify(cookieValue),
}).then(response => {
if (response.ok) {
resolve();
} else {
reject();
}
})在服务器端,我用Set-Cookie头发送响应。
这是我对CORS域的响应:
curl 'http://localhost-domain2:8000/handler' -v -X POST \
-H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0' \
-H 'Accept: */*' \
-H 'Accept-Language: de,en;q=0.7,en-US;q=0.3' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Referer: http://localhost-domain1:8000/' \
-H 'Content-Type: text/plain' \
-H 'Origin: http://localhost-domain:8000' \
-H 'Connection: keep-alive' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
--data-raw '{"my":"values"}'
* Trying 127.0.0.1:8000...
* TCP_NODELAY set
* Connected to localhost-domain2 (127.0.0.1) port 8000 (#0)
> POST /handler HTTP/1.1
> Host: localhost-domain2:8000
> User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0
> Accept: */*
> Accept-Language: de,en;q=0.7,en-US;q=0.3
> Accept-Encoding: gzip, deflate
> Referer: http://localhost-domain1:8000/
> Content-Type: text/plain
> Origin: http://localhost-domain1:8000
> Connection: keep-alive
> Pragma: no-cache
> Cache-Control: no-cache
> Content-Length: 145
>
* upload completely sent off: 145 out of 145 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.17.8
< Date: Tue, 02 Aug 2022 10:14:49 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Set-Cookie: gdpr=%7B%22my%22%3A%22values%22%7D; expires=Wed, 02 Aug 2023 10:14:49 GMT; path=/; samesite=lax
< Access-Control-Allow-Origin: http://localhost-domain1:8000
< Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: User-Agent,Keep-Alive,Content-Type,Set-Cookie
< Content-Encoding: gzip我掩盖了一些事情,如json或域名,所以有些内容长度可能是错误的,但这是实际的反应。
请求被发送,服务器做它应该做的事情,但是Set没有被处理,不管是由chrome,还是firefox。如果我访问localhost-domain2:8000,不存在cookie。
那么,需要哪些提取参数、HTTP头和cookie参数组合才能使客户端存储另一个域的cookie?
编辑:只是为了澄清:我知道我不能直接分享曲奇,我不想分享,我只是想让曲奇存在于另一个领域,就像一个单点登录。
更新
将所有内容更改为ssl并对sameSite属性使用"None“不会改变任何事情。严格的使用也是行不通的。
更新2
从一个新窗口中的dev-tools打开到第二个域的网络请求将导致cookie被正确设置,因此它确实是我遇到问题的获取api。
发布于 2022-08-03 08:24:55
更新
好吧,我很好奇--如果我做的其他事情都和下面所描述的完全一样的话,它也适用于fetch。
Cookie的
fetch(url, {
mode: "cors",
credentials: "include",
method: "get",
cache: "no-cache",
redirect: "follow",
})其余部分(服务器端,标头)如下所述。
带饼干的火狐-保护仍然不起作用.
原始答案
以下配置工作正常:
javascript示例:
const secure = window.location.protocol.indexOf('https') > -1;
const sameSite = secure ? 'none' : 'lax';
// so in vanillaJs
document.cookie = 'key=value;' + (secure ? '; secure' : '') + '; sameSite=' + sameSite;上相同
Access-Control-Allow-Origin: ${HTTP_ORIGIN:-localhost-domain1}
Access-Control-Allow-Methods: GET
Access-Control-Allow-Credentials: true
Cache-Control: no-cache, must-revalidate, no-store
Content-Type: text/html
pragma: no-cache
expires: 0和一个空的html5文档以避免控制台错误。
我不知道到底是什么引起了这个问题,但这是目前它是如何运作的。
火狐
如果启用Cookie保护,这在最新的Firefox版本中是行不通的!它没有提示我允许使用cookie,它只是阻止了它。
编辑:而且它似乎只通过https起作用。
https://stackoverflow.com/questions/73205960
复制相似问题