【问题标题】:Flask session cookies not persisting on browser close烧瓶会话cookie不会在浏览器关闭时持续存在
【发布时间】:2019-03-15 21:10:07
【问题描述】:

我制作了一个烧瓶应用程序,它依赖于会话 cookie 进行登录。但是,每当我重新打开浏览器并访问 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

我不想粘贴所有内容,因为如果它是私人的,但如果我可以包含其他任何可能有帮助的内容,请告诉我,我会这样做。

谢谢。

【问题讨论】:

    标签: python session cookies


    【解决方案1】:

    before_request 中,您需要将会话设置为永久,以便它遵守您的配置设置:

    @app.before_request
    def setup():
        session.permanent = True
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 2015-07-03
      • 2011-08-05
      • 2013-11-29
      • 1970-01-01
      • 2011-04-21
      • 2015-08-12
      • 1970-01-01
      相关资源
      最近更新 更多