egg icon indicating copy to clipboard operation
egg copied to clipboard

eggjs app.post发送数据,一直报{"message":"invalid csrf token"}

Open itsky365 opened this issue 9 years ago • 7 comments

本地post方法发送数据至接口,一直报错

Request URL:http://localhost:2017/pubshow/login Request Method:POST Status Code:403 Forbidden Remote Address:127.0.0.1:2017

Response Headers Connection:keep-alive Content-Length:32 Content-Type:application/json; charset=utf-8 Date:Wed, 15 Feb 2017 07:23:35 GMT Server:openresty/1.11.2.2

Request Headers Accept:application/json, text/plain, / Accept-Encoding:gzip, deflate, br Accept-Language:zh-CN,zh;q=0.8,en;q=0.6 Cache-Control:no-cache Connection:keep-alive Content-Length:24 Content-Type:application/x-www-form-urlencoded Cookie:Webstorm-a322739f=47fb28cf-0926-4520-8589-0680ece843f0; _ga=GA1.1.1518623303.1484269654; csrfToken=7cNOeRMXI2oOCt_Y3jGROInO Host:localhost:2017 Origin:http://localhost:2017 Pragma:no-cache Referer:http://localhost:2017/pubshow/login?suitcode=superadmin_v1 User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

Form Data username:bb password:bbb

// config/config.default.js
exports.security = {
  csrf: false,
  ctoken: false,
};

或者

// config/config.default.js
exports.security = {
    csrf: {
        enable: false,
    },
    xframe: {
        enable: false,
    },
    hsts: {
        enable: false,
    },
    methodnoallow: {
        enable: false,
    },
    noopen: {
        enable: false,
    },
    nosniff: {
        enable: false,
    },
    xssProtection: {
        enable: false,
    },
    csp: {
        enable: false,
    },
};
// app/router.js
app.post('/pubshow/login', 'pub.login');
// app/controller/pub.js
exports.login = function* () {
    const ctx = this;
    console.log(ctx);
    console.log(ctx.body);
    ctx.body = `pub.show`;
};

接口返回结果一直是

{"message":"invalid csrf token"}

itsky365 avatar Feb 15 '17 07:02 itsky365

所有的 post 请求都会经过 csrf 防范的校验,如果返回了 invalid csrf token,看看配置 csrf 校验的配置是否已经关闭了。

dead-horse avatar Feb 15 '17 07:02 dead-horse

这个也加到 faq 吧,@shaoshuai0102,周经题

atian25 avatar Feb 15 '17 08:02 atian25

然后 message 里面加个 see http://faq

atian25 avatar Feb 15 '17 08:02 atian25

message 里面不加这些了,这是给外部用户看的,可以考虑在开发环境的日志里面打印一下。

dead-horse avatar Feb 15 '17 08:02 dead-horse

我搞一下

shaoshuai0102 avatar Feb 15 '17 08:02 shaoshuai0102

一颗臭鸡蛋

mickeyzhou avatar May 31 '18 07:05 mickeyzhou

先补习下什么是 csrf 再看下 egg–security

mickeyzhou [email protected] 于 2018年5月31日周四 下午3:17写道:

一颗臭鸡蛋

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eggjs/egg/issues/387#issuecomment-393433081, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHhMlLDrbBPKavT49HomLP-to87qP8bks5t35jygaJpZM4MBYvQ .

sunfuze avatar Jun 02 '18 02:06 sunfuze