rest framework 認證 總結完結篇

2022-04-20 18:58:47 字數 1676 閱讀 5049

執行過程 apiview() ruquest() authentication() orderview()

apiview()

def duspatch:

self.initial(request)

def inital():

self.perform_authticate()

def perform_authticate():

request.user

ruquest()

@def user()

'''def _authenticate()

迴圈所有authenticatior物件

執行物件的authenticate方法

authentication 自定義認證類

def authenticate():

自定義認證類

-報錯 raise exceptions.authenticationfailed("使用者認證失敗")

-返回元組(user物件, token物件)

print(request.user, request.auth)

orderview

authentication_classes = [authtication,]

def get():

...def post()

...settinfs #全域性使用#在api目錄下建立個utils目錄 建立個auth.py檔案 將firstauthentication()新增

rest_framework =

區域性不使用

class authview(apiview):

authentication_class =

def get(self,request, *args, **kwargs):

pass

內建的認證類

authentication.py檔案中有以下幾個類 baseauthentication(object)

class baseauthentication(object):

def authenticate(self,request)

def authenticate_header(self, request) #認證失敗的話 給瀏覽器返回的相應頭餓

return 'basic realm="%s"' % self.www_authenticate_realm

basicauthentication(baseauthentication) #是瀏覽器實現的 瀏覽器將使用者和密碼發給伺服器

sessionauthntication(baseauthentication) #不用

tokenauthtication(baseauthntication) #不用

remoteuserauthentication(baseauthentication) #不用

暫時只用baseauthentication 建立類 繼承baseauthentication

from rest_framework.authentication import baseauthentication

class firstauthentication(baseauthentication):

def authenticate(self, request):

pass

#這樣就可以實現認證功能了

rest framework登入認證

class user models.model user models.charfield max length 32 pwd models.charfield max length 32 class usertoken models.model token models.charfield max...

rest framework認證元件

rest framework認證元件主要就是用來做登陸校驗 使用第一步建表 class user models.model username models.charfield max length 32 password models.charfield max length 32 user typ...

REST framework 五 認證元件

rest framework 的認證元件是在 apiview dispatch下執行的。跟著原始碼過了下,了解它的實現過程。上圖源 中的self.authenticators就是乙個認證元件類的乙個列表,我們在接下來後面的自定義認證元件時,就要注意這裡面的源代源內容,自定義類的類名可以自己起,而類中...