有几种可能的方法。您基本上需要了解最简单和最正确的规则,以便决定是否使用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);另一种方法可能只是在诸如的特定路径上使用中间件
app.post('/forms/*',express.csrf())。您只想找到一种表达方式,使其在使用或不使用中间件时变得干净。


