在 N 分钟不活动后将用户从 Django 站点注销
我正在开发一个网站,该网站要求用户在 N 分钟不活动后注销。有没有使用 Django 的最佳实践?
I'm working on a website that requires us to log a user out after N minutes of inactivity. Are there any best practices for this using Django?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看一下 会话中间件 和它的设置。具体是这两个:
设置较低的
SESSION_COOKIE_AGE
并打开SESSION_SAVE_EVERY_REQUEST
应该可以创建“滑动”过期时间。Take a look at the session middleware and its settings. Specifically these two:
Setting a low
SESSION_COOKIE_AGE
and turningSESSION_SAVE_EVERY_REQUEST
on should work to create "sliding" expiration.在“settings.py”上,对于会话到期时间,设置SESSION_COOKIE_AGE,默认情况下为1209600 秒(2 周),用于非活动注销,将“True”设置为SESSION_SAVE_EVERY_REQUEST 默认情况下为“False”,如下所示:
On "settings.py", for session expiry time, set SESSION_COOKIE_AGE which is 1209600 seconds(2 weeks) by default and for inactive logout, set "True" to SESSION_SAVE_EVERY_REQUEST which is "False" by default as shown below:
在 django 会话中间件中设置会话 cookie 期限只是在传回浏览器的 set-cookie 标头中设置过期时间。只有浏览器遵守到期时间才会强制“注销”。
根据您需要空闲注销的原因,您可能认为浏览器对到期时间的遵从性不够好。在这种情况下,您需要扩展会话中间件才能执行此操作。
例如,您可以在会话引擎中存储到期时间,并根据请求进行更新。根据站点流量的性质,您可能希望仅在 X 秒内写回会话对象一次,以避免过多的数据库写入。
Setting the session cookie age in the django session middleware just sets the expiry time in the set-cookie header passed back to the browser. It's only browser compliance with the expiry time that enforces the "log out".
Depending on your reasons for needing the idle log-out, you might not consider browser compliance with the expiry time good enough. In which case you'll need to extend the session middleware to do so.
For example you might store an expiry time in your session engine which you update with requests. Depending on the nature of traffic to your site, you may wish to only write back to the session object once in X seconds to avoid excessive db writes.
尝试将 settings.SESSION_COOKIE_AGE 设置为 N * 60 秒。
http://docs.djangoproject.com/en/dev /ref/settings/#session-cookie-age
Try setting settings.SESSION_COOKIE_AGE to N * 60 seconds.
http://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-age
尝试安装 django-auto-logout。您可以控制停机时间
AUTO_LOGOUT = {'IDLE_TIME': 600} # 停机 10 分钟后注销
访问 https://morioh .com/p/eb3e09781dbf
Try install django-auto-logout. you can control downtime
AUTO_LOGOUT = {'IDLE_TIME': 600} # logout after 10 minutes of downtime
visit https://morioh.com/p/eb3e09781dbf