【发布时间】:2016-03-14 01:19:32
【问题描述】:
我正在使用 Node.js 和 JWT 编写一个 Rest API。
我有下面的路由来验证用户。
我想问一下,User.findOne 方法返回的 user 返回正确的密码,因此我可以检查它是否正确。
但是这样做安全吗?我做了一个console.log,它显示了密码(尽管是加密的),但仍然感觉不安全,因为有人肯定会看到?
router.post('/authenticate', function(req, res) {
// find the user
User.findOne({
name: req.body.name
}, function(err, user) {
if (err) throw err;
if (!user) {
res.json({ success: false, message: 'Authentication failed. User not found.' });
} else if (user) {
// check if password matches
if (user.password != req.body.password) {
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
} else {
// if user is found and password is right
// create a token
var token = jwt.sign(user, app.get('superSecret'), {
expiresInMinutes: 1440 // expires in 24 hours
});
// return the information including token as JSON
res.json({
success: true,
message: 'Enjoy your token!',
token: token
});
}
}
});
});
【问题讨论】:
-
没有。此外,将密码存储在您的服务器上并不安全。您应该使用安全的散列机制在服务器上进行身份验证,以便永远不会存储原始密码。这里有一些关于这个主题的好信息:security.stackexchange.com/questions/19525/… 在这里:crackstation.net/hashing-security.htm
-
我们在当前项目中正在做类似的事情,到目前为止我们没有遇到任何问题。对我们来说,生成的 JWT 令牌保留在浏览器缓存中。要补充的另一件事是,我们的应用程序是组织内部的。
-
问题不在于您以某种方式传递密码,问题在于您需要在某处未加密才能通过它。这通常已经是个坏主意了。
-
@userMod2:请查看我的最新编辑。我还添加了一些您最可能感兴趣的技术方面。如果我的回答令人满意,请务必投票并接受。