DRF 认证权限

"""
必须是在继承 APIView  的视图下才会生效,请记住
"""
from rest_framework.authentication import SessionAuthentication,BaseAuthentication
from rest_framework.permissions import AllowAny,IsAdminUser,IsAuthenticated



# DRF配置信息
REST_FRAMEWORK = {
    #1,全局认证
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication', #此身份验证方案使用HTTP基本身份验证,用于测试使用
        'rest_framework.authentication.SessionAuthentication', #自己服务器认证用户
    ],

    #2,全局权限
    # 'DEFAULT_PERMISSION_CLASSES': [
    #     # 'rest_framework.permissions.IsAuthenticated', #普通用户
    #     # 'rest_framework.permissions.AllowAny', #所有用户
    #     'rest_framework.permissions.IsAdminUser', #管理员户
    # ],

    #3,全局限流
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.AnonRateThrottle', #匿名用户
        'rest_framework.throttling.UserRateThrottle' # 认证用户
    ],
    'DEFAULT_THROTTLE_RATES': {
        'anon': '2/minute',
        'user': '3/minute'
    }
}

#1,视图集
class NetworkInfoModelViewSet(ModelViewSet):
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoModelSerializer

    #1,局部认证
    # authentication_classes = [SessionAuthentication]

    #2,局部权限
    # permission_classes = [AllowAny]

    #3,局部限流
    throttle_classes = [AnonRateThrottle]
原文地址:https://www.cnblogs.com/zy09/p/15152995.html