[英]using csurf with session in express
I'm writing a single page application with MEAN stack
, and using express-session
with redis
for session management. 我正在使用
MEAN stack
编写一个单页应用程序,并将带有redis
express-session
用于会话管理。
I want to use scrf token in my client's cookies. 我想在客户的cookie中使用scrf令牌。
the problem is when I add csurf
middleware, it set a session with name csrfSecret
in redis, but how can I send it in cookie to client? 问题是当我添加
csurf
中间件时,它在redis中设置了一个名为csrfSecret
的会话,但是如何将它以cookie的形式发送给客户端?
middlewares : 中间件:
app.use(csrf({}));
app.use(function(req, res, next) {
res.cookie('csrf-token', req.csrfToken());
return next();
});
and csrf-token
is sending to client but it don't do anything.and I receive 403 error from module. 并且
csrf-token
正在发送到客户端,但是它什么也没做。我从模块收到403错误。
thank you for any answer or idea. 感谢您的任何回答或想法。
If you want to create a csrf cookie in the client you have to use the following: 如果要在客户端中创建csrf cookie,则必须使用以下命令:
app.use(csrf({ cookie: true })
This will create a token in the client. 这将在客户端中创建一个令牌。 If you do not pass any options to the csrf function it will use
req.session
. 如果您没有将任何选项传递给csrf函数,它将使用
req.session
。 If you want to save the cookie client-side, remember that you will need to use cookie-parser
module. 如果要保存cookie客户端,请记住,您将需要使用
cookie-parser
模块。
You can find more information in the github link to the project: https://github.com/expressjs/csurf 您可以在项目的github链接中找到更多信息: https : //github.com/expressjs/csurf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.