前言:
Django的rest_framework元件的功能很強大,今天來我來給大家剖析一下認證元件
下面進入正文分析,我們從視圖開始,一步一步來剖析認證元件
1、進入urls檔案
url(r'^login/', views.LoginCBV.as_view(),name="login"),
2、然後執行LoginCBV這個類的as_view方法
3、LoginCBV這個類是我們自己的寫的,但是LoginCBV類根本沒有寫as_view這個方法,那麼我們該怎麼辦? 此時我們應該去找LoginCBV的父類,看父類是否as_view方法
4、先确認LoginCBV這個類的父類,很清楚,我們看到LoginCBV這個類的父類是APIView這個類
class LoginCBV(APIView):
5、下面我們去APIView這個類中查找是否有as_view這個方法,我們在APIView這個類中找到了as_view方法,這個被classmethod修飾符修飾,也就是說這個方法是一個類方法,由一個類本身就可以調用這個方法,這個時候大家在會議一下,在urls檔案中,是不是一個類在調用as_view方法。
如果大家都classmethod這個修飾符不清楚,可以看下我的這篇部落格:https://www.cnblogs.com/bainianminguo/p/10475204.html
@classmethod
def as_view(cls, **initkwargs):
6、下面我們來具體看下as_view這個方法,到底做了什麼事情?下面是方法的源碼
@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)
我們來重點看下需要我們知道的,首先這個函數的傳回值是一個view方法

