【问题标题】:RestApi security - how to restrict accessRest Api 安全性 - 如何限制访问
【发布时间】:2018-02-20 02:46:03
【问题描述】:

我打算在 node.js 中有一个 REST API,我想确保请求来自我的 web 域是允许请求还是拒绝它,我该怎么做?

基本上我试图通过简单的技术限制对 REST api 的访问,对此有什么建议吗?

【问题讨论】:

  • 你看过任何关于身份验证的教程吗?那里有很多。
  • 你可能想检查一下像helmetjs这样的东西。它有一个 npm 包。 npmjs.com/package/helmet

标签: javascript node.js security


【解决方案1】:

如果您的条件是只有特定用户可以访问它或只有签名用户可以访问它,那么您可以使用下面的帮助函数来完成。

app.get('/api/users', checkuser, function(req, res) {
    UserModel.find(function(err, users) {
        if (!err)
            res.json(users);
        else
            console.log(err);
    });
});

function checkuser(req, res, next) {
    if (req.user != 'manoj') return res.json('message': 'you dont have permissions to access this url');
    next(); //if it is manoj then call the next() iterator function that will take u to the final callback(userFind)
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2012-02-26
    • 2015-09-08
    • 2015-11-12
    • 2014-04-23
    相关资源
    最近更新 更多