Django使用自定义认证方式
创建登录应用
首先创建一个新的login app,用来存放认证用到代码
python manage.py startapp login
修改settings.py中的认证项
AUTHENTICATION_BACKENDS = (
‘login.auth.UsernamePasswordAuth‘,
)
自定义认证类
在login app下创建auth.py文件,内容如下
#coding:utf-8
from django.contrib.auth.models import User
class UsernamePasswordAuth(object):
def authenticate(self, username=None, password=None):
print("UsernamePasswordAuth.authenticate")
try:
user = User.objects.get(username__iexact=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
print("UsernamePasswordAuth.get_user")
try:
user = User.objects.get(pk=user_id)
return user
except User.DoesNotExist:
return None
转载请以链接形式标明本文地址
时间: 2024-10-23 02:08:08