如果没有活动,则会话终止时烧瓶退出,并重定向登录页面 [英] Flask logout if sessions expires if no activity and redirect for login page
问题描述
我对Flask很陌生,并尝试使用Flask更新网站,在该网站上用户具有帐户并能够登录.如果没有活动超过10分钟,我想使用户会话过期并注销,然后将用户重定向到登录页面.
I'm very new to flask and trying updating a website with flask where users have accounts and are able to login. I want to make user session expire and logout if there is no activity for more than 10 mins and redirect the user for login page.
我想在@ app.before_request中更新它,下面是我的代码.我如何更新它,建议.检查登录时间,然后检查是否没有活动,然后注销.
I want to update it in @app.before_request and below is my code . How do i update it pls suggest. Check for the login time and check if there is been no activity then logout.
@app.before_request
def look_for_user(user=None):
g.usr = {}
g.api = False
if user:
g.usr = user
if 'user_id' in session:
g.usr = get_user((session['user_id'])) //from db
if not g.usr:
g.usr = {}
if not g.usr:
if request.url_rule:
if request.url_rule.rule not in app.config['LOGIN_NOT_REQUIRED']:
session['postlogin_landing_page'] = request.path
if g.api:
return jsonify(error=True, error_message='Invalid Login/Token')
else:
return redirect(app.config['LOGIN_URL'])
elif 'login_page' in session and request.url_rule:
if request.url_rule.rule not in app.config:
landing_page = session.pop('login_page')
return redirect(landing_page)
推荐答案
您可以使用 permanent_session_lifetime
和 session.modified
标志(如
You can use permanent_session_lifetime
and the session.modified
flag as described in this question.
请注意,默认情况下,会话不是永久会话,需要使用 session.permanent = True
激活,如
Note that sessions are not permanent by default, and need to be activated with session.permanent = True
, as described in this answer.
这篇关于如果没有活动,则会话终止时烧瓶退出,并重定向登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!