重写身份验证方法 - Django admin

发布于 2024-10-02 09:08:41 字数 246 浏览 2 评论 0原文

我正在尝试找出如何通过附加功能来增强身份验证方法。

例如,

  • 过期密码、
  • 特殊密码格式
  • 长度要求
  • 等...

对于网站的前端来说非常简单,但是管理面板呢?


我认为我应该覆盖用户管理器对象,因为可能需要进行身份验证居住在那里。我认为这是一个很难弄清楚的问题。

提前致谢! :)

I'm trying to figure out how to enhance the authenticate method with additional functionality.

e.g.

  • Expiring passwords
  • special password formats
  • length requirements
  • etc...

It is pretty straight forward for the site's frontend, but what about the admin panel?


I reckon that I should override the User's Manager object, as authenticate probably resides there. This is quite a tough one to figure out I think.

Thanks in advance! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱你是孤单的心事 2024-10-09 09:08:41

您可以按照 http://docs 中的说明创建自定义身份验证后端.djangoproject.com/en/dev/topics/auth/#authentication-backends。本质上,您创建一个具有 authenticate 方法的后端类:

class MyBackend:
    def authenticate(self, username=None, password=None):
        # Check the username/password and return a User.

然后将该类添加到 settings.py 中的 AUTHENTICATION_BACKENDS

尽管这是用于身份验证,但例如,如果密码正确但已过期,您可以通过将用户重定向到更改密码页面来完成您提到的所有密码验证操作。考虑使用消息传递框架来向用户提示什么是当引导他进入通用更改密码页面时发生的情况。

You can create custom authentication backend by following the instructions in http://docs.djangoproject.com/en/dev/topics/auth/#authentication-backends. Essentially, you create a backend class that has an authenticate method:

class MyBackend:
    def authenticate(self, username=None, password=None):
        # Check the username/password and return a User.

Then add the class to AUTHENTICATION_BACKENDS in settings.py.

Though this is for authentication, you could do all the password validation things you mentioned simply by redirecting a user to a change password page if the password is correct but expired, for instance. Consider using the messaging framework to give a user a hint about what is going on when directing him to a generic change password page.

卸妝后依然美 2024-10-09 09:08:41

如果您希望将密码验证内置到模型中,那么您可能需要 扩展 django 用户模型

否则,您可以执行以下操作:

  • 通过创建自己的用于更改和设置密码的视图来覆盖管理员密码选项,然后将相关 URL 放在 (r'^admin/', include(admin.site.urls)) 上方。正则表达式类似于 (r'^admin/auth/user/(\d+)/password/', new_change_password)
  • 在单独的模型中跟踪密码期限,然后当它们过期时,重定向到过期后更改密码。

If you want the validation for passwords to be built into the model, then you'll probably want to extend the django User model.

Otherwise, you could do the following:

  • override admin password options by creating your own views for changing and setting passwords, then putting the relevant URLS just above (r'^admin/', include(admin.site.urls)). Regex would look something like (r'^admin/auth/user/(\d+)/password/', new_change_password).
  • Keep track of password age in a separate model and then when they expire, redirect to a change password once it expires.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文