2017-03-01 173 views
0

我越來越Error: misconfigured csrf當我試圖訪問我的登錄頁面。我執行csurf到路由器,但我只是得到響應的StatusCode 500錯誤:錯誤地配置了CSRF - Express.js

實現:

let router = require("express").Router(); 

let PostLoginResource = require("./../resources/PostLoginResource"); 
let RateLimit = require("express-rate-limit"); 
let csrf = require("csurf"); 

let csrfProtection = csrf({ cookie: true }); 

router.route("/login") 
    .get(csrfProtection, function(req, res) { 
     // Do some stuff 
    }) 

    .post(loginLimiter, function(req, res) { 

     PostLoginResource(req, function(success, err) { 
      // Do some stuff 
     }) 

    }); 

我開始我使用這個模塊之前會話cookie的app.js:

// Parse the request body as JSON 
app.use(bodyParser.json()); 

// Parse the URL encoded data 
app.use(bodyParser.urlencoded({extended: true})); 

// Set up session-cookie 
app.use(session({ 
    secret: "secret", 
    resave: false, 
    saveUninitialized: true, 
    cookie: {secure: true, 
     httpOnly: true, 
     maxAge: 1000 * 60 * 60 * 24 
    } 
})); 

不過,這是行不通的。任何人都知道是什麼問題?

+0

是真實的是,路徑正確:./../resources/PostLoginResource? – Remario

+0

是的,否則那將是錯誤的消息,而不是錯誤:錯誤地配置了CSRF – Jesper

+0

但你也有一個500,這意味着內部錯誤。不正確的語句導致it.Where是否要求會話對象> – Remario

回答

0
var cookieParser = require('cookie-parser') 

解析餅乾 我們需要這個,因爲 「曲奇」 在csrfProtection

app.use(cookieParser()) 
+0

注*由於調用app.use(CSRF())必須app.use(cookieParser())和app.use(會話({之後設置... – Remario

+0

作爲快速會話中間件的官方說明:https://github.com/expressjs/session '自1.5.0版本以來,不再需要使用cookie分析器中間件來使此模塊工作。現在模塊可以直接讀取和REQ/RES寫的Cookie。使用Cookie的解析器可能導致的問題,如果這個祕密是不是這個模塊和cookie的解析器之間的相同「。 – Jesper