有没有一种简单的方法可以使烧瓶中的会话超时?

问题描述:

我正在用 Flask 构建一个网站,用户拥有帐户并且可以登录.我正在使用 Flask-principal 进行部分登录和角色管理.有没有办法让用户的会话在 5 分钟或 10 分钟后过期?我在flask 文档或flask-principal 的文档中找不到.

I'm building a website with flask where users have accounts and are able to login. I'm using flask-principal for the loging in part and the role management. Is there a way of making the user's session expire after say 5 minutes or 10 minutes? I was not able to find that in flask documentation or, flask-principal's documentation.

我想到了一种手动完成的方法,在登录时设置一个带有时间标签的变量服务器端,在用户执行的下一个操作中,服务器验证该时间戳上的时间增量并删除会议.

I thought of a way of doing it by hand, set a variable server-side with a time tag at the moment of login and at the next action the user takes, the server verifies the time-delta on that timestamp and deletes the session.

flask 会话一旦关闭浏览器就会过期,除非您有一个永久会话.您可以尝试以下操作:

flask sessions expire once you close the browser unless you have a permanent session. You can possibly try the following:

from datetime import timedelta
from flask import session, app

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

在 Flask 中,permanent_session_lifetime 默认设置为 31 天.

By default in Flask, permanent_session_lifetime is set to 31 days.