接着我們看下view這個方法,從這裡我們可以看到,view就是執行APIView父類的as_view方法,下面我們接着去找APIView類的父類的as_view方法
7、進入APIView父類中,我們看到APIView類的父類是View
class APIView(View):
8、進入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
下面我們來分析這個方法的源碼,方法的傳回值是view這個函數,而view這個函數的傳回值是self.dispatch這個方法
9、下面我們首先要找到self.dispatch這個方法,然後在看下這個方法到底幹了什麼?
這個self到底是哪個類的執行個體呢?我們來梳理一下子類和父類的關系
LoginCBV【類】------->APIView【類】------->View【類】------>view【方法】-----》dispatch【方法】
那麼我們就需要先從LoginCBV這個類中找dispatch方法,發現沒有找到,然後繼續找LoginCBV這個類的父類,也就是APIView這個類,看這個類是否dispatch方法
10、我們最終在APIView這個類中找到了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
這個方法很重要,我們來看下
首先rest_framework處理後的request
然後看下self.initialize.request方法幹了什麼,當然找這個方法到底在是哪個類的方法,也是要按照之前我們找dispatch方法的一樣,我這裡就直接找到這個方法了,self.initialize.request這個方法是APIView這個類的方法
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
)
這個函數傳回一個Request的執行個體對象,然後我們在看下Request這個類的,Request類的源碼如下
class Request(object):
"""
Wrapper allowing to enhance a standard `HttpRequest` instance.
Kwargs:
- request(HttpRequest). The original request instance.
- parsers_classes(list/tuple). The parsers to use for parsing the
request content.
- authentication_classes(list/tuple). The authentications used to try
authenticating the request's user.
"""
def __init__(self, request, parsers=None, authenticators=None,
negotiator=None, parser_context=None):
assert isinstance(request, HttpRequest), (
'The `request` argument must be an instance of '
'`django.http.HttpRequest`, not `{}.{}`.'
.format(request.__class__.__module__, request.__class__.__name__)
)
self._request = request
self.parsers = parsers or ()
self.authenticators = authenticators or ()
self.negotiator = negotiator or self._default_negotiator()
self.parser_context = parser_context
self._data = Empty
self._files = Empty
self._full_data = Empty
self._content_type = Empty
self._stream = Empty
if self.parser_context is None:
self.parser_context = {}
self.parser_context['request'] = self
self.parser_context['encoding'] = request.encoding or settings.DEFAULT_CHARSET
force_user = getattr(request, '_force_auth_user', None)
force_token = getattr(request, '_force_auth_token', None)
if force_user is not None or force_token is not None:
forced_auth = ForcedAuthentication(force_user, force_token)
self.authenticators = (forced_auth,)
不知道大家是否明白這段代碼的意思,如果authenticators為真,則self.authenticators等于authenticators,如果authenticators為假,則self.authenticators等于一個空的元組
self.authenticators = authenticators or ()
我們這裡要看下執行個體化Request這個類的時候,authenticators這個參數傳遞的是什麼?
我們在回到initlize_request方法的傳回值,下面我們要來看下self.get_authenticators()方法是在做什麼
下面看下self.get_authenticators()這個方法的源碼,從字面的我們就可以了解,self.authentication_classes是一個認證的類的清單。auth()是每個類的執行個體對象,這個方法的傳回值就是清單,清單中的元素就是每個認證類的執行個體對象,這裡先劇透一下,authentication_class這個屬性是由我們自己的配置的
def get_authenticators(self):
"""
Instantiates and returns the list of authenticators that this view can use.
"""
return [auth() for auth in self.authentication_classes]
到這裡,APIView類中的dispatch方法的initialize_request條線就做完了,就是給我們傳回了一個新的Request類的執行個體,這個執行個體的authenticators就包括我們認證元件相關的類的執行個體對象
下面我們繼續往下走APIView類的dispatch方法,走self.initial方法
11、下面先看下initial方法的源碼
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_authentication方法。隻傳回一個request.user
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、莫名其妙,傳回一個執行個體的屬性?其實這裡大家不要忘記了,如果一個類的方法被property修飾了,調用這個方法的就可以使用屬性的方式調用,而不用加括号了,如果大家不清楚,可以看我這篇部落格:https://www.cnblogs.com/bainianminguo/p/9950607.html
14、下面我們看下request.user到底是什麼?我們先要知道request是什麼?看下面的截圖
在dispatch方法中initial方法的參數有一個request,而這個request就是initialize_request的傳回值,而initialize_request的傳回值就是Request的執行個體對象
這個request有一個user的屬性或者方法,我們下面來找下
15、下面我們來看下request.user到底是個什麼東西?我們在Request類中确實找到了user這個方法,這個方法也被property裝飾器裝飾了,是以也印證了我們之前的猜測了,request.user是一個被property修飾的方法
@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、然後看下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()
如果符合規範,則傳回None,如果不符合規範,則raise抛出錯誤
到這裡,我們就認證元件的源碼梳理完了,下面我們來看下具體怎麼寫認證元件
17、下面進入如何寫認證元件
我們的類中要有這麼一個屬性。
然後認證元件的類中要
18、做認證,我們是通過一個token來做的,每次使用者登陸,我們都會給他重新一個token,然後把這個token告訴客戶,下次客戶來通路帶着token,我們就認為認證通過了
是以我們先設計表,一個model表,一個Token表,兩張表是一對一的關系
class User(models.Model):
name = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
class Token(models.Model):
user = models.OneToOneField(to=User)
token = models.CharField(max_length=128)
19、然後我們寫使用者登陸的處理邏輯
from django.http import JsonResponse
class LoginCBV(APIView):
def get(self,request):
pass
def post(self,request):
name = request.data.get("name")
pwd = request.data.get("pwd")
obj = models.User.objects.filter(name=name,pwd=pwd).exists()
res = {"code":200,"message":"","token":""}
if obj:
user_obj = models.User.objects.filter(name=name,pwd=pwd).first()
token = create_token(name)
models.Token.objects.update_or_create(user_obj,defaults={"token":token})
token_obj = models.Token.objects.get(user=user_obj)
res["token"] = token_obj.token
else:
res["code"] = 201
res["message"] = "使用者名或者密碼錯誤"
import json
return JsonResponse(json.dumps(res))
上面的update_or_create的方法寫錯了,正确的寫法是下面的寫法
models.Token.objects.update_or_create(user=user_obj,defaults={"token":token})
20、這裡還寫了一個生成token的函數,加鹽的鹽為使用者的名稱
利用時間和使用者的名稱計算出來一個md5值,作為這次登陸的token
import hashlib
import time
def create_token(user):
now = time.time()
test_md5 = hashlib.md5(bytes(str(now),encoding="utf-8"))
test_md5.update(bytes(user,encoding="utf-8"))
return test_md5.hexdigest()
21、下面我們開始寫的認證元件,如果我們想控制通路這條url:
url(r'^book_cbv/', views.Book_cbv.as_view(),name="test3"),
22、那麼我們就需要進入Book_cbv這個類中來做操作,這個屬性我們之前也看到了,名稱必須是authentication,且值要為一個list
23、最後我們下Book_auther這個類
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
24、最後我們用postman測試一下,首先先用post登陸一下,生成一下token
然後看下Token表中是否有token字段
25、我們再次用postman登入一下,看下token是否會更新,我們看到token已經更新
26、我們首先先不攜帶token去通路book表,看下效果,提示驗證失敗
27、下面我們攜帶token去通路,這樣就可以傳回查詢到的結果了
大家要慢慢的體會