Gio*_*Gpx 13 csrf node.js express
我正在使用Express框架编写一个带有Node.js的小型Web应用程序.我正在使用csrf中间件,但我想为某些请求禁用它.这是我在我的应用程序中包含它的方式:
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.cookieSession({secret: 'secret'}));
app.use(express.csrf());
Run Code Online (Sandbox Code Playgroud)
我想设置一个没有csrf控件的POST路由.
Pet*_*ons 30
有几种可能的方法.您基本上需要了解决定是否使用csrf中间件的最简单和最正确的规则.如果你想要csrf大部分时间,除了请求模式的小白名单,请按照这个答案中的例子我有条件记录中间件(为方便起见,下面复制).
var express = require("express");
var csrf = express.csrf();
var app = express.createServer();
var conditionalCSRF = function (req, res, next) {
//compute needCSRF here as appropriate based on req.path or whatever
if (needCSRF) {
csrf(req, res, next);
} else {
next();
}
}
app.use(conditionalCSRF);
app.listen(3456);
Run Code Online (Sandbox Code Playgroud)
另一种方法可能只是在某个路径上使用中间件app.post('/forms/*', express.csrf())
.您只想找到一种表达方式,以便在中间件使用或不使用时将其清理干净.
归档时间: |
|
查看次数: |
8548 次 |
最近记录: |