Node.js benutzt csurf bedingt mit Express 4
Ich versuche, csurf in meiner Express-App auf nur wenigen Routen zu verwenden. Das ist der Ansatz:
var express = require('express');
var session = require('express-session');
var csrf = require('csurf');
// some more stuff
var csrfExclusion = ['/myApi','/unsecure'];
var app = express();
var conditionalCSRF = function (req, res, next) {
if (csrfExclusion.indexOf(req.path) !== -1){
next();
}
else{
csrf();
}
});
app.use(conditionalCSRF);
versuchte sogar:
var conditionalCSRF = function (req, res, next) {
if (csrfExclusion.indexOf(req.path) !== -1){
next();
}
else{
csrf(req, res, next);
next();
}
});
und
var conditionalCSRF = function (req, res, next) {
if (csrfExclusion.indexOf(req.path) !== -1){
next();
}
else{
csrf();
next();
}
});
Aber das gibt mir einen Fehler: Objekt # hat keine Methode 'csrfToken'
Wie kann ich csurf bedingt nutzen? Die Dokumentation gibt nur Auskunft, um sie auf allen Strecken in der Express-App mit zu nutzen
app.use(csrf());
Aber das ist nicht was ich will, ich möchte eine Route ausschließen ...
freundlich ... martin
AKTUALISIEREN:
endlich klappt es Ich habe folgendes gemacht:
app.use(function(req, res, next){
if (csrfExclusion.indexOf(req.path) !== -1) {
next();
}
else {
csrf()(req, res, next);
});
Dadurch wird die csrf-Middleware bedingt hinzugefügt. Aber ich finde es wirklich seltsam, es so zu nennen:
csrf()(req, res, next);
Ich bekomme nicht einmal die Syntax ...