url.py:python
from django.conf.urls import url, include from web.views.s1_api import TestView urlpatterns = [ url(r'^test/', TestView.as_view()), ]
views.py:web
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獲取到。具體爲何是這兩個值,會在後面的源碼分析中說明。django
2)認證的返回值有三種狀況:編程
返回元組(如(1)中所述):認證成功api
返回None:處理下一個認證類ide
拋出異常:認證失敗源碼分析
3)上面的基本結構是作局部的類的認證方式,若是相對絕大多數的類作認證,那麼能夠經過全局認證的方式實現。該方法在下文中介紹。post
4)authentication_classes 屬性變量是一個列表,列表元素是類,通常狀況只使用一個認證類。 url
python 的面向對象編程中,咱們首先要執行的方法確定是dispatch方法,因此咱們的分析入口就是dispatch方法,在dispatch方法中,能夠看到,經過initialize_request方法將django原生的request進行了一次封裝。由initialize_request方法的實現過程能夠看出,將其封裝實例化成了一個Request對象。而authenticators屬性就是認證屬性。spa
經過查看get_authenticators方法,能夠知道,它的返回值是一個列表生成式,而這個列表生成式中所用的就是咱們在認證類中賦值authenticatin_classes屬性變量。在查找該變量的定義位置,就看到了它是經過settings配置文件來實現賦值的,除非,在子類中將其賦值。咱們的代碼就是這樣作的。同時,也能夠看出,咱們能夠修改settings配置文件來爲全局定義認證規則。
回到前面說是的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)」的操做,那天然而然,咱們定義的認證類中要實現這個方法了。
由 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()), ]
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請求,響應內容')
若是要進行全局配置,由上面的源碼分析可知,咱們只須要在配置文件中配置咱們存儲到authentication_classes的值便可。但還要注意的是,在寫配置文件時,要使用的是路徑,因此最好在和views.py同級目錄下新建一個文件夾(我習慣叫utils),再在該文件夾下新建一個認證文件(auth.py),將咱們的認證類都寫到這裏。
REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES" :['api.utils.auth.MyAuthentication] }
MyAuthentication類就是咱們寫在utils文件夾下auth.py文件中的認證類。
注意:若是有部分類不須要認證的話,能夠在這裏類中添加「authentication_classes = []」,便可。