【发布时间】:2018-06-21 10:38:16
【问题描述】:
我一直在尝试诊断一个问题,即使用 HTTP Basic Auth 的 GET 请求在 Node 中成功,但在浏览器中失败。该问题直接表现为 CORS 故障(401 页面上没有 Access-Control-Allow-Origin)。
request.js:119 OPTIONS http://HOST?PARAMS 401 (Unauthorized)
ClientRequest._onFinish @ request.js:119
(anonymous) @ request.js:61
...
17:53:59.170 localhost/:1 Failed to load http://HOST?PARAMS: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9966' is therefore not allowed access. The response had HTTP status code 401. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
请求如下所示:
const request = require("request-promise");
const options = {"url":"http://...?","auth":{"user":"...","pass":"..."},"json":true,"qs":{...},"headers":{},"resolveWithFullResponse":true}
request.get(options).then(...);
此请求在 Chrome 中的显示方式:
让我惊讶的是:
- 方法是 OPTIONS,而不是 GET。 (我没有明确要求。)
- 我的身份验证凭据不包含在标题中(即使我没有设置
sendImmediately: false - “显示临时标题”警告。
我想知道的:
- 这是预期的行为吗?
- 如果不是,怎么回事?
- 如果是,出了什么问题? :)
在命令行上运行等效的请求似乎工作正常:
curl -I 'http://MYUSERNAME:MYPASSWORD@SAMEURL?SAME=PARAMETERS' -H 'Origin: http://localhost:4466' -X OPTIONS
HTTP/1.1 200 OK
Date: Wed, 20 Jun 2018 07:29:28 GMT
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://localhost:4466
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Access-Control-Allow-Origin,Access-Control-Allow-Credentials
Vary: Origin
X-Frame-Options: SAMEORIGIN
Allow: GET,HEAD,POST,OPTIONS
Content-Length: 0
但是我注意到,如果没有 URL 中提供的凭据,响应中就没有 CORS 标头:
HTTP/1.1 401 Unauthorized
Date: Wed, 20 Jun 2018 07:45:26 GMT
Server: Apache/2.4.29 (Unix) OpenSSL/1.0.2l
WWW-Authenticate: Basic realm="Authentication Required"
Content-Length: 381
Content-Type: text/html; charset=iso-8859-1
这是正确的行为吗?
假设
我怀疑:
- 浏览器正在发送 OPTIONS “飞行前”请求,因为它必须发送,因为该请求不是“简单请求”。 (不完全清楚这是请求库还是浏览器本身这样做)
- 服务器响应 401,因为未提供凭据,但未添加应有的 CORS 标头。
- 如果没有 CORS 标头,浏览器会阻止请求库访问结果,因此无法完成身份验证。
我不确定:
- 服务器是否配置错误?
- 请求是否未能在 OPTIONS 预检请求中传递凭据?
【问题讨论】:
-
服务器配置错误。它没有正确启用 CORS。要正确启用 CORS,它必须使用 200 或 204 状态代码响应未经身份验证的 OPTIONS 请求。请求不是“未能”在 OPTIONS 预检请求中传递凭据。 Request 甚至没有发出 OPTIONS 请求。相反,浏览器引擎是独立的。 CORS 规范要求浏览器从 OPTIONS 请求中省略所有凭据。因此,这种行为是有意的,设计使然。详细解释见答案stackoverflow.com/a/45406085/441757
-
谢谢,太棒了。你想把它作为一个答案让我接受吗?
标签: javascript cors xmlhttprequest node-request request-promise