在 N 分钟不活动后将用户从 Django 站点注销

发布于 2024-08-27 04:04:03 字数 56 浏览 3 评论 0原文

我正在开发一个网站,该网站要求用户在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里南柯 2024-09-03 04:04:03

看一下 会话中间件 和它的设置。具体是这两个:

SESSION_COOKIE_AGE

默认:1209600(2周,以秒为单位)

会话cookies的年龄,在
秒。

SESSION_SAVE_EVERY_REQUEST

默认值:假

是否保存会话数据
每一个请求。如果这是假的
(默认),那么会话数据将
仅在修改后才保存
-- 也就是说,它的任何字典值是否已被分配或删除。

设置较低的 SESSION_COOKIE_AGE 并打开 SESSION_SAVE_EVERY_REQUEST 应该可以创建“滑动”过期时间。

Take a look at the session middleware and its settings. Specifically these two:

SESSION_COOKIE_AGE

Default: 1209600 (2 weeks, in seconds)

The age of session cookies, in
seconds.

SESSION_SAVE_EVERY_REQUEST

Default: False

Whether to save the session data on
every request. If this is False
(default), then the session data will
only be saved if it has been modified
-- that is, if any of its dictionary values have been assigned or deleted.

Setting a low SESSION_COOKIE_AGE and turning SESSION_SAVE_EVERY_REQUEST on should work to create "sliding" expiration.

浪荡不羁 2024-09-03 04:04:03

“settings.py”上,对于会话到期时间,设置SESSION_COOKIE_AGE,默认情况下为1209600 秒(2 周),用于非活动注销,将“True”设置为SESSION_SAVE_EVERY_REQUEST 默认情况下为“False”,如下所示:

# "settings.py"

SESSION_COOKIE_AGE = 180 # 3 minutes. "1209600(2 weeks)" by default 

SESSION_SAVE_EVERY_REQUEST = True # "False" by default

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:

# "settings.py"

SESSION_COOKIE_AGE = 180 # 3 minutes. "1209600(2 weeks)" by default 

SESSION_SAVE_EVERY_REQUEST = True # "False" by default
栀梦 2024-09-03 04:04:03

在 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.

窝囊感情。 2024-09-03 04:04:03

尝试将 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

沉溺在你眼里的海 2024-09-03 04:04:03

尝试安装 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文