有没有一种简单的方法可以使烧瓶中的会话超时? [英] Is there an easy way to make sessions timeout in flask?

查看:37
本文介绍了有没有一种简单的方法可以使烧瓶中的会话超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 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.

这篇关于有没有一种简单的方法可以使烧瓶中的会话超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