credentials: include 下 ACAO: * 为何被拒,Allow-Credentials 与精确来源为何必须成对出现,模拟器演示。
场景预设
请求 — 浏览器(页面)侧
Method
Content-Type
自定义头部
响应 — 服务器返回的头部
Access-Control-Allow-Origin
Access-Control-Allow-Methods
Access-Control-Allow-Headers
请求所需 + 常用头部 — 点击切换
浏览器处理步骤
页面发起 fetch 请求
简单请求(CORS-safelisted)— 直接发送,无预检
发送正式请求
GET /data HTTP/1.1 Origin: https://myapp.example.com Content-Type: text/plain Cookie: session=abc123
服务器正式响应到达
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{"status": "ok"}凭据请求不允许使用通配符 '*'
浏览器拦截响应并 reject fetch
浏览器控制台
✕ Access to fetch at 'https://api.example.com/data' from origin 'https://myapp.example.com' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
(index):1
修改任意值,下方步骤会立即重新计算。试试「credentials + *」场景看通配符为何被拦。
credentials: 'include' 会让 Cookie/认证跨域传输,浏览器因此严格得多,直接拒绝 ACAO: * —— 通配符等于把你的登录态响应开放给所有网站。
通过需要两个条件成套:服务器在 ACAO 回显精确来源,并返回 Allow-Credentials: true。缺任何一个都会以不同的理由被拦。
还牵扯 SameSite Cookie:跨站带 Cookie 需 SameSite=None; Secure,CORS 与 Cookie 设置要同时配对。把 '*' 换成「回显来源」看拦截如何解除。 CORS 模拟器