rest_framework.views.APIView -- Request 以及 用戶認證 源代碼分析

在講解源碼之前,先介紹一下 APIView和django中的View有什麼不同

  • APIView是REST framework提供的所有視圖的基類,繼承自Django的View父類。

APIView與View的不同之處在於:

  • 傳入到視圖方法中的是REST framework的Request對象,而不是Django的HttpRequeset對象;REST framework 提供了Parser解析器,在接收到請求後會自動根據Content-Type指明的請求數據類型(如JSON、表單等)將請求數據進行parse解析,解析爲類字典對象保存到Request對象中;
  • 視圖方法可以返回REST framework的Response對象,視圖會爲響應數據設置(render)符合前端要求的格式;
  • 任何APIException異常都會被捕獲到,並且處理成合適的響應信息;
    在進行dispatch()分發前,會對請求進行身份認證、權限檢查、流量控制。

好的,關於用戶認證的源代碼分析,我們現在從`dispatch()`這個函數開始分析:
 def dispatch(self, request, *args, **kwargs):
        """
        `.dispatch()` is pretty much the same as Django's regular dispatch,
        but with extra hooks for startup, finalize, and exception handling.
        """
        self.args = args
        self.kwargs = kwargs
--->  request = self.initialize_request(request, *args, **kwargs)
        self.request = request
        self.headers = self.default_response_headers  # deprecate?

        try:
 --->       self.initial(request, *args, **kwargs)

            # Get the appropriate handler method
            if request.method.lower() in self.http_method_names:
                handler = getattr(self, request.method.lower(),
                                  self.http_method_not_allowed)
            else:
                handler = self.http_method_not_allowed

            response = handler(request, *args, **kwargs)

        except Exception as exc:
            response = self.handle_exception(exc)

        self.response = self.finalize_response(request, response, *args, **kwargs)
        return self.response

這個函數首先是給了我們一個註釋:

