本文介绍了ExpressJS会话过期,尽管活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于。



我正在设置会话maxAge = 900000,我看到session session上的expires属性设置正确。
但是,在后续请求中,超时没有被扩展。它永远不会被扩展,cookie最终会过期。



说Session#touch()不是必需的,因为会话中间件将为我做。我实际上尝试手动调用 req.session.touch(),没有做任何事情,我也尝试在 req.session.cookie上设置maxAge 也没有做任何事情: - (



我是否在某处设置某个地方自动扩展活动会话?没有手动重新创建cookie请求在最终用户活动之后有延长会话超时的其他方法?






编辑: strong>我在express v3中遇到了这个问题,我不是100%肯定的,但我认为可能是罪魁祸首:


解决方案

这里是解决方案,以防其他人有同样的问题:

  function(req,res,next){

if('HEAD '== req.method || 'OPTIONS'== req.method)return next();

//休息会话哈希/强制表达每秒最多吐出一个新的cookie
req.session._garbage = Date();
req.session.touch();

next();

}


Bringing this question to SO since the express group didn't have an answer.

I'm setting the session maxAge = 900000 and I see that the the expires property on the session cookie is set correctly.However, on subsequent requests the timeout is not being extended. It is never extended and the cookie eventually expires.

The session middleware docs say that Session#touch() isn't necessary because the session middleware will do it for me. I actually tried calling req.session.touch() manually and that did nothing, I also tried setting the maxAge on the req.session.cookie as well and that did nothing :-(

Am I missing a setting somewhere to automatically extend active sessions? Short of recreating the cookie manually on each request is there any other way to extend a session timeout after end-user activity?


EDIT: I experienced this problem in express v3. I'm not 100% sure but I think this note from the express changelog may have been the culprit:

解决方案

Here is the solution in case anyone else has the same issue:

function (req, res, next) {

    if ('HEAD' == req.method || 'OPTIONS' == req.method) return next();

    // break session hash / force express to spit out a new cookie once per second at most
    req.session._garbage = Date();
    req.session.touch();

    next();

}

这篇关于ExpressJS会话过期,尽管活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:52