【问题标题】:Any way of geting csrfToken in POST request in express?在快递的POST请求中获取csrf令牌的任何方式?
【发布时间】:2018-05-10 04:07:05
【问题描述】:

在我的应用程序中,我有一个来自 official docs 的代码,除了一个区别:我发送 xsrfToken 以响应 POST 请求,而不是 GET。

var cookieParser = require('cookie-parser')
var csrf = require('csurf')
var bodyParser = require('body-parser')
var express = require('express')

// setup route middlewares
var csrfProtection = csrf({ cookie: true })
var parseForm = bodyParser.urlencoded({ extended: false })

var app = express()

// we need this because "cookie" is true in csrfProtection
app.use(cookieParser())

app.post('/getCsrfToken', /*csrfProtection,*/ function (req, res) {
    // check credentials from request.body
    // and then 

    res.render('send', { csrfToken: req.csrfToken() })  //EXCEPTION: csrfToken is not a function 
})

app.post('/process', parseForm, csrfProtection, function (req, res) {
    res.send('data is being processed')
})

我正面临着鸡蛋母鸡的问题:如果我启用 csrfProtection,我无法在没有令牌的情况下进入端点的代码,但如果我禁用它,req.csrfToken 将变为未定义。

我需要 gerCsrfToken 端点为 POST,因为我不想将密码公开为 url 参数。

【问题讨论】:

    标签: javascript node.js express csrf csrf-protection


    【解决方案1】:

    问题已由 csurf 维护者回答,感谢您的快速回复!

    https://github.com/expressjs/csurf/issues/133

    (棘手的)解决方案是忽略此特定端点的 POST 方法

    app.post('/authenticate', csrf({ cookie: true, ignoreMethods: ['POST'] }), function (req, res) {
    

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 2015-05-16
      • 1970-01-01
      • 2018-12-23
      • 2019-01-01
      相关资源
      最近更新 更多