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, ] 就會執行上面的方法,執行認證