Django JWT登陆与AUTHENTICATION_BACKENDS自定义验证关系

1.相关配置

# project/urls.py
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
	...
	path('login/', obtain_jwt_token),
]
# project/setting.py
AUTHENTICATION_BACKENDS = (
    'users.views.CustomBackend', #自定义手机号密码登陆
    'social_core.backends.weibo.WeiboOAuth2', #第三方登录
    'django.contrib.auth.backends.ModelBackend' # 默认的验证登陆
)

2.源码分析

  • restframework_jwt/view.py

在这里插入图片描述
按住Ctrl进入JSONWebTokenAPIView
在这里插入图片描述
按Ctrl进入JSONWebTokenSerializer
在这里插入图片描述
进入authenticate(**credentials)
源码如下

def authenticate(request=None, **credentials):
    """
    调用_get_backends()对AUTHENTICATION_BACKENDS做循环遍历验证
    验证成功返回user
    """
    for backend, backend_path in _get_backends(return_tuples=True):
        try:
            inspect.getcallargs(backend.authenticate, request, **credentials)
        except TypeError:
            # This backend doesn't accept these credentials as arguments. Try the next one.
            continue
        try:
        	# 调用该自定义验证的authenticate方法
            user = backend.authenticate(request, **credentials)
        except PermissionDenied:
            # This backend says to stop in our tracks - this user should not be allowed in at all.
            break
        # 用户为空继续执行下一个验证
        if user is None:
            continue
        # Annotate the user object with the path of the backend.
        user.backend = backend_path
        return user

    # The credentials supplied are invalid to all backends, fire signal
    user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials), request=request)

_get_backends()源码

def _get_backends(return_tuples=False):
    backends = []
    """
    循环AUTHENTICATION_BACKENDS
    """
    for backend_path in settings.AUTHENTICATION_BACKENDS:
        backend = load_backend(backend_path)
        backends.append((backend, backend_path) if return_tuples else backend)
    if not backends:
        raise ImproperlyConfigured(
            'No authentication backends have been defined. Does '
            'AUTHENTICATION_BACKENDS contain anything?'
        )
    return backends

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值