django注册自定义后端
我已经成功地实现了我的auth/login自定义后端,现在我正在尝试实现我自己的django注册自定义后端。 如果我使用contrib.auth的正常身份验证,django注册码似乎工作正常。如果没有(在我的情况下,我想使用我自己新创建的自定义身份验证),我会得到一个 用户对象没有后端属性 我的注册后端:django注册自定义后端,django,authentication,registration,django-registration,Django,Authentication,Registration,Django Registration,我已经成功地实现了我的auth/login自定义后端,现在我正在尝试实现我自己的django注册自定义后端。 如果我使用contrib.auth的正常身份验证,django注册码似乎工作正常。如果没有(在我的情况下,我想使用我自己新创建的自定义身份验证),我会得到一个 用户对象没有后端属性 我的注册后端: from django.conf import settings #from django.contrib.auth import authenticate from django.cont
from django.conf import settings
#from django.contrib.auth import authenticate
from django.contrib.auth import login
from registration import signals
from registration.forms import RegistrationForm
class MyRegistrationBackend(object):
def register(self, request, **kwargs):
"""
Create and immediately log in a new user.
"""
print "debug"
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
User.objects.create_user(username, email, password)
# authenticate() always has to be called before login(), and
# will return the user we just created.
auth = MyAuthBackend()
new_user = auth.authenticate(username=username, password=password)
login(request, new_user)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
class MyAuthBackend(object):
"""
Authenticates against django.contrib.auth.models.User. with my modifications
"""
supports_inactive_user = True
"""
This function does not upgrade the user password hasher
"""
def check_password(self,password, encoded):
if not password or not is_password_usable(encoded):
return False
password = smart_str(password)
encoded = smart_str(encoded)
if encoded[0] == "$":
encoded = encoded[1:] #make it compatible so that drupal 7 sha512 hasher can work properly
if len(encoded) == 32 and '$' not in encoded:
hasher = get_hasher('unsalted_md5')
else:
algorithm = encoded.split('$', 1)[0]
hasher = get_hasher(algorithm)
is_correct = hasher.verify(password, encoded)
return is_correct
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(username=username)
if self.check_password(password, user.password):
return user
except User.DoesNotExist:
return None
然后我的身份验证后端:
from django.conf import settings
#from django.contrib.auth import authenticate
from django.contrib.auth import login
from registration import signals
from registration.forms import RegistrationForm
class MyRegistrationBackend(object):
def register(self, request, **kwargs):
"""
Create and immediately log in a new user.
"""
print "debug"
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
User.objects.create_user(username, email, password)
# authenticate() always has to be called before login(), and
# will return the user we just created.
auth = MyAuthBackend()
new_user = auth.authenticate(username=username, password=password)
login(request, new_user)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
class MyAuthBackend(object):
"""
Authenticates against django.contrib.auth.models.User. with my modifications
"""
supports_inactive_user = True
"""
This function does not upgrade the user password hasher
"""
def check_password(self,password, encoded):
if not password or not is_password_usable(encoded):
return False
password = smart_str(password)
encoded = smart_str(encoded)
if encoded[0] == "$":
encoded = encoded[1:] #make it compatible so that drupal 7 sha512 hasher can work properly
if len(encoded) == 32 and '$' not in encoded:
hasher = get_hasher('unsalted_md5')
else:
algorithm = encoded.split('$', 1)[0]
hasher = get_hasher(algorithm)
is_correct = hasher.verify(password, encoded)
return is_correct
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(username=username)
if self.check_password(password, user.password):
return user
except User.DoesNotExist:
return None
有什么想法吗??我相信可能是我以错误的方式实例化了
auth=MyAuthBackend()。。或者可能是其他原因请尝试设置身份验证\u后端
设置,如上的文档中所述
然后,在register方法中,使用django.contrib.auth.authenticate
方法登录(请参阅),而不是手动实例化后端实例。该身份验证方法应该负责设置userbackend
属性,这样就不会出现任何错误