egg icon indicating copy to clipboard operation
egg copied to clipboard

eggjs cros 跨域问题

Open CNYeFeng opened this issue 8 years ago • 10 comments

  • Node Version:
  • Egg Version:
  • Plugin Name:
  • Plugin Version:
  • Platform:
  • Mini Showcase Repository:

egg本地开发。在另一个系统中调用egg服务的接口时出现了跨域问题。引入了 egg-cors 后,依然有跨域问题,报了下面的错误, 哪位仁兄遇到过? Access to fetch at 'http://127.0.0.1:7001/test?' from origin 'http://localhost:9088' 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'.

CNYeFeng avatar Nov 08 '18 04:11 CNYeFeng

exports.security = {
  domainWhiteList: [ 'http://localhost:4200' ],
};

Or:

// {app_root}/config/config.default.js
exports.cors = {
  // {string|Function} origin: '*',
  // {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};

If the origin is set, the plugin will follow it to set the Access-Control-Allow-Origin and ignore the security.domainWhiteList. Otherwise, the security.domainWhiteList which is default will take effect as described above.

  • https://github.com/eggjs/egg-cors

thonatos avatar Nov 08 '18 09:11 thonatos

config.cors = { credentials: true, origin: [ 'http://localhost:9088' ], allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', };

@thonatos 非常感谢你的回答。我之前也是像你发的那样设置的,还是会报跨域问题。 需要加上 credentials: true。 设置代码如上。domainWhiteList 可以不设置。

CNYeFeng avatar Nov 08 '18 12:11 CNYeFeng

看了好多 issues 和文档都没有给出比较完整和具体的跨域配置方案 我尝试了只需要配置下面的内容可以成功:

// config/config.default.js
module.exports = appInfo => {
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1546505251052_7827';

  // add your config here
  config.middleware = [];

  config.security = {
    domainWhiteList: [ 'http://localhost:8899' ], // 没有配置的话,错误信息:404
    csrf: {
      enable: false, // 暂时禁用掉 csrf,错误信息:403 missing csrf token
    },
  }

  return config;
};

安装 egg-cors

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

104gogo avatar Jan 03 '19 10:01 104gogo

@104gogo

/ {app_root}/config/config.default.js
exports.cors = {
  // 这里,支持函数
  // {string|Function} origin: '*',
  // {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};

atian25 avatar Jan 03 '19 10:01 atian25

嗯,光是跨域的话,我也觉得只需要配置 cors 就好了

104gogo avatar Jan 04 '19 02:01 104gogo

@104gogo 404

huchun666 avatar Apr 09 '19 13:04 huchun666

看了好多 issues 和文档都没有给出比较完整和具体的跨域配置方案 我尝试了只需要配置下面的内容可以成功:

// config/config.default.js
module.exports = appInfo => {
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1546505251052_7827';

  // add your config here
  config.middleware = [];

  config.security = {
    domainWhiteList: [ 'http://localhost:8899' ], // 没有配置的话,错误信息:404
    csrf: {
      enable: false, // 暂时禁用掉 csrf,错误信息:403 missing csrf token
    },
  }

  return config;
};

安装 egg-cors

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

就这个可用!!!👍

leeyw19837 avatar Sep 28 '20 03:09 leeyw19837

看了好多 issues 和文档都没有给出比较完整和具体的跨域配置方案 我尝试了只需要配置下面的内容可以成功:

// config/config.default.js
module.exports = appInfo => {
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1546505251052_7827';

  // add your config here
  config.middleware = [];

  config.security = {
    domainWhiteList: [ 'http://localhost:8899' ], // 没有配置的话,错误信息:404
    csrf: {
      enable: false, // 暂时禁用掉 csrf,错误信息:403 missing csrf token
    },
  }

  return config;
};

安装 egg-cors

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

建议官方文档改成这个 @atian25

Holybasil avatar Oct 23 '20 02:10 Holybasil

其实应该配置 origin 而不是 domainWhiteList

欢迎 PR

atian25 avatar Oct 23 '20 03:10 atian25

我的options预检请求就直接挂掉了,怎么解

zhaoqi-nb avatar Dec 31 '21 08:12 zhaoqi-nb