如何在没有 maxAge 的情况下在 redis node.js 中设置会话到期日期?

How do I set a session expiration date in redis node.js without maxAge?

我有这个代码:

const redisClient = redis.createClient({
   host: 'localhost',
   port: 6379,
   ttl: 10
});

app.use(session({
  store: new RedisStore({ client: redisClient });,
  secret: 'some secret',
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    sameSite: true,
    maxAge: 60*1000
  }
}));

一切正常。 maxAge过期后,redis也清空。但是如果删除maxAge留下ttl,那么redis永远不会被清除,如何在不指定maxAge的情况下清除redis,因为用户可以改变cookie的值maxAge ]到null,然后redis就永远不会被清零了。我能以某种方式直接在 redis 中而不是在 cookie 中指定过期时间吗?还是我弄错了什么?提前谢谢你。

如您在 connect-redis docs under the ttl option 中所见 - “如果会话 cookie 有过期日期,connect-redis 将使用它作为 TTL。”。

所以在你的情况下 - cookie 和 redis 中的密钥都将在到期时被清除。

而且即使客户端以某种方式更改cookie过期时间,也不会影响redis(因此cookie可能通过HTTP请求发送,但服务器不会识别)。

(您没有提到您使用的是哪个模块,我只是假设它是 express-sessionconnect-redis