重写的Django身份验证未返回用户对象

时间:2019-01-01 12:30:55

标签: django authentication backend

我已经重写了Django的默认身份验证系统。但这并不返回用户对象。

from __future__ import unicode_literals
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission

class ModelBackend(object):
"""
Authenticates against settings.AUTH_USER_MODEL.
"""

  def authenticate(self, username=None, password=None, **kwargs):
    UserModel = get_user_model()
    if username is None:
        username = kwargs.get(UserModel.USERNAME_FIELD)
    try:
        user = UserModel._default_manager.get_by_natural_key(username)
        print("Database pass : ",user.password,"incoming pass : ",password)
        # if user.check_password(password):
            # print(user.check_password(password))
            # return user
        if str(user.password) == str(password):
            print(12345,user)
            return user
    except UserModel.DoesNotExist:
        # Run the default password hasher once to reduce the timing
        # difference between an existing and a non-existing user (#20760).
        UserModel().set_password(password)

尽管密码相同,但始终显示不正确的密码,并且还会打印12345和用户,但不会返回用户。

0 个答案:

没有答案