我制作了一个依赖会话 cookie 登录的 Flask 应用程序。但是,每当我重新打开浏览器并访问http://localhost:5000/ 时,cookie 就会消失并且我已注销。
这是我的一些代码:
app = Flask(__name__)
app.config["SECRET_KEY"] = <bytes object generated by os.urandom(24)>
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(hours=1)
@app.route("/")
def login_redirect():
if check_login(session, False):
return redirect("/interface")
return redirect("/login")
def check_login(session, requires_elevated):
if "username" not in session:
return False
elif session["username"] == "admin":
return True
elif session["username"] == "regular" and not requires_elevated:
return True
return False
我不想粘贴所有内容,因为有些内容是私人的,但如果我可以包含任何其他内容可能会有所帮助,请告诉我,我会这样做。
谢谢。
在 abefore_request
您需要将会话设置为永久,以便它遵守您的配置设置:
@app.before_request
def setup():
session.permanent = True
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句