Node.js客户端会话 - 大的不可知的字符串?

时间:2017-03-02 18:28:52

标签: node.js security cookies

我有一个Node.js网络服务器,并在我使用的client-sessions客户端上安全存储会话cookie。

要设置cookie,可以设置一些属性,如cookieName和一个秘密,其中"应该是一个很大的不可知的字符串"。 但是这个字符串应该怎么做?如何创建这样的字符串,以及如何在程序中正确使用它?使用环境变量或从文件中读取它?

在启动时使用随机字符串在我看来并不是很聪明,因为如果网络服务器崩溃,那么它会创建一个新的秘密并且无法读取以前发布的所有cookie我是对的吗?

app.use(sessions({
  cookieName: 'mySession', // cookie name dictates the key name added to the request object
  secret: 'blargadeeblargblarg', // should be a large unguessable string
  duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
  activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
}));

1 个答案:

答案 0 :(得分:1)

我会生成一个UUID或使用密码管理器程序来生成一个大的随机字符串。然后将其存储在系统环境变量中,以便它不会在您的代码中提交。