当前位置:网站首页>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)
边栏推荐
- How to count the number of project code lines
- Simulation volume leetcode [general] 1567 Length of the longest subarray whose product is a positive number
- Systick tick timer
- JVM 垃圾回收 详细学习笔记(二)
- PMP Exam Preparation experience, seek common ground while reserving differences, and successfully pass the exam
- 数据在内存中的存储
- 串口實驗——簡單數據收發
- Original collection of hardware bear (updated on June 2022)
- STM32 serial port register library function configuration method
- 模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
猜你喜欢
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
PMP examination experience sharing
PMP Exam Preparation experience systematically improve project management knowledge through learning
What is the use of PMP certificate?
【Istio Network CRD VirtualService、Envoyfilter】
C language pointer (Part 2)
UnityShader入门精要个人总结--基础篇(一)
C语言指针(中篇)
外部中断实现按键实验
JVM 垃圾回收 详细学习笔记(二)
随机推荐
【Istio Network CRD VirtualService、Envoyfilter】
徽商期货公司评级是多少?开户安全吗?我想开户,可以吗?
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
Why is access to the external network prohibited for internal services of the company?
Count the number of words C language
Led analog and digital dimming
Panel display technology: LCD and OLED
Selenium mouse sliding operation event
Interview question: general layout and wiring principles of high-speed PCB
PMP Exam details after the release of the new exam outline
Several stages of PMP preparation study
How to pass the PMP Exam in a short time?
C language pointer (Part 2)
Platformization, a fulcrum of strong chain complementing chain
How to use Arthas to view class variable values
Analysis of Hessian serialization principle
C语言指针(习题篇)
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c