如何使用 express-session 在 node.js 上设置持久性 cookie?
How can I set presistent cookies on node.js with express-session?
我想创建预先存在的 cookie,这样我的应用程序的用户就不必在每次访问网络时都登录。但是,如果 he/she 曾经注销,则只需要登录。基本上,我想知道是否有办法将 maxAge 设置为 infinity(equivalent) 并且即使用户关闭浏览器也能保留 cookie。
这是我的代码:
app.use(expressSession({
secret: '',
resave: false,
saveUninitialized: false,
cookie: {
secure: false,
expires: false,
}
}))
根据标准,您不能将其设置为无穷大:
Max-Age=value
OPTIONAL. The value of the Max-Age attribute is delta-seconds,
the lifetime of the cookie in seconds, a decimal non-negative
integer. To handle cached cookies correctly, a client SHOULD
calculate the age of the cookie according to the age calculation
rules in the HTTP/1.1 specification [RFC2616]. When the age is
greater than delta-seconds seconds, the client SHOULD discard the
cookie. A value of zero means the cookie SHOULD be discarded
immediately.
http://www.faqs.org/rfcs/rfc2965.html
但作为替代方案,您可以安全地将年龄设置为 5 年或 10 年后。到这个时候,用户很可能会摆脱他的 device/PC/Mobile。 :)
更新:
要设置未来的到期时间,您可以使用 MaxAge,如下所示,将其设置为一个月后到期:
app.use(expressSession({
secret: '',
resave: false,
saveUninitialized: false,
rolling: true,
cookie: {
secure: false,
maxAge: 30 * 24 * 60 * 60 * 1000
}
}))
这会将其设置为过期
我想创建预先存在的 cookie,这样我的应用程序的用户就不必在每次访问网络时都登录。但是,如果 he/she 曾经注销,则只需要登录。基本上,我想知道是否有办法将 maxAge 设置为 infinity(equivalent) 并且即使用户关闭浏览器也能保留 cookie。 这是我的代码:
app.use(expressSession({
secret: '',
resave: false,
saveUninitialized: false,
cookie: {
secure: false,
expires: false,
}
}))
根据标准,您不能将其设置为无穷大:
Max-Age=value OPTIONAL. The value of the Max-Age attribute is delta-seconds, the lifetime of the cookie in seconds, a decimal non-negative integer. To handle cached cookies correctly, a client SHOULD calculate the age of the cookie according to the age calculation rules in the HTTP/1.1 specification [RFC2616]. When the age is greater than delta-seconds seconds, the client SHOULD discard the cookie. A value of zero means the cookie SHOULD be discarded immediately.
http://www.faqs.org/rfcs/rfc2965.html
但作为替代方案,您可以安全地将年龄设置为 5 年或 10 年后。到这个时候,用户很可能会摆脱他的 device/PC/Mobile。 :)
更新:
要设置未来的到期时间,您可以使用 MaxAge,如下所示,将其设置为一个月后到期:
app.use(expressSession({
secret: '',
resave: false,
saveUninitialized: false,
rolling: true,
cookie: {
secure: false,
maxAge: 30 * 24 * 60 * 60 * 1000
}
}))
这会将其设置为过期