Express.js如何在login后使用安全性

我有一个web应用程序正在build立在express.js与postgresql数据库。 我想知道如何实现安全,但每个人都使用不同的东西(我想这是件好事?)。 不同的模块不同的authentication序列等我现在有什么:1)用户表单发布到例如/login2)应用程序路线到特定路线3)在路线我尝试以下

var localconstring = "postgres://" + usr + ":" + pass + "@ip:port/db"; var client = new pg.Client(localconstring); client.on('drain', client.end.bind(client)); client.connect(function (err, client, done) { 

数据库使用md5,所以pass已经被db保护了。

究竟应该发生什么? 我应该用salt和散列的用户名和密码,然后保存咸/散列的凭据旁边的盐,然后使用数据库的MD5也? 如果是这样的模块?

我应该如此login或尝试从pg_roles /用户select*

非常感谢! (关于盐和散列如果可能的话,一些详细的例子,因为我很熟悉authentication安全)

忘了提及。 cookies ..authentication后,我设置了以下的cookie:

 res.cookie('user', req.body.lguser.username, { signed: true }) res.cookie('watcher', o, { signed: true }) 

之后再看看

 req.signedCookies.user !== undefined 

签名属性是否安全?

你应该生成一个密钥。 这个密钥应该保存在一个cookie和数据库上。 然后当用户提出请求时,可以获取cookie上的密钥并在数据库上search用户。

有些图书馆可以帮助你,看看Passportjs:

http://passportjs.org/

首先MD5是不能再搞的了,所以我build议你使用'sha512'。

一个片段会是这样的:

 var crypto = require('crypto'); var salt = crypto.pseudoRandomBytes(32); crypto.pbkdf2(userPassword,salt,1024,32,function(err,finalPassword){ //on the db you save the salt as a field and the SALTEDPASSWORD !! //in this case the finalPassword } 

所以,当用户login时,您可以通过用户名从db获取用户,并执行以下操作:

 //after getting the user from DB recalculate the hash crypto.pbkdf2(passw,user.salt,1024,32,function(err,corrPass){ if(corrPass.toString() == user.password.toString()) // log in the user //where user.password is the result from the db query } 

而且我推荐使用护照,就像其他哥们说的那样,它简化了所有的cookie。

希望它有帮助!