一
請求到來以後,都要先執行dispatch方法,dispatch方法方法根據請求方式的不一樣觸發get/post/put/delete等方法api
注意:APIView中的dispatch方法有不少功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
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進行加工(添加數據) request = self.initialize_request(request, *args, **kwargs) self.request = request self.headers = self.default_response_headers |
二
上面是大體步驟,下面咱們來具體分析一下,看每一個步驟中都具體幹了什麼事restful
對request進行加工(添加數據)
咱們看看request裏面都添加了那些數據app
a
首先 request = self.initialize_request(request, *args, **kwargs)
點進去,會發現:在Request裏面多加了四個,以下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
def initialize_request(self, request, *args, **kwargs): """ Returns the initial request object. """ |
b
獲取認證相關的類的具體
authenticators=self.get_authenticators()
1 2 3 4 5 6
|
def get_authenticators(self): """ Instantiates and returns the list of authenticators that this view can use. """ |
c
查看認證的類:self.authentication_classes
1
|
authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES |
d
接着走進api_settings
1
|
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS) |
e
導入了類看看類裏面具體幹了什麼
1 2
|
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import BaseAuthentication
|
f
看到裏面有個authenticate方法和authenticate_header方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class BaseAuthentication(object): """ All authentication classes should extend BaseAuthentication. """
def authenticate(self, request): """ Authenticate the request and return a two-tuple of (user, token). """ raise NotImplementedError(".authenticate() must be overridden.")
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. """ pass
|
具體處理認證,從headers裏面能獲取用戶名和密碼ide
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
class BasicAuthentication(BaseAuthentication): """ HTTP Basic authentication against username/password. """ www_authenticate_realm = 'api'
def authenticate(self, request): """ Returns a `User` if a correct username and password have been supplied using HTTP Basic authentication. Otherwise returns `None`. """ auth = get_authorization_header(request).split()
if not auth or auth[0].lower() != b'basic': return None |
g
固然restfulframework默認定義了兩個類。咱們也能夠自定製類,
本身有就用本身的了,本身沒有就去找父類的了,
可是裏面必須實現authenticate方法,否則會報錯。
進行如下操做
- 處理版權信息
- 認證
- 權限
- 請求用戶進行訪問頻率的限制
咱們主要來看一下認證流程:函數
a
首先 self.initial(request, *args, **kwargs)能夠看到作了如下操做
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
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)
|
b
咱們先來看認證,self.perform_authentication(request)
具體幹了什麼,按住ctrl點擊進去
1 2 3 4 5 6 7 8 9
|
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 |
c
那麼咱們能夠從視圖裏面導入一下Request,找到request對象的user方法
1
|
from rest_framework.views import Request
|
1 2 3 4 5 6 7 8 9 10
|
@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() |
d
執行self._authenticate() 開始用戶認證,
若是驗證成功後返回元組: (用戶,用戶Token)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
def _authenticate(self): """ Attempt to authenticate the request using each authentication instance in turn. """ |
e
在user_auth_tuple = authenticator.authenticate(self) 進行驗證,
若是驗證成功,執行類裏的authenticatie方法
f
若是用戶沒有認證成功:self._not_authenticated()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
def _not_authenticated(self): """ Set authenticator, user & authtoken representing an unauthenticated request.
Defaults are None, AnonymousUser & None. """ |
執行get/post/delete等方法
對返回結果在進行加工