Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

跨域问题:Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. #2009

Closed
ithinco opened this issue Jan 23, 2018 · 12 comments

Comments

@ithinco
Copy link

ithinco commented Jan 23, 2018

  • Node Version: 9.3.0
  • Egg Version: ^2.0.0
  • Plugin Name: egg-cors
  • Plugin Version: ^2.0.0

环境类似于这个问题

一个 eggJS 容器(api)做 api 服务器,一个 eggJS 容器(admin)做 Vue 后台 SPA 的托管,用 traefik 做反向代理,api 和 admin 在同一个 overlay private 网络中。

现在的问题是,从 admin 发送 post 请求给 api,options 返回 200,post 请求不返回,报错『Failed to load http://api/api/v1/user/signIn: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://admin.localhost' is therefore not allowed access.』

api 的配置为:

// plugin.js
exports.cors = {
  enable: true,
  package: 'egg-cors'
}

exports.security = false
// config.default.js
config.cors = {
  origin: '*',
  allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'
}

试过很多次,都没成功,不知道问题出在哪?谢谢。

尝试的方法包括但不限于给 security 配置里加白名单 ['http://admin.localhost']

image

@hongjinquan
Copy link

如果不做安全检查的话,你可以在plugin中设置:
exports.security = false; // 关闭安全检查

@atian25
Copy link
Member

atian25 commented Jan 23, 2018

提供下 API server 的最小可复现代码库

@ithinco
Copy link
Author

ithinco commented Jan 23, 2018

@atian25 就是一个很简单的 egg 应用,和安全/cors 有关的代码都已经贴出,作为约定大于配置的框架,其他地方没有修改与安全/cors有关的代码,问题贴出来只是让你们帮看下,我已经换用 AWS lambda 了。

@atian25
Copy link
Member

atian25 commented Jan 24, 2018

我是怀疑你 config 没写对,提供最小可复现代码库才能排查

@atian25
Copy link
Member

atian25 commented Jan 24, 2018

因为你提供的步骤无法复现

@ithinco
Copy link
Author

ithinco commented Jan 24, 2018

@atian25 不用 docker swarm mode + traefik 只是单独在本地启动跨域没有问题的

@atian25
Copy link
Member

atian25 commented Jan 24, 2018

那说明不是 egg 的问题,你可以排查下 cors header 有没有正常输出,如果输出了,那 admin 那边有没有接受到,如果没有,那就查下中间的 proxy 是不是有问题咯。

我们本地无法复现,你也给不出可以复现的方式,所以也只能提供思路你自己查了。

@atian25 atian25 closed this as completed Jan 24, 2018
@ithinco
Copy link
Author

ithinco commented Jan 24, 2018

@atian25 嗯,谢谢。

@atian25
Copy link
Member

atian25 commented Jan 24, 2018

你可以试下在 egg docker 里面自己 curl ip 看看有没有 header,二分法定位下问题到底是 egg 还是中间 proxy

@atian25
Copy link
Member

atian25 commented Nov 9, 2018

@toknT if you are a qualified developer, you must know about CORS, it's a common web scenario.

@toknT
Copy link

toknT commented Nov 9, 2018

@toknT if you are a qualified developer, you must know about CORS, it's a common web scenario.
sorry I just mistake config the config file. but I realy don't like the style of configs.

@atian25
Copy link
Member

atian25 commented Nov 9, 2018

egg-cors is just a standalone plugin, so if you don't like it, you are free to write your own one, it's ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants