删除API后调用csrf保护

我想从我的Express 3.0应用程序中删除csrf,因为我不需要它。 我使用oauth来validation客户端。 当使用express.csrf()时,是否将中间件列入白名单?

你可以用两种方法做到这一点。

1.)创build一个自己的小型中间件,允许白名单url模式不被csrf阻塞;

var express = require("express"); var expressCsrf = express.csrf(); var app = express.createServer(); var customCsrf = function (req, res, next) { // I assume exact match, but you can use regex match here var csrfEnabled = true; var whiteList = new Array("/pattern1/param1","/pattern2/param2","/pattern3/param3"); if (whiteList.indexOf(req.path) != -1) { csrfEnabled = false; } if (csrfEnabled) { expressCsrf(req, res, next); } else { next(); } } app.use(customCsrf); app.listen(3000); 

2.)在要启用的控制器上使用csrf中间件。 例如,您想在configuration文件保存控制器上使用csrf检查;

 app.post("/profile/save", express.csrf(), function(req, res, next) { // put your code here });