auth.js:84 跨域读取阻塞 (CORB) 阻止了跨域响应http://myserver/auth与 MIME 类型 application/json。这也不适用于 Firefox,尽管 Firefox 错误消息更通用。奇怪的是,Firefox 的网络面板显示我想要的响应实际上已交付,浏览器只是不接受将其传递给我的 JavaScript 代码的响应。这是我的krakend.json文件中的 CORS 设置: "github_com/devopsfaith/krakend-cors": { "allow_origins": ["http://localhost:61552"], "allow_headers": ["Origin", "Authorization", "Content-Type", "Accept", "X-Auth-Token", "Referer", "User-Agent"], "expose_headers": ["Content-Type", "Content-Length"], "allow_credentials": true, "allow_methods": ["GET", "HEAD", "POST", "OPTIONS"] }这是被调用的特定端点: "endpoints": [{ "endpoint": "/auth", "method": "POST", "output_encoding": "no-op", "extra_config": { "github.com/devopsfaith/krakend-ratelimit/juju/router": { "maxRate": 20, "clientMaxRate": 8, "strategy": "ip" } }, "backend": [{ "url_pattern": "/connect/token", "encoding": "no-op", "sd": "dns", "host": ["identity-server.service.consul"], "disable_host_sanitize": true }] },我的 JavaScript 请求如下所示: xhr.open('POST', url); xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.withCredentials = true; xhr.onreadystatechange = function () { ... } xhr.send(...我想尝试将响应的内容类型更改为text/plain以防万一,但我目前无法访问生成该响应的代码,而且我不知道这是否会有所帮助。当我从 node.js 服务器或 Postman 之类的应用程序发出相同的请求时,一切都会正确返回,并且我想看到的标头应该足以让 CORS 感到高兴 ( access-control-allow-credentials: true, access-control-allow-origin: *)
1 回答
![?](http://img1.sycdn.imooc.com/54585050000156a302200220-100-100.jpg)
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
我终于找到了答案。
第一个问题是,当使用 时xhr.withCredentials = true
,获得 的响应头还不够好access-control-allow-origin: *
。响应头必须包含请求本身的原始来源,例如access-control-allow-origin: https://example.com
.
第二个问题是 kraken 使用的 CORS 模块处理通配符域的方式。如果您使用"allow_origins": []
或"allow_origins": ["*"]
,则服务器access-control-allow-origin: *
无论如何都会响应。
但是,我不想将所有可能想要使用此服务器的主机列入白名单。
幸运的是,有人能够指出 kraken 用于处理 CORS 的源代码(在https://github.com/rs/cors),事实证明,如果您使用除"*"
for之外的任何其他类型的通配符表达式一切都一样"http*"
,然后服务器回显原始主机,一切都很好!我的配置现在是:
"allow_origins": ["http*"]
注意:这样做可能很危险!如果您将敏感数据放入 cookie,同样的数据可以通过这种方式提供给任何其他网站。
添加回答
举报
0/150
提交
取消