drf——drf认证功能源码分析

1、drf认证功能源码分析源码分析

1 APIView---》dispatch---》self.initial(request, *args, **kwargs)--》self.perform_authentication(request)
---》Request.user--->self._authenticate(self):Request类的方法---》self.authenticators:Request类的属性---》在Request对象实例化的时候传入的
----》Request在何时实例化的?dispatch的时候
---》APIView:self.get_authenticators()--》return [auth() for auth in self.authentication_classes]
----》若是在本身定义的视图类中写了authentication_classes=[类1,类2]----》Request的self.authenticators就变成了咱们配置的一个个类的对象 2 self._authenticate(self):Request类的方法 def _authenticate(self): for authenticator in self.authenticators: # BookView中配置的一个个类的对象 try: user_auth_tuple = authenticator.authenticate(self) 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 3 只要在视图类中配置authentication_classes = [MyAuthen.LoginAuth, ] 就会执行上面的方法,执行认证