egg.js中跨域请求RESTfull api接口提示 missing csrf token

发布于 5 年前 作者 phonegap100 4659 次浏览 来自 分享

egg.js中跨域请求RESTfull api接口提示 missing csrf token,vue请求post请求egg api接口数据提示 missing csrf token。如下图

1.png

1 回复

如果是前后的不分离的话,按照官方给的例子,服务器渲染的时候把csrf传到页面上,然后ajax请求携带csrf。

如果是跨域的话关闭csrf验证。

或者忽略api接口安全验证

exports.security = {
    csrf: {
      // 判断是否需要 ignore 的方法,请求上下文 context 作为第一个参数
      ignore: ctx => {
        if (ctx.request.url == 'ctx.request.url == '/alipay/alipayNotify' || ctx.request.url == '/weixinpay/weixinpayNotify') {
          return true;
        }else if(ctx.request.url.indexOf('/api')!=-1){
           return true;
        }else{
          return false;
        }
      }
    },
    domainWhiteList: [ 'http://localhost:8080' ]
  };
回到顶部