.dispatch()`與Django的常規調度幾乎相同,
但有額外的鉤子,用於啓動,結束和異常的處理

在dispatch類方法中,在request執行get\post\put\delete\等方法時候,會先對request請求進行用戶驗證,也就是如下2行代碼:

1:request = self.initialize_request(request, *args, **kwargs)
2:self.initial(request, *args, **kwargs)

request = self.initialize_request(request, *args, **kwargs)

關於第一行代碼initialize_request,從字面意思來看,就是對請求來的request,進行再次初始化封裝,得到初始化後request,那就看看是如何進行初始化的:

    def initialize_request(self, request, *args, **kwargs):
        """
        Returns the initial request object.
        """
        parser_context = self.get_parser_context(request)

        return Request(
            request,
            parsers=self.get_parsers(),
            authenticators=self.get_authenticators(),
            negotiator=self.get_content_negotiator(),
            parser_context=parser_context
        )

嗯,這個函數裏面的註釋是驗證了剛纔的說法,這個是用來初始化請求對象的:

先分析這一段代碼parser_context = self.get_parser_context(request),我們可以通過這個函數的返回值看到,它是返回的一個字典類型的數據

def get_parser_context(self, http_request):
        """
        Returns a dict that is passed through to Parser.parse(),
        as the `parser_context` keyword argument.
        """
        # Note: Additionally `request` and `encoding` will also be added
        #       to the context by the Request object.
        return {
            'view': self,
            'args': getattr(self, 'args', ()),
            'kwargs': getattr(self, 'kwargs', {})
        }

在進行字典格式轉換之後,initialize_request方法最終返回一個django自己封裝的Request:

        return Request(
            request,
            parsers=self.get_parsers(),
            authenticators=self.get_authenticators(),
            negotiator=self.get_content_negotiator(),
            parser_context=parser_context
        )

來細看一下它這個Request在返回的時候,做了哪些事情:

1:parsers=self.get_parsers()是獲取解析器(用來解析通過get_parser_context構造的字典數據的)
在這裏插入圖片描述
在這裏插入圖片描述
2:parser_context=parser_context是構造好的原生request封裝的字典格式數據

3:negotiator=self.get_content_negotiator()一個內容協商類,它決定如何爲響應選擇一個呈現器,給定一個傳入請求 ;
在這裏插入圖片描述
在這裏插入圖片描述
4:authenticators=self.get_authenticators()是獲取 [auth() for auth in self.authentication_classes] 認證列表,裏面是存放的一個個認證類對象;
get_authenticators方法如下,最後跟代碼定位到如下鍵值對
在這裏插入圖片描述
在這裏插入圖片描述

我們可以先看一下父類BaseAuthentication的代碼,如下,裏面有2個方法,也就是進行認證,我們可以利用此來擴充校驗認證規則
在這裏插入圖片描述

get_authenticators方法就是去遍歷繼承類SessionAuthentication、BasicAuthentication的子類的列表,裏面是一個一個子類對象,因爲在列表推導是中[auth() for auth in self.authentication_classes]通過auth()獲取類的實例對象;(其實這裏面還有關於局部配置和全局配置,簡單理解就是:單純在某個類視圖裏面的配置,還是在settings.py中配置的,下一次會有進一步說明,get_authenticators會從自身找尋,查詢不到,在去全局配置中查找)。

好的request = self.initialize_request(request, *args, **kwargs)在此告一段落,現在開始說self.initial(request, *args, **kwargs)

self.initial(request, *args, **kwargs)

    def initial(self, request, *args, **kwargs):
        """
        Runs anything that needs to occur prior to calling the method handler.
        """
        self.format_kwarg = self.get_format_suffix(**kwargs)

        # Perform content negotiation and store the accepted info on the request
        neg = self.perform_content_negotiation(request)
        request.accepted_renderer, request.accepted_media_type = neg

        # Determine the API version, if versioning is in use.
        version, scheme = self.determine_version(request, *args, **kwargs)
        request.version, request.versioning_scheme = version, scheme

        # Ensure that the incoming request is permitted
        self.perform_authentication(request)
        self.check_permissions(request)
        self.check_throttles(request)

這個註釋挺好玩的:

在調用方法處理程序之前運行需要發生的任何事情。

我們來着重看一下我們需要了解的信息,也就是最後三段代碼的用戶認證、權限認證、訪問頻率控制(本篇不做說明):

1.self.perform_authentication(request)  認證

在這裏插入圖片描述
哈哈,真是驚訝,就一段代碼 request.user, 其實看到這裏,我們應該知道,這個user肯定是被@property裝飾過的,不然是不可能被直接調用而且不加任何括號;可以在DRF中找一下這個Request中帶有@property的user:

在這裏插入圖片描述

@property
    def user(self):
        """
        Returns the user associated with the current request, as authenticated
        by the authentication classes provided to the request.
        """
        #判斷當前類中是否有已經認證過的user
        if not hasattr(self, '_user'):
            #沒有認證則去認證
            self._authenticate()
        #認證過了直接返回
        return self._user

沒有認證的話,就需要調用Request類中的_authenticate()方法進行認證

def _authenticate(self):
        """
        Attempt to authenticate the request using each authentication instance
        in turn.
        """
        #遍歷request對象中封裝的Authentication對象
        for authenticator in self.authenticators:
            try:
                #調用Authentication對象中的authenticate方法,必須要有這個方法不然拋出異常
                #當然Authentication類一般有我們自己定義,實現這個方法就可以了
                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

        self._not_authenticated()
2.  self.check_permissions(request)  權限

在這裏插入圖片描述

#檢查權限
    def check_permissions(self, request):
        """
        Check if the request should be permitted.
        Raises an appropriate exception if the request is not permitted.
        """
        #self.get_permissions()得到的是一個權限對象列表
        for permission in self.get_permissions():
            #在自定義的Permission中has_permission方法是必須要有的
            #判斷當前has_permission返回的是True,False,還是拋出異常
            #如果是True則表示權限通過,False執行下面代碼
            if not permission.has_permission(request, self):
                #爲False的話則拋出異常,當然這個異常返回的提示信息是英文的,如果我們想讓他顯示我們自定義的提示信息
                #我們重寫permission_denied方法即可
                self.permission_denied(
                    #從自定義的Permission類中獲取message(權限錯誤提示信息),一般自定義的話都建議寫上,如果沒有則爲默認的(英文提示)
                    request, message=getattr(permission, 'message', None)
                )

#self.get_permissions()得到的是一個權限對象列表,如下圖
在這裏插入圖片描述
在這裏插入圖片描述

如果has_permission位False執行下面的代碼

def permission_denied(self, request, message=None):
        """
        If request is not permitted, determine what kind of exception to raise.
        """
        if request.authenticators and not request.successful_authenticator:
            #沒有登錄提示的錯誤信息
            raise exceptions.NotAuthenticated()
        #一般是登陸了但是沒有權限提示
        raise exceptions.PermissionDenied(detail=message)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章