Django的rest_framework的認證元件之全域性設定的原始碼解析
前言:
在我的上一篇部落格我介紹了一下單獨為某條url設定認證,但是如果我們想對所有的url設定認證,該怎麼做呢?我們這篇部落格就是給大家介紹一下在Rest_framework中如何實現全域性的設定認證元件的功能。下面就請大家跟著我的思路看部落格
如果有對區域性設定不清楚的,可以看我的上一篇部落格,原始碼級的分析單獨設定Rest_framework的認證元件:https://www.cnblogs.com/bainianminguo/p/10480887.html
正文:
我們在走一步流程
1、進入urls路由檔案
url(r'^login/', views.LoginCBV.as_view(),name="login"),
2、進入 as_view 這個方法,這個方法被類直接呼叫,那麼這個方法一定會被 classmethod 修飾符修飾,是一個類方法
@classmethod def as_view(cls, **initkwargs): """ Store the original class on the view function. This allows us to discover information about the view when we do URL reverse lookups.Used for breadcrumb generation. """ if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet): def force_evaluation(): raise RuntimeError( 'Do not evaluate the `.queryset` attribute directly, ' 'as the result will be cached and reused between requests. ' 'Use `.all()` or call `.get_queryset()` instead.' ) cls.queryset._fetch_all = force_evaluation view = super(APIView, cls).as_view(**initkwargs) view.cls = cls view.initkwargs = initkwargs # Note: session based authentication is explicitly CSRF validated, # all other authentication is CSRF exempt. return csrf_exempt(view)
3、 as_view 這個方法,我們看到返回值為view的方法的返回值,而 view 這個方法又是什麼?我們在 as_view 方法中看到這樣一段程式碼,就是執行 父類的as_view的方法
view = super(APIView, cls).as_view(**initkwargs)
4、進入APIView的父類的as_view方法,也就是 View類的as_view 方法
@classonlymethod def as_view(cls, **initkwargs): """ Main entry point for a request-response process. """ for key in initkwargs: if key in cls.http_method_names: raise TypeError("You tried to pass in the %s method name as a " "keyword argument to %s(). Don't do that." % (key, cls.__name__)) if not hasattr(cls, key): raise TypeError("%s() received an invalid keyword %r. as_view " "only accepts arguments that are already " "attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.request = request self.args = args self.kwargs = kwargs return self.dispatch(request, *args, **kwargs) view.view_class = cls view.view_initkwargs = initkwargs # take name and docstring from class update_wrapper(view, cls, updated=()) # and possible attributes set by decorators # like csrf_exempt from dispatch update_wrapper(view, cls.dispatch, assigned=()) return view
5、下面重點來分析View類的as_view方法,這個as_view方法返回是一個view方法的執行的結果,而view方法又幹了什麼,我們看下view方法,這個方法返回的是 dispatch方法
def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.request = request self.args = args self.kwargs = kwargs return self.dispatch(request, *args, **kwargs)
6、下面我們看下dispatch方法,這個 dispatch方法是APIView這個類 的方法
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
7、先看這裡,將源生的request進行初始化
8、看下 initialize_reques t方法,這個方法返回了一個 新的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 )
9、因為我們這裡在做認證的原始碼分析,我們重點看下authenticators這個屬性的,也就是 get_authenticators方法 ,這裡要非常的注意,這裡非常的關鍵,就是有 self.authentication_classes 這個屬性
def get_authenticators(self): """ Instantiates and returns the list of authenticators that this view can use. """ return [auth() for auth in self.authentication_classes]
重點是這個屬性,大家一定要記住
10、下面我們接著步驟7在往後執行,看下 initial 這個方法
11、進入這個initial這個方法,這裡有3個元件,認證,許可權,頻率,我們重點看認證這個元件
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)
12、看下認證元件的方法 perform_authenticatio n這個方法,返回一個 request.user 這個,request是什麼,我們看到在執行initial方法的時候,傳了一個request進去,這個request就是 request.user的這個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
13、我們在匯過去看下inital方法傳遞的引數request,我們看到 initial方法的request是initalize_request方法 執行的結果
14、下面看下initalize_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 )
15、進入Request類,我們看下的user屬性或者方法,看程式碼,發現這是一個 被propery修飾過的方法,呼叫這個方法的方法和呼叫屬性的方法一樣
@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
16、Request這個類,我們看了下沒有_user這個屬性,所以會進入if的條件語句,下面我們看下 _authenticat e方法
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()
17、這個方法有些引數大家可能也不清楚,我們在回答一下,先看下 authenticators,由於這個self是Request類的 一個例項物件
for authenticator in self.authenticators:
我們看下例項化 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 )
我們很清楚的可以看到authenticators這個值就是 get_authenticator s這個方法的返回值,我們在看這個方法
def get_authenticators(self): """ Instantiates and returns the list of authenticators that this view can use. """ return [auth() for auth in self.authentication_classe
authentication_classes 就是我們自己的配置的認證類
18、在看下authenticator.authenticate這個方法
19、然後這個時候就可以看下面我們的認證配置
首先定義了一個認證類,這個認證類有 authenticate方法,這個方法的返回值為一個元組,我已經圈起來來
class Book_auther(BaseAuthentication): def authenticate(self,request): token = request.GET.get("token") token_obj = models.Token.objects.filter(token=token).first() if token_obj: return token_obj.user.name,token_obj.token else: raise exceptions.AuthenticationFailed("驗證失敗") def authenticate_header(self,request): pass
然後在我們自己的檢視類中定義了認證的類的列表,例項化我們的認證類
class Book_cbv(APIView): authentication_classes = [Book_auther,] def get(self,request): query_list = models.Book.objects.all() # bs = book_serializers(query_list,many=True) bs = bookmodelserializer(query_list,many=True,context={'request': request}) return Response(bs.data) def post(self,request): bs = bookmodelserializer(data=request.data) print(request.data) if bs.is_valid(): print(bs.validated_data) bs.save() return Response(bs.data) else: return Response(bs.errors)
20、這個時候,我們才能正式進入認證類的全域性配置的地方,做全域性配置,我們當然不能在每個檢視類中配置,那麼如果我們不配置這個authentication_classes這個屬性呢?
其實APIView預設是有這個引數,如果我們沒有配置,則用APIView這個類的屬性
21、看到這句程式碼大家可能不懂,如果看不懂,大家可以下我的這篇部落格,介紹面向物件的 __getattr__ 方法的作用
部落格地址:https://www.cnblogs.com/bainianminguo/p/10475204.html
authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
22、下面我們進入api_settings這個例項,我們看到api_settings這個是APISettings類的例項物件
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
23、然後我們在看下APISettings這個類
class APISettings(object): """ A settings object, that allows API settings to be accessed as properties. For example: from rest_framework.settings import api_settings print(api_settings.DEFAULT_RENDERER_CLASSES) Any setting with string import paths will be automatically resolved and return the class, rather than the string literal. """ def __init__(self, user_settings=None, defaults=None, import_strings=None): if user_settings: self._user_settings = self.__check_user_settings(user_settings) self.defaults = defaults or DEFAULTS self.import_strings = import_strings or IMPORT_STRINGS self._cached_attrs = set()
24、看了我的部落格,就會知道__getattr__這個方法的使用場景,也就知道下面這段程式碼實際就會執行APISettings類的__getattr__方法
authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
25、下面我們看下APISettings類的__getattr__方法
def __getattr__(self, attr): if attr not in self.defaults: raise AttributeError("Invalid API setting: '%s'" % attr) try: # Check if present in user settings val = self.user_settings[attr] except KeyError: # Fall back to defaults val = self.defaults[attr] # Coerce import strings into classes if attr in self.import_strings: val = perform_import(val, attr) # Cache the result self._cached_attrs.add(attr) setattr(self, attr, val) return val
在看下user_settings是是否有DEFAULT_AUTHENTICATION_CLASSES這個k值
然後看user_settings這個方法
下面就是在例項化APISettings類的時候程式碼,第一個引數user_settings,這個引數的值None
所以APISettings這個類的例項物件沒有_user_settings這個屬性,所以會進入if的流程中
這個settings是什麼呢?其實就是Djaong的project的settings檔案
所以我們就需要在settings中配置“REST_FRAMEWORK”這個屬性的值為一個字典,我們看到後面如果拿不到“REST_FRAMEWORK”就會給賦值給空的字典
26、但是到了這裡,我們字典該怎麼寫呢?大家一臉的懵逼了,我們可以看下這段程式碼,如果我們在settings沒有配置“REST_FRAMEWORK”就會走下面的流程,我們看下面的流程的是什麼東西
27、self.defaults是什麼,就是我們在例項時候APISetings這個類的時候傳遞的引數
在來回憶一下,例項化APISettings類
在看下初始化APISettings類的時候__init__方法
我們看下DEFAULTS是什麼,就是下面的配置
我們模仿上面寫我們的REST_FRAMEWORK
REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES":( "app1.utils.Book_auther", ) }
28、我們使用postman進行測試
先使用get的方式訪問book_cbv
然後使用post方式訪問book_cbv
最後使用post訪問book_detail_cbv
最後我們加上token在訪問一次,訪問了2個url,均可以訪問成功
至此Rest_framework全域性設定認證元件的原始碼剖析和實現我們多講完了,大家請檢視,並給出意見