1. 程式人生 > >rest_framework -- 認證元件

rest_framework -- 認證元件

#####認證元件#####

一、認證是什麼就不說了,某些網頁必須是使用者登陸之後,才能訪問的,所以這時候就需要用上認證元件。
    你不用rest_framework的認證元件也行,這種認證的話,完全可以自己寫出來。


二、之前再寫APIView的時候,那裡提到過。
    不記得在哪裡的話,先找dispatch方法(APIView的記得),然後是self.initial(request, *args, **kwargs),
    最後找到self.perform_authentication(request)。看看這個方法的原始碼:

        def perform_authentication(self, request):
            
""" Perform authentication on the incoming request. Note that if you override this and simply 'pass', then authentication will instead be performed lazily, the first time either `request.user` or `request.auth` is accessed. """ request.user
## 回憶之前,此時的request是rest_framework進行封裝後的request了,所以找到的話,就去rest_framework中Request去找。 ## 下面附上request.user這個方法的原始碼: @property def user(self): """ Returns the user associated with the current request, as authenticated by the authentication classes provided to the request.
""" if not hasattr(self, '_user'): with wrap_attributeerrors(): self._authenticate() return self._user ## 它通過property將一個方法裝飾成一個屬性,此時self是request物件(rest_framework的),經過if判斷,執行了self._authenticate() ## 那我們繼續去看這個方法是什麼,附上原始碼: def _authenticate(self): """ Attempt to authenticate the request using each authentication instance in turn. """ for authenticator in self.authenticators: 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 self._not_authenticated() ### 此時的self也還是request物件(rest_framework的),self.authenticators這個是什麼? # authenticators它是request的一個屬性,那麼我們在哪裡生成了這個request物件呢?我們回到APIView的dispatch方法,找到這行程式碼 # request = self.initialize_request(request, *args, **kwargs),有沒有印象,得到一個rest_framework的request物件, # 下面是self.initialize_request(request, *args, **kwargs)的原始碼: 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 ) authenticators=self.get_authenticators() ---->> authenticators是一個裝著物件的列表 那麼繼續看self.get_authenticators()這個方法到底做了些什麼,附上原始碼: def get_authenticators(self): """ Instantiates and returns the list of authenticators that this view can use. """ return [auth() for auth in self.authentication_classes] 返回的是一個列表,那麼self.authentication_classes應該就是列表(元組),此時self是檢視類的物件 ####重點:面向物件屬性的查詢順序,記住!! 方式一:我們可以在當前檢視類中寫一個authentication_classes的列表(元組),裡面裝著一個一個的類, 而這個類不是隨便的一個類,是進行認證驗證的類。 方式二:當前檢視類中沒有authentication_classes這個屬性,那麼便會去APIView中去找該屬性,肯定能APIView中能夠找到該屬性 authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES -->> api_settings它是一個物件 我們去看產生api_settings物件的類,其他的不說了,說說這段程式碼: @property def user_settings(self): if not hasattr(self, '_user_settings'): self._user_settings = getattr(settings, 'REST_FRAMEWORK', {}) return self._user_settings 這裡的setting是通過from django.core import settings 匯入的 大概意思是:如果django的settings檔案中有'REST_FRAMEWORK',那麼便會去那裡找DEFAULT_AUTHENTICATION_CLASSES這個屬性, 沒有的話,便會去rest_framework的settings檔案中找DEFAULT_AUTHENTICATION_CLASSES, 所以方式二可以這樣寫,在django的settings檔案中寫上這樣的程式碼 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES':[進行認證的類1,進行認證的類2], } 方式三:什麼都不寫,用rest_framework的settings檔案中的DEFAULT_AUTHENTICATION_CLASSES 好了,我們再回到self._authenticate()的原始碼來看,for迴圈一個裝著物件的列表,所以authenticator就是一個物件, user_auth_tuple = authenticator.authenticate(self) --->>> 執行該物件的方法,將返回值賦給user_auth_tuple, 我們使用前面的方式一,方式二,自己寫認證類的的話,那麼必須要有authenticate這個方法對吧,這個先放著, 我們先看方式三,我猜rest_framework的settings檔案中的DEFAULT_AUTHENTICATION_CLASSES裡的認證類中,也肯定有authenticate方法, 看看它是怎麼寫,我們跟著寫不就好了嘛? 地址:from rest_framework import authentication 看了下每個類中都有authenticate,傳來兩個引數,一個self,一個request,那我們自己寫的認證類也這樣寫。該方法的返回值將會賦值給user_auth_tuple, 繼續回到def _authenticate(self)這個方法中,繼續看,如果返回值user_auth_tuple為None的話,將會繼續for迴圈,返回值為True的話, 那麼這個返回值必須為一個元組,而且只能有兩個元素。執行for迴圈的過程中,authenticate這個方法沒有異常的話,那麼表示驗證成功。 總結:上面把認證的整個流程都寫了一般,那麼需要些的東西我列出來, 1、根據需求要求自己寫一個認證類,該類必須要有authenticate這個方法,繼承BaseAuthentication這個類 2、驗證通過的話,返回None或者兩個元素的元組(列表也行) 3、驗證不通過的話,拋異常,拋這個異常exceptions.APIException 4、假如只想當前檢視類中使用認證功能的話,那麼在當前檢視類中新增authentication_classes屬性 5、想全域性都想進行認證功能,就在django的settings檔案中新增 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES':[進行認證的類1,進行認證的類2], } 6、如果你既想全域性配置,但是某個區域性又不配置認證的話,那麼就是該檢視類中寫authentication_classes,值為[],就好了。 下面寫個登陸驗證的例子把:test頁面必須登陸之後才能訪問 models檔案: class User(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(max_length=32) password = models.CharField(max_length=32) class Token(models.Model): nid = models.AutoField(primary_key=True) token = models.UUIDField(max_length=64) user = models.OneToOneField(to='User') urls檔案: url(r'login/',views.Login.as_view()), url(r'test/',views.Test.as_view()), views檔案: class Login(APIView): def post(self, request, *args, **kwargs): response = {'status': 100, 'msg': '登陸成功'} name = request.data.get('name') password = request.data.get('password') try: user = models.User.objects.get(name=name, password=password) token = uuid.uuid4() ret = models.Token.objects.filter(user=user) if ret: models.Token.objects.filter(user=user).update(token=token) else: models.Token.objects.create(token=token, user=user) response['token'] = token except Exception: response['status'] = 101 response['msg'] = '使用者名稱或密碼錯誤' return JsonResponse(response) class Test(APIView): authentication_classes = [LoginAuth,] def get(self, request, *args, **kwargs): return HttpResponse('test get') auth_class.py檔案: class LoginAuth(BaseAuthentication): def authenticate(self, request): token = request.query_params.get('token', None) try: ret = models.Token.objects.get(token=token) except ObjectDoesNotExist: raise exceptions.APIException('請先進行登陸') return ret.user, ret 這裡推薦一個傳送各種請求的軟體,postman