Django restframework 框架認(rèn)證、權(quán)限、限流用法示例
本文實(shí)例講述了Django restframework 框架認(rèn)證、權(quán)限、限流用法。分享給大家供大家參考,具體如下:
概述
Django Rest Framework 是一個(gè)強(qiáng)大且靈活的工具包,使用Django REST Framework可以在Django的基礎(chǔ)上迅速實(shí)現(xiàn)API,用以構(gòu)建Web API。
認(rèn)證Authentication
可以在配置文件中配置全局默認(rèn)的認(rèn)證方案
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication', # 基本認(rèn)證
'rest_framework.authentication.SessionAuthentication', # session認(rèn)證
)
}
也可以在每個(gè)視圖中通過(guò)設(shè)置authentication_classess屬性來(lái)設(shè)置
from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.views import APIView class ExampleView(APIView): authentication_classes = (SessionAuthentication, BasicAuthentication) ...
認(rèn)證失敗會(huì)有兩種可能的返回值:
- 401 Unauthorized 未認(rèn)證
- 403 Permission Denied 權(quán)限被禁止
權(quán)限Permissions
權(quán)限控制可以限制用戶對(duì)于視圖的訪問(wèn)和對(duì)于具體數(shù)據(jù)對(duì)象的訪問(wèn)。
- 在執(zhí)行視圖的dispatch()方法前,會(huì)先進(jìn)行視圖訪問(wèn)權(quán)限的判斷
- 在通過(guò)get_object()獲取具體對(duì)象時(shí),會(huì)進(jìn)行對(duì)象訪問(wèn)權(quán)限的判斷
使用
可以在配置文件中設(shè)置默認(rèn)的權(quán)限管理類,如
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
如果未指明,則采用如下默認(rèn)配置
'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.AllowAny', )
也可以在具體的視圖中通過(guò)permission_classes屬性來(lái)設(shè)置,如
from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView class ExampleView(APIView): permission_classes = (IsAuthenticated,) ...
提供的權(quán)限
- AllowAny 允許所有用戶
- IsAuthenticated 僅通過(guò)認(rèn)證的用戶
- IsAdminUser 僅管理員用戶
- IsAuthenticatedOrReadOnly 認(rèn)證的用戶可以完全操作,否則只能get讀取
舉例
from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.generics import RetrieveAPIView class BookDetailView(RetrieveAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoSerializer authentication_classes = [SessionAuthentication] permission_classes = [IsAuthenticated]
自定義權(quán)限
如需自定義權(quán)限,需繼承rest_framework.permissions.BasePermission父類,并實(shí)現(xiàn)以下兩個(gè)任何一個(gè)方法或全部
- .has_permission(self, request, view)
是否可以訪問(wèn)視圖, view表示當(dāng)前視圖對(duì)象
- .has_object_permission(self, request, view, obj)
是否可以訪問(wèn)數(shù)據(jù)對(duì)象, view表示當(dāng)前視圖, obj為數(shù)據(jù)對(duì)象
例如:
class MyPermission(BasePermission): def has_object_permission(self, request, view, obj): """控制對(duì)obj對(duì)象的訪問(wèn)權(quán)限,此案例決絕所有對(duì)對(duì)象的訪問(wèn)""" return False class BookInfoViewSet(ModelViewSet): queryset = BookInfo.objects.all() serializer_class = BookInfoSerializer permission_classes = [IsAuthenticated, MyPermission]
限流Throttling
可以對(duì)接口訪問(wèn)的頻次進(jìn)行限制,以減輕服務(wù)器壓力。
使用
可以在配置文件中,使用DEFAULT_THROTTLE_CLASSES 和 DEFAULT_THROTTLE_RATES進(jìn)行全局配置,
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
DEFAULT_THROTTLE_RATES 可以使用 second, minute, hour 或day來(lái)指明周期。
也可以在具體視圖中通過(guò)throttle_classess屬性來(lái)配置,如
from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView class ExampleView(APIView): throttle_classes = (UserRateThrottle,) ...
可選限流類
1) AnonRateThrottle
限制所有匿名未認(rèn)證用戶,使用IP區(qū)分用戶。
使用DEFAULT_THROTTLE_RATES['anon'] 來(lái)設(shè)置頻次
2)UserRateThrottle
限制認(rèn)證用戶,使用User id 來(lái)區(qū)分。
使用DEFAULT_THROTTLE_RATES['user'] 來(lái)設(shè)置頻次
3)ScopedRateThrottle
限制用戶對(duì)于每個(gè)視圖的訪問(wèn)頻次,使用ip或user id。
例如:
class ContactListView(APIView):
throttle_scope = 'contacts'
...
class ContactDetailView(APIView):
throttle_scope = 'contacts'
...
class UploadView(APIView):
throttle_scope = 'uploads'
...
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.ScopedRateThrottle',
),
'DEFAULT_THROTTLE_RATES': {
'contacts': '1000/day',
'uploads': '20/day'
}
}
實(shí)例
from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.generics import RetrieveAPIView from rest_framework.throttling import UserRateThrottle class BookDetailView(RetrieveAPIView): queryset = BookInfo.objects.all() serializer_class = BookInfoSerializer authentication_classes = [SessionAuthentication] permission_classes = [IsAuthenticated] throttle_classes = (UserRateThrottle,)
希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python各種語(yǔ)言間時(shí)間的轉(zhuǎn)化實(shí)現(xiàn)代碼
這篇文章主要介紹了python各種語(yǔ)言間時(shí)間的轉(zhuǎn)化,需要的朋友可以參考下2016-03-03
Python接口測(cè)試get請(qǐng)求過(guò)程詳解

