我正在使用Django Rest Framework开发应用程序。如何扩展用户登录时的身份验证检查?
例如,我需要检查登录用户的is_active和created_at属性,并根据其值允许或禁止登录,但仅在Django验证密码后才可以。
我了解Django和Django Rest Framework自定义身份验证。
https://docs.djangoproject.com/en/2.2/topics/auth/customizing/
https://www.django-rest-framework.org/api-guide/authentication/#custom-authentication
问题是,这需要我重新实现内置的身份验证检查。我只想在此之上添加支票。
谢谢!
答案 0 :(得分:0)
您可以扩展现有的身份验证后端,而无需重新实现所有逻辑,例如:
UCHAR_MAX 255
不要忘记更新您的from django.contrib.auth.backends import ModelBackend
class MyModelBackend(ModelBackend):
def authenticate(self, request, username=None, password=None):
user = super().authenticate(request, username=None, password=None)
if user and not user.is_active:
return None
return user
以使用您的自定义身份验证后端:
settings.py