【问题标题】:Csrf fix in node js at page level页面级别的节点 js 中的 Csrf 修复
【发布时间】:2019-02-07 08:34:57
【问题描述】:

我查看了 node js Git hub example 中的 csrf 实现,想知道这是针对应用程序级别的。如果我想在应用程序的一两个页面上应用它怎么办。我在网上没有找到任何示例。我知道在 asp.net 中你可以做到,但不确定如何在 Node JS 中实现。

非常感谢任何帮助指导解决方案。

【问题讨论】:

    标签: html node.js csrf-protection csrf-token


    【解决方案1】:

    在要保护的路由上添加 csruf 中间件。就像 Github 链接中的example 所示:

    app.get('/route/to/protect', csrfProtection, function (req, res) {
       // pass the csrfToken to the view
       res.render('send', { csrfToken: req.csrfToken() })
    })
    

    这将允许 csrf 仅用于这条路线,而不是其他路线。您还可以使用 Regex 来匹配路由,如 express docs 中所述。

    【讨论】:

    • 我们正在使用 res.redirect()。对于这个 csrftoken 不能按照您在示例中显示的方式传递。如果我们使用 res.redirect(),你能分享一个示例吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 2016-05-15
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多