认证的使用

from rest_framework.authentication import BaseAuthentication
from api import models
# 认证类
class TokenAuthentication(BaseAuthentication):
def authenticate(self, request):
token = request.query_params.get('token')
user_object = models.UserInfo.objects.filter(token=token).first()
if user_object:
return (user_object, token)
return (None, None) # 认证类的应用
# 单独的视图认证应用
class OrderView(APIView):
authentication_classes = [MyAuthentication, ] #设置成空列表代表没有权限
def get(self,request,*args,**kwargs):
print(request.user)
print(request.auth)
return Response('order') # 认证的全局应用
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES"=["api.views.auth.MyAuthentication",]
}

认证的源码分析

执行流程

1.请求进来执行dispatch方法中的initialize_request方法
def initialize_request(self, request, *args, **kwargs):
parser_context = self.get_parser_context(request) return Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
#会对request对象进行重新封装,把老的request封装成新的request
执行self.get_authenticators()
def get_authenticators(self):
return [auth() for auth in self.authentication_classes] # 循环自定义的认证类,生成认证对象列表封装到request中
authentication_classes
settings中的认证配置
"DEFAULT_AUTHENTICATION_CLASSES":["api.views.auth.MyAuthentication]
2.执行inital
def initial(self, request, *args, **kwargs):
.......略过的代码
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme self.perform_authentication(request) #认证函数
self.check_permissions(request)
self.check_throttles(request)
3.执行perform_authentication(request)认证函数
def perform_authentication(self, request): request.user# 调用request的user方法
4.
@property
def user(self):
if not hasattr(self, '_user'):
with wrap_attributeerrors():
self._authenticate()
return self._user
5.执行_authenticate()方法
def _authenticate(self): for authenticator in self.authenticators:
try:
user_auth_tuple = authenticator.authenticate(self) #每个认证对象(authenticator)中的authenticate方法,可以重写该方法进行定制,之前已经封装到request中了
except exceptions.APIException:
self._not_authenticated()
raise if user_auth_tuple is not None:
self._authenticator = authenticator
self.user, self.auth = user_auth_tuple
return self._not_authenticated()
#返回元祖(user,auth)代表认证成功
#抛出异常代表认证失败
#返回None继续下个认证

看张图吧!

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

正经点......

概括

1.请求进来时,先执行dispatch方法,对的request进行重新封装
2.执行get_authenticators(),循环authentication_classes得到这个认证类的对象,把对象也封装进去
3.执行initial中的perform_authentication(request),调用request.user,调用request的user方法(静态属性)
4.执行 _authenticate,循环authentication对象,调用authenticate方法返回元祖(user,auth)代表认证成功或者抛出异常代表认证失败或返回None继续下个认证

drf源码分析系列---认证的更多相关文章

  1. drf源码分析系列---节流(访问频率限制)

    使用 from rest_framework.throttling import AnonRateThrottle from rest_framework.generics import ListAP ...

  2. drf源码分析系列---权限

    权限的使用 全局使用 from rest_framework.permissions import BasePermission from rest_framework import exceptio ...

  3. drf源码分析系列---版本控制

    版本的使用 第一步:写路由url(r'^api/(P<version>\w+)/user/$',views.UserView.as_view()), 第二步:写模块导入from rest_ ...

  4. MyCat源码分析系列之——前后端验证

    更多MyCat源码分析,请戳MyCat源码分析系列 MyCat前端验证 MyCat的前端验证指的是应用连接MyCat时进行的用户验证过程,如使用MySQL客户端时,$ mysql -uroot -pr ...

  5. drf源码剖析系列(系列目录)

    drf源码剖析系列(系列目录) 01 drf源码剖析之restful规范 02 drf源码剖析之快速了解drf 03 drf源码剖析之视图 04 drf源码剖析之版本 05 drf源码剖析之认证 06 ...

  6. jQuery源码分析系列

    声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://git ...

  7. jQuery-1.9.1源码分析系列完毕目录整理

    jQuery 1.9.1源码分析已经完毕.目录如下 jQuery-1.9.1源码分析系列(一)整体架构 jQuery-1.9.1源码分析系列(一)整体架构续 jQuery-1.9.1源码分析系列(二) ...

  8. MyCat源码分析系列之——结果合并

    更多MyCat源码分析,请戳MyCat源码分析系列 结果合并 在SQL下发流程和前后端验证流程中介绍过,通过用户验证的后端连接绑定的NIOHandler是MySQLConnectionHandler实 ...

  9. MyCat源码分析系列之——SQL下发

    更多MyCat源码分析,请戳MyCat源码分析系列 SQL下发 SQL下发指的是MyCat将解析并改造完成的SQL语句依次发送至相应的MySQL节点(datanode)的过程,该执行过程由NonBlo ...

随机推荐

  1. TestNG+Maven+IDEA 环境配置+入门

    一.环境配置 1.安装IDEA(参考:https://blog.csdn.net/m0_38075425/article/details/80883078) 2.在Prefernces,通过Plugi ...

  2. web前端开发面试题(Vue.js)

    1.active-class是哪个组件的属性?嵌套路由怎么定义? 答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数?  ...

  3. 红帽学习记录[RHCE] 防火墙与网络合作

    目录 防火墙 基本介绍 firewalld 区域zone 管理firewalld 关于富规则 定义 firewalld操作富规则的命令 语法 常用的示例 网络合作 链路聚合 网络组的文件 网络组命令 ...

  4. php基础文档

    目录 PHP简介 PHP概述和名词解释 PHP常见数据类型 PHP运算符 PHP流程控制语句 PHP函数 PHP类与对象 PHP会话session与缓存cookie(扩展) 1.PHP简介 PHP,即 ...

  5. jquery操作css样式的方法

    jquery操作css样式的方法(设置和获取)

  6. 高效PHP Redis缓存技术,可参考下步骤

    是否想过PHP使用redis作为缓存时,如何能: 前后台模块共用Model层: 但是,不能每个Model类都进行缓存,这样太浪费Redis资源: 前后台模块可以自由决定从数据库还是从缓存读数据: 没有 ...

  7. .NET Core 跨平台 GUI 开发之 GTtkSharp 初级篇

    .NET Core 跨平台 GUI 开发之 GTtkSharp 初级篇 本文作为初级篇,适合已经安装好.NET Core 环境以及 Gtk 环境,并具备了 C#开发基础知识,能跑一些简单的例子,希望更 ...

  8. MySQL CRUD使用之小总结

    总结一下最近碰到的一些关于MySQL CRUD方面的语句. 在使用pymysql的executemany方法时,需要注意的几个问题: 1.在写sql语句时,不管字段为什么类型,占位符统一使用%s,且不 ...

  9. css关于控制div靠左或靠右的排版布局

    关于控制div靠左或靠右的排版布局,我整理三种平时用到的css属性小知识(元素模块靠左或靠右排版): 1.float属性(float 属性定义元素在哪个方向浮动) 值:left(元素向左浮动).rig ...

  10. Spring Boot SpringApplication启动类(一)

    目录 目录 前言 1.起源 2.SpringApplication 准备阶段 2.1.推断 Web 应用类型 2.2.加载应用上下文初始器 ApplicationContextInitializer ...