目錄:
- 基礎配置
- 路由配置
- 傳遞方式
一、基礎配置
1、安裝rest_framework.authtoken
說明:在settings.py的文件中INSTALLED_APPS
# settings.py INSTALLED_APPS = ( ... 'rest_framework.authtoken' ) #遷入數據 >python manage.py migrate
2、全局配置和局部配置
說明:如果是全局配置的話,需要在 settings.py文件中配置,但是如果是局部配置,就要在具體的視圖中配置。這個根據自己的需求來配置。
# settings.py配置 全局配置:表示對所有視圖有效 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.TokenAuthentication', ), } # 局部配置 :只對當前視圖有效 from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import BasicAuthentication from rest_framework.authentication import TokenAuthentication class XXXX(APIView): authentication_classes = [BasicAuthentication,TokenAuthentication,SessionAuthentication]
3、全局配置單個不配置
說明:我們可能遇到這種情況,就是全局需要配置,但是我只是單單某個接口不需要認證。那咋辦吶。那我就情況認證列表。
class CartView(APIView): #局部的 # 基於什么登錄認證的 authentication_classes = [] # 如果全局配置了,但是這個視圖不需要驗證,就authentication_classes 變成空列表 # 只有登錄才能訪問 permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): .....
二、 路由配置
我們配置token驗證的時候,還有配置路由,驗證登錄之后,產生token值,后面其他接口 拿到 這個token 值放在 請求 header中,去校驗。
在根級路由或者子路由中配置,這個你自己看,我們這邊是根級路由配置
from django.contrib import admin from django.urls import path, include from rest_framework.authtoken import views #設置路由,必須導入view urlpatterns = [ path('admin/', admin.site.urls), path('api/', include('app05.urls')), path('api-token-auth/', views.obtain_auth_token) #token哪里來,我們這邊還需要配置一個路由,請求生成token ]
我們來看一下 obtain_auth_token 源碼:Ctrl + obtain_auth_token 看一下:
obtain_auth_token => ObtainAuthToken
obtain_auth_token這個類自動幫我們生成一個token。
三、傳遞方式
3.1、視圖編寫
說明:編寫Views.py文件,既然我們需要token驗證,所以就需要導入token認證
from rest_framework.views import APIView from django.http import JsonResponse from rest_framework.authentication import BasicAuthentication, TokenAuthentication from rest_framework.permissions import IsAuthenticated # Create your views here. class CartView(APIView): # 基於什么登錄認證的 authentication_classes = [BasicAuthentication, TokenAuthentication] #支持token認證 # 只有登錄才能訪問 permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): .... return JsonResponse(ctx)
3.2、傳遞方式
封裝到請求頭中,已下面的格式 Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
缺點:token值在分布式系統中會有問題產生,並且沒有過期時間,一旦被竊取,任何人都可以使用。
傳遞步驟:
1、POST方法:請求http://127.0.0.1:8000/api-token-auth/ 獲取token值。
2、我們看下 TokenAuthentication 的源碼,看如何 傳入 token值的。=> Ctrl + TokenAuthentication
class TokenAuthentication(BaseAuthentication): """ Simple token based authentication. Clients should authenticate by passing the token key in the "Authorization" HTTP header, prepended with the string "Token ". For example: Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a #傳token格式 """ .....
很明顯: 傳token的格式: Authorization: Token Toekn值。
3、我們訪問GET請求:http://127.0.0.1:8000/api/cart/
說明:我們在請求header中:傳入 Authorization: Token 877025d8e30491fbb00558c334d308f1cc7142d7(http://127.0.0.1:8000/api-token-auth/請求生成的token值)
4、請求錯誤的token值