码迷,mamicode.com
首页 > 其他好文 > 详细

dajngo2.1用户名和密码都正确authticate返回None

时间:2019-05-10 23:43:00      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:密码   ack   返回   model   ati   self   check   sel   认证   

django2.1后默认的认证后端(django.contrib.auth.backends.ModelBackend)
而这个类中的authenticate方法中有如下代码:

    def authenticate(self, request, username=None, password=None, **kwargs):
            .....
        if user.check_password(password) and self.user_can_authenticate(user):
                return user

而且user_can_authenticate函数如下:

    def user_can_authenticate(self, user):
        is_active = getattr(user, ‘is_active‘, None)
        return is_active or is_active is None

由此可知django2.1后也会对用户的is_active进行判断,如果is_active为false,则authticate也会返回None.

要解决以上问题,只要继承django.contrib.auth.backends.ModelBackend类重写authticate方法返回True即可,此出刚好有一个类实现了以上操作django.contrib.auth.backends.AllowAllUsersModelBackend.
在settings.py中重新指定后端处理类即可,如下:

AUTHENTICATION_BACKENDS = [‘django.contrib.auth.backends.AllowAllUsersModelBackend‘]

dajngo2.1用户名和密码都正确authticate返回None

标签:密码   ack   返回   model   ati   self   check   sel   认证   

原文地址:https://blog.51cto.com/smly1989/2392322

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!