当前位置:网站首页>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_CLASSESSet 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_classesProperty 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 :
403Authority is forbidden 、401Uncertified . 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
getAccess read :IsAuthenticatedOrReadOnly
2.1 Global permissions
- The default permission policy can use
DEFAULT_PERMISSION_CLASSESSet 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_CLASSESandDEFAULT_THROTTLE_RATESThe default current limiting policy will be set globally , secondsecond, branchminute、 whenhour、 GoddayAs 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)
边栏推荐
- JVM garbage collection detailed learning notes (II)
- 个人力扣题目分类记录
- Output a spiral matrix C language
- Interpretation of MySQL optimization principle
- Locust performance test 2 (interface request)
- systemd
- 2022-06-30 Unity核心8——模型导入
- Count the number of words C language
- What is the value of getting a PMP certificate?
- 【Istio Network CRD VirtualService、Envoyfilter】
猜你喜欢
随机推荐
MySql数据库-事务-学习笔记
C language pointer (exercises)
LeetCode 736. LISP syntax parsing
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
Simulation volume leetcode [general] 1609 Parity tree
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
Two schemes of unit test
2021 year end summary
个人力扣题目分类记录
端口复用和重映像
Regularly modify the system time of the computer
Count the number of words C language
[istio introduction, architecture, components]
C语言指针(习题篇)
H3C vxlan configuration
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
PMP Exam Preparation experience systematically improve project management knowledge through learning
Expérience de port série - simple réception et réception de données
Full link voltage test of the e-commerce campaign Guide
UnityShader入门精要个人总结--基础篇(一)









