django自定义用户认证模块

class CustomAuth(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = UserProfile.objects.get(Q(username=username) | Q(mobile=username))
            if user.check_password(password):
                return user
        except Exception as e:
            return None

  

AUTHENTICATION_BACKENDS = [
    'apps.users.views.CustomAuth',   # 引用自定义的认证后端
]

  

 

官方文档:https://docs.djangoproject.com/en/2.2/topics/auth/customizing/

猜你喜欢

转载自www.cnblogs.com/xiaohaodeboke/p/12813464.html