如何解决均值堆栈中的CSRF令牌
我无法将Express的CSRF令牌与Angular的XSRF TOKEN集成。我正在使用给定的教程 https://jasonwatmore.com/post/2020/09/08/nodejs-mysql-boilerplate-api-with-email-sign-up-verification-authentication-forgot-password。
这是我的快递代码
server.js(main)
require('rootpath')();
const express = require('express');
const app = express();
const helmet = require("helmet");
const bodyParser = require('body-parser');
var cookieParser = require('cookie-parser')
var csrf = require('csurf')
const cors = require('cors');
const errorHandler = require('_middleware/error-handler');
app.use(cookieParser());
app.use(csrf({ cookie: true }))
app.use(helmet({ dnsprefetchControl: { allow: true }}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.all('*',function (req,res) {
res.cookie('XSRF-TOKEN',req.csrftoken())
})
app.use(cors({ origin: (origin,callback) => callback(null,true),credentials: true }));
// api routes
app.use('/accounts',require('./accounts/accounts.controller'));
// swagger docs route
app.use('/api-docs',require('_helpers/swagger'));
// global error handler
app.use(errorHandler);
// start server
const port = process.env.NODE_ENV === 'production' ? (process.env.PORT || 80) : 4000;
app.listen(port,() => console.log('Server listening on port ' + port));
在Angular中,我在module.ts中添加了XSRF TOKEN
imports: [
browserModule,ReactiveFormsModule,HttpClientModule,HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-TOKEN',headerName: 'X-XSRF-TOKEN'
}),AppRoutingModule
],
请帮助我解决问题
解决方法
我使用了其他方法解决了我的问题。 链接在这里 https://www.npmjs.com/package/express-csrf-double-submit-cookie
const cookieParser = require('cookie-parser')
const csrfDSC = require('express-csrf-double-submit-cookie')
const express = require('express')
// create middleware
const csrfProtection = csrfDSC();
const app = express();
app.use(cookieParser());
// middleware to set cookie token
app.use(csrfProtection)
// protect /api
app.post('/api',csrfProtection.validate,function (req,res) {
res.status(200).end();
})
以前我在用 https://www.npmjs.com/package/csurf 用于我的单页应用程序
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。