五:认证、权限、频率源码流程

使用rest_framework,无论用哪种视图,最终都走向APIview下的dispatch:

  1、dispatch的执行意味着新的request的生成,post数据用data方法取,_request获取wsgi的原生request。

     2、生成新的request后进行各种校验,认证,权限、频率

   3、校验通过后才能得到最终返回的数据

  进入验证部分:

  首先看认证:

  此时request为dispatch重构的request:

  进入重构方法:

  进入Request找到了user方法:

  进入到_authenticate()方法:

  找到anthenticators:谁调的user,self就是谁,它是新的request调的

  重构request时实例化的:

 

 

authentication_classes的值由api_settings决定

 

所以self.authenticators就是APIview下的authentication_classes经过get_authenticators方法转换成一个存放类对象的列表

所以authentication_classes接收一个列表,我们实现的视图继承APIview,在CBV中可以覆盖它,自己来实现具体的需求。

知道了self.authenticators是谁继续回到user方法中的_authenticate,

所以,如果自己实现认证类时,需要在CBV中覆盖authentication_classes的值,值为认证类,认证类中需要实现authenticate方法,权限与频率基本一致。

原文地址:https://www.cnblogs.com/aizhinong/p/12548224.html