【问题标题】:Why does Chrome send user credentials with preflighed CORS request?为什么 Chrome 会发送带有预检 CORS 请求的用户凭据?
【发布时间】:2012-04-07 11:53:51
【问题描述】:

w3.org CORS specification 明确指出在使用 CORS 发出预请求请求时应省略用户凭据:

排除用户凭据。

为什么谷歌浏览器忽略了这一点?

看看下面的代码。

var url = 'http://example.net/resources/test.php';
var xhr = new XMLHttpRequest();
xhr.open('PUT', url, true);
xhr.onreadystatechange = handler;
xhr.withCredentials = true;
xhr.send();

这是来自上述场景的请求标头。

OPTIONS /resources/test.php HTTP/1.1
Host: example.net
Connection: keep-alive
Access-Control-Request-Method: PUT
Origin: http://elsewhere.example.net
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.10 Chromium/17.0.963.79 Chrome/17.0.963.79 Safari/535.11
Access-Control-Request-Headers: Origin
Accept: */*
Referer: http://elsewhere.example.net/cors-example.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: somecookies=here

如您所见,cookie(用户凭据)发送的,尽管有 CORS 规范。这背后有什么原因吗?

【问题讨论】:

标签: google-chrome credentials cors


【解决方案1】:

听起来像一个错误。您可以向 Google 提交错误报告:new.crbug.com

【讨论】:

【解决方案2】:

是的,这是有原因的。您明确告诉 XHR,它应该通过设置属性来包含凭据:

xhr.withCredentials = true;

删除该行,Chrome 应该会在没有 cookie 的情况下发出请求。

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2018-10-31
    • 2016-08-05
    • 2012-11-08
    • 1970-01-01
    • 2016-12-09
    • 2022-07-18
    • 1970-01-01
    相关资源
    最近更新 更多