【问题标题】:RestApi security - how to restrict accessRest Api 安全性 - 如何限制访问
【发布时间】:2018-02-20 02:46:03
【问题描述】:
我打算在 node.js 中有一个 REST API,我想确保请求来自我的 web 域是允许请求还是拒绝它,我该怎么做?
基本上我试图通过简单的技术限制对 REST api 的访问,对此有什么建议吗?
【问题讨论】:
标签:
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)
};