简体   繁体   English

NodeJ的快速会话选项

[英]NodeJs express-session options

I want to create a login system by using the express-session module. 我想使用express-session模块创建一个登录系统。 I am not sure if I does all correct and I am confused by the secret option. 我不确定我是否做对了所有的事情,并且对秘密选择感到困惑。 My currently initalize for the express-session is this: 我目前对速成会的要求是:

app.use(session({
   secret: 'important',
   resave: true,
   saveUninitialized: true,
   genid: function(req) {
       return uuidv4();
   }
}));

And I´ve read that the secret parameter is just for cookies ( Link ) but I don´t want to use cookies. 而且我已经读到secret参数仅用于cookie( Link ),但是我不想使用cookie。 I just want to use sessions. 我只想使用会话。

Can I now ignore the secret parameter? 我现在可以忽略secret参数吗?

Apart from what Quentin has pointed out , if you want the secret to be kept secret (as it is an open source project and you don't want to put it in public), you can use an environment variable. 除了Quentin指出的那样 ,如果您希望将秘密保密(因为它是一个开源项目,并且您不想将其公开),则可以使用环境变量。

This should solve your problem: 这应该可以解决您的问题:

secret: process.env.MY_SECRET || "secret"

If the environment variable is set, its value will be used. 如果设置了环境变量,将使用其值。 Else, the string "secret" will be used. 否则,将使用字符串"secret"

As this is an important field, that the users should set it, you can later check if the environment variable is set or not and warn the users accordingly. 由于这是用户应设置的重要字段,因此您以后可以检查是否设置了环境变量,并相应地警告用户。

Short answer: No. You need to use cookies. 简短答案:否。您需要使用Cookie。


When you store a data in a session, it is put in a storage area on the server that is associated with an identifier that represents the user. 当您在会话中存储数据时,会将其放置在服务器上与代表用户的标识符关联的存储区域中。

When the browser makes a request to the server, the server needs some way to tell which storage area to use. 当浏览器向服务器发出请求时,服务器需要某种方式来告知要使用的存储区域。 It does this by putting the identifier in a cookie and giving it to the browser (which then includes it in each subsequent request until the browser is closed). 它通过将标识符放入cookie并将其提供给浏览器(然后将其包含在每个后续请求中,直到关闭浏览器)来实现。


If you really, really don't want to use cookies, then you would have to use some other mechanism to include the token in every request from the browser such as putting it in every URL. 如果您确实确实不想使用cookie,那么您将不得不使用其他机制将令牌包含在浏览器的每个请求中,例如将其放入每个URL。 This is what PHP does if you use trans_sid … and is considered a bad idea: 如果您使用trans_sid,这就是PHP所做的……,这是一个坏主意:

URL based session management has additional security risks compared to cookie based session management. 与基于cookie的会话管理相比,基于URL的会话管理具有其他安全风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM