Django 基于密钥的会话过期
我有一个网站,其中包含针对不同类型用户的多个规则集。 其中一项规则(权限)取决于会话过期时间。
例如,当浏览器关闭时,未经身份验证的用户会话必须被刷新,但是,经过身份验证的用户会话应该持续一段固定的时间。
此外,对于经过身份验证的用户,当浏览器关闭时,会话中的某些密钥可能会被删除,但其他密钥应保持活动状态。
如何在 Django 中实现基于密钥的会话过期?
I have a website which contains several rule sets for different kind of users.
One of the rules (permission) depends on the session expiration.
For instance, an unauthenticated users' session must be flushed when browser is closed however, authenticated users' sessions should live for a constant time.
Furthermore, for authenticated users some keys in the session may be deleted when the browser is closed but other should be kept alive.
How can I achieve this key-based session expiration in Django ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
set_expiry
方法。该方法采用整数表示会话过期的秒数,使用datetime
或timedelta
表示会话何时过期,整数 0 表示会话应过期于浏览器关闭时间或None
指示会话应回退到默认超时策略。您应该能够编写一个中间件来评估会话过期的标准,然后在处理请求之前在会话上调用
set_expiry
。You can do this by using the
set_expiry
method onrequest.session
. The method takes either an integer for number of seconds to expire the session, adatetime
ortimedelta
for when the session should expire, the integer 0 to indicate the session should expire at browser close time orNone
to indicate that the session should fall-back to the default timeout policy.You should be able to write a piece of middleware that evaluates the criteria you have for session expiration then call
set_expiry
on the session before processing the request.