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

django drf 自定义jwt用户验证逻辑

时间:2019-04-05 18:17:13      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:nbsp   settings   check   setting   turn   render   except   exception   obj   

新建Backend类

from django.contrib.auth.backends import ModelBackend
from django.shortcuts import render

from django.http import HttpResponse
# Create your views here.

from django.contrib.auth import get_user_model
from django.db.models import Q

User = get_user_model()  # 须在settings里配置AUTH_USER_MODEL = ‘users.UserProfile‘

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

配置settings

AUTH_USER_MODEL = users.UserProfile
AUTHENTICATION_BACKENDS=(
    users.views.CumstomBackend,
)

 

django drf 自定义jwt用户验证逻辑

标签:nbsp   settings   check   setting   turn   render   except   exception   obj   

原文地址:https://www.cnblogs.com/chenyishi/p/10659230.html

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