当前位置:网站首页>DRF authentication, permissions, and flow restrictions (only for views in DRF)
DRF authentication, permissions, and flow restrictions (only for views in DRF)
2022-07-07 09:11:00 【FOR. GET】
One 、 authentication Authentication
Authentication needs to be used in combination with permissions !Authentication Official configuration file
1.1 Global authentication
- Use
DEFAULT_AUTHENTICATION_CLASSES
Set the global default authentication scheme
# settings.py
# REST_FRAMEWORK DRF All configurations in are written in this
REST_FRAMEWORK = {
# Configure global authentication scheme
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication', # Basic authentication
'rest_framework.authentication.SessionAuthentication',# session authentication
)
}
1.2 Partial Certification
- Set it separately in the view
authentication_classes
Property to set , The view class that needs authentication can be written in the required view class .
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
class ExampleView(APIView):
# Set authentication
authentication_classes = (SessionAuthentication, BasicAuthentication)
# Set the permissions
permission_classes = (IsAuthenticated,)
def get(self, request, format=None):
content = {
'user': unicode(request.user), # `django.contrib.auth.User` example .
'auth': unicode(request.auth), # None
}
return Response(content)
The return values of authentication failure are :
403
Authority is forbidden 、401
Uncertified . General authentication can use global authentication .
Two 、 jurisdiction Permissions
jurisdiction Permissions Official documents , The permissions provided are :
- Allow all users :
AllowAny
- Only authenticated users :
isAuthenicated
- Only administrator users :
isAdminUser
- Authenticated users can fully operate , Otherwise, we can only
get
Access read :IsAuthenticatedOrReadOnly
2.1 Global permissions
- The default permission policy can use
DEFAULT_PERMISSION_CLASSES
Set global settings .
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
# Only authenticated users can access
'rest_framework.permissions.IsAuthenticated',
)
}
- If not specified , This setting defaults to allow unrestricted access :
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
2.2 Local permissions
from rest_framework.permissions import IsAuthenticated
class ExampleView(APIView):
# Set the permissions
permission_classes = (IsAuthenticated,)
Authentication is generally used globally , Permissions are generally used locally
3、 ... and 、 Current limiting Shrottling
Limit the frequency of interface access , To reduce server pressure . Current limiting Shrottling Official address
3.1 Global current limiting
Use
DEFAULT_THROTTLE_CLASSES
andDEFAULT_THROTTLE_RATES
The default current limiting policy will be set globally , secondsecond
, branchminute
、 whenhour
、 Godday
As the current limiting period
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day', # Anonymous users
'user': '1000/day' # The logged in user
}
}
3.2 Local current limiting
- Based on APIView View of class , You can set the current limiting policy on a per view or per view set basis
from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
class ExampleView(APIView):
throttle_classes = (UserRateThrottle,)
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
边栏推荐
- Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
- Serial port experiment - simple data sending and receiving
- 面试题:高速PCB一般布局、布线原则
- Port multiplexing and re imaging
- 数据在内存中的存储
- Implement custom memory allocator
- Digital triangle model acwing 275 Pass a note
- C语言指针(习题篇)
- C语言指针(上篇)
- Locust performance test 2 (interface request)
猜你喜欢
使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题
【Istio Network CRD VirtualService、Envoyfilter】
Calf problem
PMP Exam details after the release of the new exam outline
C语言指针(下篇)
Summary of PMP learning materials
Output all composite numbers between 6 and 1000
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
Ppt template and material download website (pure dry goods, recommended Collection)
How long does the PMP usually need to prepare for the exam in advance?
随机推荐
Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
2022-06-30 unity core 8 - model import
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
NVIC interrupt priority management
OpenGL三维图形绘制
Simulation volume leetcode [general] 1706 Where does the ball meet
Reading notes of pyramid principle
How to use Arthas to view class variable values
Implement custom memory allocator
Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
外部中断实现按键实验
[istio introduction, architecture, components]
Synchronized underlying principle, volatile keyword analysis
Common short chain design methods
C language pointer (special article)
STM32串口寄存器库函数配置方法
2022-07-06 unity core 9 - 3D animation
【ChaosBlade:根据标签删除POD、Pod 域名访问异常场景、Pod 文件系统 I/O 故障场景】