[英]How to set XSRF-TOKEN cookie in koa2 app?
I'm write node-koa2-angular app. 我正在编写node-koa2-angular应用程序。 And I need set XSRF-TOKEN cookie like in express app:
我需要像在快速应用中那样设置XSRF-TOKEN Cookie:
var csrfProtection = csrf({ cookie: true })
But in koa-csrf I cannot find this option and by default it's don't create cookie. 但是在koa-csrf中,我找不到此选项,默认情况下,它不创建cookie。 Thanks for your help.
谢谢你的帮助。
You're correct that koa-csrf does not create the cookie. 您是正确的, koa-csrf不会创建cookie。 Instead it introspects the cookie on the koa context at
ctx.session
. 相反,它会在
ctx.session
上对koa上下文中的cookie进行内部ctx.session
。 Check out where this happens in the code . 在代码中查看发生这种情况的位置。
You'll need to add an additional middleware like koa-session to create the cookie. 您需要添加其他中间件(例如koa-session)来创建cookie。 Your implementation should look something like:
您的实现应类似于:
const session = require('koa-session');
const CSRF = require('koa-csrf');
// set the session keys and add session support
app.keys = ['secret']
app.use(session({}, app))
// add the CSRF middleware
app.use(new CSRF());
// your middleware here (e.g. parse a form submit)
app.use((ctx, next) => {
if (ctx.method === 'GET') {
ctx.state.csrf = ctx.csrf;
}
return next();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.