烧瓶常会:在哪里定义它们? [英] Flask permanent session: where to define them?

查看:69
本文介绍了烧瓶常会:在哪里定义它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Flask使用易失性会话,这意味着会话cookie被设置为在浏览器关闭时过期。为了使用永久会话(将使用具有定义的到期日期的cookie),应设置 session.permanent = True ,并且将根据失效日期进行设置在 config ['PERMANENT_SESSION_LIFETIME'] 上。

By default, Flask uses volatile sessions, which means the session cookie is set to expire when browser closes. In order to use permanent sessions, which will use a cookie with a defined expiration date, one should set session.permanent = True, as is mentioned in this question., and the expiration date will be set based on config['PERMANENT_SESSION_LIFETIME'].

我很惊讶会话生存期是在配置文件中定义的,但是不可能通过配置请求使用永久会话,例如 config ['USE_PERMANENT_SESSION'] =真

I am surprised that session lifetime is defined in config file, yet it is not possible to request the use of permanent sessions through configuration, such as a config['USE_PERMANENT_SESSION'] = True. But so be it.

我的问题是:如果您要做想要永久性会议,那么定义它们的最佳位置是什么?是否在提到的问题中提出的 @ app.before_request 函数中?但这是否意味着在每次请求时都要重新设置?似乎一旦设置, session.permanent 仍然适用,直到会话结束。

My question is: if you do want permanent sessions, what is the best place to define them ? Is it in an @app.before_request function as proposed in mentioned question ? But that would mean setting it over again at each request ? It seems that once set, session.permanent remains true till end of session.

永久会话通常在登录后使用 ,因此请求它们的最佳位置可能是在处理 login_user时()?因此,最好的策略是对所有匿名页面使用易失性会话cookie,并通过在登录时执行 session.permanent = True 切换到永久会话吗?

Permanent sessions are generally used after sign-in, so maybe the best place to request them is while processing login_user() ? So is the best policy to use volatile session cookies for all anonymous pages, and switch to permanent sessions by doing a session.permanent = True at sign-in ?

然后可能要设置不同的生存期,具体取决于它是普通的会话 cookie还是 remember_me cookie。达到此目的的最佳方法是什么?

And one might want to set a different lifetime depending on whether it is the ordinary session cookie, or the remember_me cookie. What would be the best way to achieve this ?

推荐答案

我很惊讶没有一个人回答这个问题。似乎应该有某种类型的配置变量 SESSION_PERMANENT = True 。但不幸的是没有。正如您提到的,这是最好的方法。

I'm surprised no on has answered this question. It seems like there should be some type of config variable SESSION_PERMANENT = True. But unfortunately there isn't. As you mentioned this is the best way to do it.

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

这篇关于烧瓶常会:在哪里定义它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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