凭据:'include' 不包括 Cookie header

Credentials: 'include' not including Cookie header

我在客户端上使用 credentials: 'include'mode: 'cors'。在服务器上我看到 access-control-allow-credentials: trueaccess-control-allow-origin: https://dev.com:9443 headers。不过,我没有看到我的 cookie header,而且我似乎无法找到它不发送的原因。让我知道是否可以提供更多详细信息。

获取请求

fetch(url, {
  method: 'get',
  credentials: 'include',
  mode: 'cors',
  headers: {
    'Content-Type': 'application/json'
  }
});

选项请求Headers

:authority: prod.fakedomain.com
:method: OPTIONS
:path: /Search
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,fr;q=0.8,la;q=0.7
access-control-request-headers: content-type
access-control-request-method: GET
cache-control: no-cache
dnt: 1
origin: https://dev.com:9443
pragma: no-cache
referer: https://dev.com:9443/
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36

选项响应Headers

access-control-allow-credentials: true
access-control-allow-headers: content-type
access-control-allow-methods: GET,HEAD,POST
access-control-allow-origin: https://dev.com:9443
access-control-max-age: 1800
allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
content-length: 0
date: Mon, 04 Feb 2019 03:45:06 GMT
status: 200
vary: Origin
x-application-context: application:8080

获取请求Headers

:authority: prod.fakedomain.com
:method: GET
:path: /Search
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,fr;q=0.8,la;q=0.7
cache-control: no-cache
content-type: application/json
dnt: 1
origin: https://dev.com:9443
pragma: no-cache
referer: https://dev.com:9443/
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36

获取响应Headers

access-control-allow-credentials: true
access-control-allow-origin: https://dev.com:9443
content-encoding: gzip
content-type: application/json;charset=UTF-8
date: Mon, 04 Feb 2019 03:45:07 GMT
status: 200
vary: Origin,Accept-Encoding
x-application-context: application:8080

正如 sideshowbarker 在他的评论中提到的那样,浏览器没有为域 prod.fakedomain.com 设置 cookie,看起来服务器也没有设置 cookie。因此,如果您为 dev.com 设置了 cookie,但它们不是 httpOnly,那么您可以尝试将它们复制到 prod.fakedomain.com(通过 JS 读写)。

如果您想在 cookie 中存储敏感数据(例如 JWT 令牌),请阅读 XSS/XST 攻击并考虑使用 HttpOnly 标志的可能性。

我可以通过进入我的 Safari 隐私设置并取消选中 Prevent cross-site tracking

来解决这个问题

我终于发现问题只是浏览器不允许两个不同的域共享任何 cookie(相同的二级域除外),这超出了 credentials: include 可以做的,经过 2 小时的尝试......我找出 credentials: include 只是意味着您可以发送 cookie,但这并不意味着您可以使用两个不同的域发送 cookie..

无论你做什么,改变浏览器设置、设置 cookie 的 SameSite 任何方式,就是不能那样做...

以上就是我的总结,希望你能明白...

试了几天得出结论:

cedentials: 'include'

xhrFields: { withCredentials: true}

仅适用于具有不同端口的相同域,如果我们想向另一个域发出请求,我们必须手动向请求添加凭据(令牌等)header