drf三大认证组件

三大认证

源码入口

# drf的APIView类中的dispatch方法中的 self.initial(request, *args, **kwargs)

img

跳转到initial方法

img

 

认证组件

点击perform_authentication方法

img

user其实是用property装饰器装饰的方法属性

而且这里是request.user

request是drf重新封装的request,并且是一个对象

进入request.user方法

现在是Request类中,所有self都是Request类的实例化

img

 

点击进入self._authenticate方法中

img

self.authenticators 属性是在实例化时传入

Request实例化

img

 

在APIView中的dispatch方法中Request已经进行了实例化

现在在APIView类中

img

进入initialize_request方法

img

点击进入get_authenticators方法

img

返回一个列表,列表套对象

正好解释了在Request类中_authenticate方法,为什么要进行遍历取值

 

我们点击permission_classes进去看看是哪些类

img

看到是这个,说明我们可以进行全局配置,也可以进行局部配置

 

进入settings文件

img

我们进authentication中看看SessionAuthentication和BasicAuthentication这两个类

在request类中的_authenticate方法中,遍历出来的对象都是SessionAuthentication和BasicAuthentication这两个类的实例化,都调用了authenticate方法,而且传入了self参数

img

authenticate方法需要传入两个参数,一个self,一个为request

 

BasicAuthentication的authenticate方法

img

没有这些属性,直接返回None

SessionAuthentication的authenticate方法

img

没有user属性,返回None

在Response中

img

认证直接通过

大致走一遍认证组件生命周期,后期都会自定义认证组件

 

原文地址:https://www.cnblogs.com/asdaa/p/11690575.html