DRF — source code analysis of DRF authentication function

Time:2020-11-24

1、 DRF authentication function source code analysis

1 APIView---》dispatch---》 self.initial (request, *args, **kwargs)--》 self.perform_ authentication(request)---》 Request.user --->self._ Authenticate (self): method of request class -- > self.authenticators When is the request class property passed in when the request object is instantiated? When dispatching self.get_ authenticators()--》return [auth() for auth in  self.authentication_ If authentication is written in the view class defined by yourself_ Classes = [class 1, class 2] --- request's self.authenticators It becomes the object of each class that we configure
        
        
2 self._ Authenticate (self): method of the request class
def _authenticate(self):
     for authenticator in  self.authenticators The objects of the classes configured in 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. Simply configure authentication in the view class_ classes = [ MyAuthen.LoginAuth ]
    The above method will be executed and authentication will be executed