Django restframework 框架認(rèn)證、權(quán)限、限流用法示例
本文實例講述了Django restframework 框架認(rèn)證、權(quán)限、限流用法。分享給大家供大家參考,具體如下:
概述
Django Rest Framework 是一個強(qiáng)大且靈活的工具包,使用Django REST Framework可以在Django的基礎(chǔ)上迅速實現(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)證
)
}
也可以在每個視圖中通過設(shè)置authentication_classess屬性來設(shè)置
from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.views import APIView class ExampleView(APIView): authentication_classes = (SessionAuthentication, BasicAuthentication) ...
認(rèn)證失敗會有兩種可能的返回值:
- 401 Unauthorized 未認(rèn)證
- 403 Permission Denied 權(quán)限被禁止
權(quán)限Permissions
權(quán)限控制可以限制用戶對于視圖的訪問和對于具體數(shù)據(jù)對象的訪問。
- 在執(zhí)行視圖的dispatch()方法前,會先進(jìn)行視圖訪問權(quán)限的判斷
- 在通過get_object()獲取具體對象時,會進(jì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', )
也可以在具體的視圖中通過permission_classes屬性來設(shè)置,如
from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView class ExampleView(APIView): permission_classes = (IsAuthenticated,) ...
提供的權(quán)限
- AllowAny 允許所有用戶
- IsAuthenticated 僅通過認(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父類,并實現(xiàn)以下兩個任何一個方法或全部
- .has_permission(self, request, view)
是否可以訪問視圖, view表示當(dāng)前視圖對象
- .has_object_permission(self, request, view, obj)
是否可以訪問數(shù)據(jù)對象, view表示當(dāng)前視圖, obj為數(shù)據(jù)對象
例如:
class MyPermission(BasePermission): def has_object_permission(self, request, view, obj): """控制對obj對象的訪問權(quán)限,此案例決絕所有對對象的訪問""" return False class BookInfoViewSet(ModelViewSet): queryset = BookInfo.objects.all() serializer_class = BookInfoSerializer permission_classes = [IsAuthenticated, MyPermission]
限流Throttling
可以對接口訪問的頻次進(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來指明周期。
也可以在具體視圖中通過throttle_classess屬性來配置,如
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'] 來設(shè)置頻次
2)UserRateThrottle
限制認(rèn)證用戶,使用User id 來區(qū)分。
使用DEFAULT_THROTTLE_RATES['user'] 來設(shè)置頻次
3)ScopedRateThrottle
限制用戶對于每個視圖的訪問頻次,使用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'
}
}
實例
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,)
希望本文所述對大家基于Django框架的Python程序設(shè)計有所幫助。
相關(guān)文章
python各種語言間時間的轉(zhuǎn)化實現(xiàn)代碼
這篇文章主要介紹了python各種語言間時間的轉(zhuǎn)化,需要的朋友可以參考下2016-03-03

