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

Posted chenyishi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django drf 自定义jwt用户验证逻辑相关的知识,希望对你有一定的参考价值。

新建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用户验证逻辑的主要内容,如果未能解决你的问题,请参考以下文章