我想从我的Express 3.0应用程序中删除csrf,因为我不需要它.我使用oauth验证客户端.使用express.csrf()时,是否将中间件列入白名单API网址?
你可以用两种方式做到这一点.
1.)创建一个自己的小中间件,允许白名单网址模式不被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中间件.例如,您要在配置文件保存控制器上使用csrf检查;
app.post("/profile/save", express.csrf(), function(req, res, next) { // put your code here });