egg: 405 (Method Not Allowed)

我已经安装了 egg-cors插件,并在 config/plugin.js里禁用了cors,

security: {
    csrf: false,
    debug: 'csrf-disable',
    domainWhiteList: [ 'http://localhost:3000' ]
  }

同时设置了header,

fetch(API_URL + '/entities/' + entity + '?label=' + label + '&page=' + page, {headers: { 'Content-Type': 'application/json', 'x-csrf-token':  cookies.csrfToken}});

可是还是返回 405 (Method Not Allowed),我用postman 是可以调用这个API的,但是用代码就是不行。

请教一下我哪里缺了一环吗?

我只是禁用所有的csrf, cors等安全特性,该如何做? 这个安全方面的东西太影响开发效率了

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 21 (7 by maintainers)

Most upvoted comments

我建议先搞清楚 “csrf” 和 “cors” 这两个概念,405是正常的,因为你的cors插件没生效。。看起来你是想发起一个跨域请求,但是被egg拦截了。

本身egg内部是不允许随便发跨域请求的,你必须开启 cors插件,,正确的做法是

1.重新开启你的cors插件 2.配置安全白名单

    security: {
        domainWhiteList: [ 'http://localhost:7001' ] // 这个端口一定要跟你实际的一致
      }

类似的issue也可以搜索下,比如 https://github.com/eggjs/egg/issues/725 . https://github.com/eggjs/egg/issues?utf8=✓&q=跨域

@jtyjty99999 请问 security 插件里面的 methodnoallow 这个中间件为什么把 options 的请求拒绝哦.

image

代码地址: https://github.com/eggjs/egg-security/blob/master/lib/middlewares/methodnoallow.js#L7