drf源码分析系列---认证
认证的使用
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源码分析系列---认证的更多相关文章
- drf源码分析系列---节流(访问频率限制)
使用 from rest_framework.throttling import AnonRateThrottle from rest_framework.generics import ListAP ...
- drf源码分析系列---权限
权限的使用 全局使用 from rest_framework.permissions import BasePermission from rest_framework import exceptio ...
- drf源码分析系列---版本控制
版本的使用 第一步:写路由url(r'^api/(P<version>\w+)/user/$',views.UserView.as_view()), 第二步:写模块导入from rest_ ...
- MyCat源码分析系列之——前后端验证
更多MyCat源码分析,请戳MyCat源码分析系列 MyCat前端验证 MyCat的前端验证指的是应用连接MyCat时进行的用户验证过程,如使用MySQL客户端时,$ mysql -uroot -pr ...
- drf源码剖析系列(系列目录)
drf源码剖析系列(系列目录) 01 drf源码剖析之restful规范 02 drf源码剖析之快速了解drf 03 drf源码剖析之视图 04 drf源码剖析之版本 05 drf源码剖析之认证 06 ...
- jQuery源码分析系列
声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://git ...
- jQuery-1.9.1源码分析系列完毕目录整理
jQuery 1.9.1源码分析已经完毕.目录如下 jQuery-1.9.1源码分析系列(一)整体架构 jQuery-1.9.1源码分析系列(一)整体架构续 jQuery-1.9.1源码分析系列(二) ...
- MyCat源码分析系列之——结果合并
更多MyCat源码分析,请戳MyCat源码分析系列 结果合并 在SQL下发流程和前后端验证流程中介绍过,通过用户验证的后端连接绑定的NIOHandler是MySQLConnectionHandler实 ...
- MyCat源码分析系列之——SQL下发
更多MyCat源码分析,请戳MyCat源码分析系列 SQL下发 SQL下发指的是MyCat将解析并改造完成的SQL语句依次发送至相应的MySQL节点(datanode)的过程,该执行过程由NonBlo ...
随机推荐
- 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息
承接上一篇,上一篇讲到可以在lldb调试中调用QuartzCore.framework里的CA::Render::Object::show方法来是观察CA::Render模块内的类的信息,但是在lld ...
- SpringBoot系列之切换log4j日志框架
SpringBoot系列之使用切换log4j日志框架 ok,在pom文件右键->Diagrams->show Dependencies....,如图,找到spring-boot-start ...
- ubuntu server 1604 搭建FTP服务器
1.查看是否安装 ftp服务器vsftpd -v 2.安装ftp服务器sudo apt-get install vsftpd 3.如果安装失败或者配置出现问题,可以卸载 ftp服务器sudo apt- ...
- php中static关键字的理解
函数内的static变量 static静态变量的理解 静态变量 类型说明符是static. 静态变量属于静态存储方式,其存储空间为内存中的静态数据区(在 静态存储区内分配存储单元),该区域中的数据在整 ...
- 新闻实时分析系统Hive与HBase集成进行数据分析
(一)Hive 概述 (二)Hive在Hadoop生态圈中的位置 (三)Hive 架构设计 (四)Hive 的优点及应用场景 (五)Hive 的下载和安装部署 1.Hive 下载 Apache版本的H ...
- Selenium+Java(四)Selenium Xpath元素定位
前言 关于Selenium元素定位,这是最后一篇博客. Xpath定位可以实现的功能 Selenium+Java(三)Selenium元素定位中讲的定位方式也可以实现,具体要用那种定位方式要根据自己的 ...
- phpstorm2019激活码
6ZUMD7WWWU-eyJsaWNlbnNlSWQiOiI2WlVNRDdXV1dVIiwibGljZW5zZWVOYW1lIjoiSmV0cyBHcm91cCIsImFzc2lnbmVlTmFtZ ...
- NTP服务编译安装报错:ntpd.c:124:29: 致命错误:sys/capability.h:没有那个文件或目录
缺少libcap-devel [root@localhost libcap]# cd /mnt/ [root@localhost mnt]# rpm -Uvh libcap*
- 经典sql面试题(学生表_课程表_成绩表_教师表)
转载:https://www.cnblogs.com/qixuejia/p/3637735.html 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna ...
- auto_modify_ip Shell脚本安装
#!/bin/bash # : #This author is DKS #auto modify ip of linux ############################### IP_NAME ...