有没有一种简单的方法可以使会话在烧瓶中超时? [英] Is there an easy way to make sessions timeout in flask?
问题描述
我正在用烧瓶建立一个网站,用户有帐户并且能够登录。
我正在使用flask-principal进行部分日志和角色管理。
说5分钟或10分钟后,用户的会话是否有效?
我无法在flask文档或flask-principal的文档中找到它。
我想到了一个手动的方法,设置一个变量服务器端在登录的时候带有时间标签,在用户下一个动作时,服务器验证该时间戳上的时间差,并删除会话。
一旦关闭浏览器,会话就会过期,除非您有永久会话。您可以尝试以下操作:
from datetime从flask导入会话导入timedelta
,
@ app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes = 5)
默认情况下,在Flask中,permanent_session_lifetime设置为31天。
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 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)
By default in Flask, permanent_session_lifetime is set to 31 days.
这篇关于有没有一种简单的方法可以使会话在烧瓶中超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!