1. 程式人生 > >Django Rest Framework之認證

Django Rest Framework之認證

結構 params 這就是 header 情況 繼續 拋出異常 列表生成式 相對

代碼基本結構

  url.py:

from django.conf.urls import url, include
from web.views.s1_api import TestView
 
urlpatterns = [
    url(r‘^test/‘, TestView.as_view()),
]

  views.py:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import BaseAuthentication
from rest_framework.request import Request
from rest_framework import exceptions


class TestAuthentication(BaseAuthentication):
    def authenticate(self, request):
    ‘‘‘
    認證代碼編寫區域
    ‘‘‘
    return (用戶,用戶Token)

    def authenticate_header(self, request):
        # 驗證失敗時,返回的響應頭WWW-Authenticate對應的值
        pass  


class TestView(APIView):
    authentication_classes = [TestAuthentication, ]  

    def get(self, request, *args, **kwargs):
        pass

    
     def post(self, request, *args, **kwargs):
        pass

        ‘‘‘
        等等一系列的視圖功能方法
        ‘‘‘

  說明:

    1)在authenticate方法的返回值是一個元組,元組中第一個元素是用戶名,第二個元素是認證數據token。這個返回值會在我們的視圖類中通     過request.user 和 request.auth獲取到。具體為什麽是這兩個值,會在後面的源碼分析中說明。

    2)認證的返回值有三種情況:

      返回元組(如(1)中所述):認證成功

      返回None:處理下一個認證類

      拋出異常:認證失敗

    3)上面的基本結構是做局部的類的認證方式,如果相對絕大多數的類做認證,那麽可以通過全局認證的方式實現。該方法在下文中介紹。

    4)authentication_classes 屬性變量是一個列表,列表元素是類,一般情況只使用一個認證類。     

源碼分析

  1) 為什麽要使用authentication_classes 屬性變量?

 技術分享圖片 技術分享圖片

  python 的面向對象編程中,我們首先要執行的方法肯定是dispatch方法,所以我們的分析入口就是dispatch方法,在dispatch方法中,可以看到,通過initialize_request方法將django原生的request進行了一次封裝。由initialize_request方法的實現過程可以看出,將其封裝實例化成了一個Request對象。而authenticators屬性就是認證屬性。

 技術分享圖片

技術分享圖片

  通過查看get_authenticators方法,可以知道,它的返回值是一個列表生成式,而這個列表生成式中所用的就是我們在認證類中賦值authenticatin_classes屬性變量。在查找該變量的定義位置,就看到了它是通過settings配置文件來實現賦值的,除非,在子類中將其賦值。我們的代碼就是這樣做的。同時,也可以看出,我們可以修改settings配置文件來為全局定義認證規則。

2)為什麽要認證類中要使用authenticate方法?

技術分享圖片

技術分享圖片

  回到前面說是的dispatch方法來,在做完了對django原生的request的封裝和實例化後,緊接著就會開始認證(try...中,捕獲異常,如果沒有捕獲到異常,說明認證成功,就會繼續執行下面的反射過程)。認證的過程就包含在上圖中的inital方法中,有圖可知,是通過perform_authentication方法實現的認證。

技術分享圖片

技術分享圖片

  在perform_authentication方法中可以看到,只調用了一個request.user,而這個user一定是方法,不會是屬性變量,因為如果是屬性變量,那麽就一定有語法錯誤,變量一定是要賦值的,不可能孤零零的寫到那裏。我們在源碼中找到它,就明白了,之所以它能這麽寫,就是因為有了property裝飾器。在user方法中找到_authenticate方法,這就是認證的方法。

 技術分享圖片

  在這個方法中,一切答案都就找到了。首先看authenticators,是不是很眼熟,沒錯它就是前面說的,封裝和實例化原生request的Request類中所定義的屬性變量。在實例化時,我們就將authentication_classes列表的值通過get_authenticators方法中的列表生成式賦值給了authenticators。再往下看,authenticator.autheneicate(self)中的authenticator是不是就是我們自己定義的認證類,而它在源碼中要做“.authenticate(self)”的操作,那自然而然,我們定義的認證類中要實現這個方法了。

  3)為什麽認證成功後的返回值在request.user和request.auth中?

  由 2)中最後一個圖可知,當我們認證成功後會執行“self.user, self.auth = user_auth_tuple”代碼,我們在認證類定義的方法authenticate的返回值就保存在 user_auth_tuple中,所以我們通過request.user 和 request.auth 就可以獲取到了。

實例

技術分享圖片
from django.conf.urls import url, include
from web.viewsimport TestView

urlpatterns = [
    url(r^test/, TestView.as_view()),
]
urls.py 技術分享圖片
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import BaseAuthentication
from rest_framework.request import Request
from rest_framework import exceptions

token_list = [
    sfsfss123kuf3j123,
    asijnfowerkkf9812,
]


class TestAuthentication(BaseAuthentication):
    def authenticate(self, request):
        """
        用戶認證,如果驗證成功後返回元組: (用戶,用戶Token)
        :param request: 
        :return: 
            None,表示跳過該驗證;
                如果跳過了所有認證,默認用戶和Token和使用配置文件進行設置
                self._authenticator = None
                if api_settings.UNAUTHENTICATED_USER:
                    self.user = api_settings.UNAUTHENTICATED_USER()
                else:
                    self.user = None
        
                if api_settings.UNAUTHENTICATED_TOKEN:
                    self.auth = api_settings.UNAUTHENTICATED_TOKEN()
                else:
                    self.auth = None
            (user,token)表示驗證通過並設置用戶名和Token;
            AuthenticationFailed異常
        """
        val = request.query_params.get(token)
        if val not in token_list:
            raise exceptions.AuthenticationFailed("用戶認證失敗")

        return (登錄用戶, 用戶token)

    def authenticate_header(self, request):
        """
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        """
        # 驗證失敗時,返回的響應頭WWW-Authenticate對應的值
        pass


class TestView(APIView):
    authentication_classes = [TestAuthentication, ]
    permission_classes = []

    def get(self, request, *args, **kwargs):
        print(request.user)
        print(request.auth)
        return Response(GET請求,響應內容)

    def post(self, request, *args, **kwargs):
        return Response(POST請求,響應內容)

    def put(self, request, *args, **kwargs):
        return Response(PUT請求,響應內容)
Views.py

擴展:全局認證

  如果要進行全局配置,由上面的源碼分析可知,我們只需要在配置文件中配置我們存儲到authentication_classes的值即可。但還要註意的是,在寫配置文件時,要使用的是路徑,所以最好在和views.py同級目錄下新建一個文件夾(我習慣叫utils),再在該文件夾下新建一個認證文件(auth.py),將我們的認證類都寫到這裏。

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES" :[‘api.utils.auth‘,]     
}

註意:如果有部分類不需要認證的話,可以在這裏類中添加“authentication_classes = []”,即可。

Django Rest Framework之認證