我正在尝试使用 Flask 更新一个网站,其中用户拥有帐户并且能够登录。如果超过 10 分钟没有活动,我想让用户会话过期并注销,并将用户重定向到登录页面。
我想更新它@app.before_request
下面是我的代码。如何检查登录时间并检查是否没有任何活动,然后注销。
@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
如中所述的标志这个问题 https://stackoverflow.com/questions/19760486/resetting-the-expiration-time-for-a-cookie-in-flask/19795394.
请注意,默认情况下会话不是永久的,需要使用以下命令激活session.permanent = True
,如中所述这个答案 https://stackoverflow.com/a/11785722/246534.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)