Django Rest Framework --認證

基本結構

urls.pypython

from django.conf.urls import url, include
from web.viewsimport TestView

urlpatterns = [
    url(r'^test/', TestView.as_view()),
]

 views.pyweb

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):
        """
        用戶認證代碼邏輯編寫區
        """
    if 驗證成功:
        return ('登陸用戶', '用戶token')
    else 驗證失敗
        #拋出驗證失敗異常
        raise exceptions.AuthenticationFailed('用戶認證失敗')
    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, ]

    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請求,響應內容')

  

 注意事項:django

  1.使用認證類api

    在須要認證的視圖類中,寫入authentication_classes = [TestAuthentication, ],變量屬性authentication_classes是個列表,列表中可寫多個認證類,經過認證方纔執行視圖類的方法。(此方法屬於局部的類的認證方式)ide

  2.返回值函數

    (1)元組形式(登陸用戶,用戶token),元組中第一個元素是用戶名,第二個元素是認證數據token。這個返回值會在咱們的視圖類中經過request.user 和 request.auth獲取到。在實際使用中能夠傳遞你本身須要使用的數據。源碼分析

    (2)none,跳過當前認證類,處理下個認證類。post

    (3)拋出異常,父類接收異常,返回錯誤。url

源碼分析

  (1)尋找函數入口spa

    經過urls.py文件,咱們首先要尋找TestView類的as_view()方法,咱們能在APIView類找到as_view()方法,APIView繼承了View中as_view()方法,返回了一個view函數最終的結果就是調用了dispatch方法,整個視圖類的入口就找到了。

   (2)爲何要使用authentication_classes 屬性變量?

    如今咱們開始尋找dispatch方法,這時候請注意,咱們應該從子類TestView中開始尋找這個方法,由於在子類中可能會重構父類的dispatch方法,最後咱們在APIView類中找到了dispatch方法。

 

由initialize_request方法的實現過程能夠看出,將其封裝實例化成了一個Request對象。而authenticators屬性就是認證屬性。

   

從上圖能夠看出authentication_classes是一個列表,而且這個列表中的每一個元素都是一個個認證類,因此咱們會在須要認證的視圖類頂部將authentication_classes賦值,也能夠在配置文件中全局配置(下面會說如何進行全局認證),咱們新封裝的request對象中authenticators屬性就是由一個個認證類實例組成的列表。

  (3)爲何要認證類中要使用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)」的操做,那天然而然,咱們定義的認證類中要實現這個方法了。

   (4)爲何認證成功後的返回值在request.user和request.auth中?

    由 (3)中最後一個圖可知,當咱們認證成功後會執行「self.user, self.auth = user_auth_tuple」代碼,咱們在認證類定義的方法authenticate的返回值就保存在 user_auth_tuple中,因此咱們經過request.user 和 request.auth 就能夠獲取到了。

 實例

1 from django.conf.urls import url, include
2 from web.viewsimport TestView
3 
4 urlpatterns = [
5     url(r'^test/', TestView.as_view()),
6 ]
urls
 1 from rest_framework.views import APIView
 2 from rest_framework.response import Response
 3 from rest_framework.authentication import BaseAuthentication
 4 from rest_framework.request import Request
 5 from rest_framework import exceptions
 6 
 7 token_list = [
 8     'sfsfss123kuf3j123',
 9     'asijnfowerkkf9812',
10 ]
11 
12 
13 class TestAuthentication(BaseAuthentication):
14     def authenticate(self, request):
15         """
16         用戶認證,若是驗證成功後返回元組: (用戶,用戶Token)
17         :param request: 
18         :return: 
19             None,表示跳過該驗證;
20                 若是跳過了全部認證,默認用戶和Token和使用配置文件進行設置
21                 self._authenticator = None
22                 if api_settings.UNAUTHENTICATED_USER:
23                     self.user = api_settings.UNAUTHENTICATED_USER()
24                 else:
25                     self.user = None
26         
27                 if api_settings.UNAUTHENTICATED_TOKEN:
28                     self.auth = api_settings.UNAUTHENTICATED_TOKEN()
29                 else:
30                     self.auth = None
31             (user,token)表示驗證經過並設置用戶名和Token;
32             AuthenticationFailed異常
33         """
34         val = request.query_params.get('token')
35         if val not in token_list:
36             raise exceptions.AuthenticationFailed("用戶認證失敗")
37 
38         return ('登陸用戶', '用戶token')
39 
40     def authenticate_header(self, request):
41         """
42         Return a string to be used as the value of the `WWW-Authenticate`
43         header in a `401 Unauthenticated` response, or `None` if the
44         authentication scheme should return `403 Permission Denied` responses.
45         """
46         # 驗證失敗時,返回的響應頭WWW-Authenticate對應的值
47         pass
48 
49 
50 class TestView(APIView):
51     authentication_classes = [TestAuthentication, ]
52     permission_classes = []
53 
54     def get(self, request, *args, **kwargs):
55         print(request.user)
56         print(request.auth)
57         return Response('GET請求,響應內容')
58 
59     def post(self, request, *args, **kwargs):
60         return Response('POST請求,響應內容')
61 
62     def put(self, request, *args, **kwargs):
63         return Response('PUT請求,響應內容')
Views

全局配置

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

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES" :['api.utils.auth.MyAuthentication]    
}

  MyAuthentication類就是咱們寫在utils文件夾下auth.py文件中的認證類。

  注意:若是有部分類不須要認證的話,能夠在這裏類中添加「authentication_classes = []」,便可。

相關文章
相關標籤/搜索