简体   繁体   English

如何动态设置Flask会话超时?

[英]How to dynamically set the Flask Session timeout?

In my Flask application I typically set the timeout for the session in the beginning of the code: 在我的Flask应用程序中,我通常在代码的开头设置会话的超时:

session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=5)

I need to set the timeout depending on the user's permissions after logging in. Where is the best place to add this code so that I can dynamically change the session lifetime variable? 我需要在登录后根据用户的权限设置超时。添加此代码的最佳位置在哪里,以便我可以动态更改会话生存期变量? I was thinking maybe after_request, but I only need this to run after a single particular login request.... 我想也许是after_request,但我只需要在一个特定的登录请求之后运行....

I would recommend adding it at before_request and checking permissions on each request to set the timeout. 我建议在before_request中添加它并检查每个请求的权限以设置超时。

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=5)

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

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