(一)、认证
全局配置:在settings.py文件中
REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': [# 此身份认证方案使用HTTP基本身份认证,验证请求是否是HTTP方式过来的'rest_framework.authentication.BasicAuthentication',# 自己服务器用户认证,即通过session来判断用户是否是自己服务器的'rest_framework.authentication.SessionAuthentication']}
局部配置:在视图中
class UserExtraActionViewSet(ModelViewSet):''' 视图集额外的动作 '''queryset = UserProfile.objects.all()serializer_class = UserProfileSerializerslookup_url_kwarg = 'id'# 局部认证authentication_classes = [SessionAuthentication]# 通过创建时间来查询用户信息@action(methods=["GET"], detail=False)def createTiem(self, request):starttime = datetime.date(2020, 11, 11)endtime = datetime.date(2020, 11, 13)user = UserProfile.objects.filter(date_joined__range=[starttime, endtime])serializer = self.get_serializer(instance=user, many=True)return Response(serializer.data)
-
二、权限
权限分类:
- AllowAny:允许任何人访问
- IsAuthenticated:允许被认证的用户访问,没有认证的拒绝访问
- IsAdminUser:允许管理员用户访问
- IsAuthenticatedOrReadOnly:允许被认证的用户访问任何请求,但是没有被认证的用户只能访问get请求获取部分查询
全局设置:在settings.py文件中设置
REST_FRAMEWORK = {'DEFAULT_PERMISSION_CLASSES': [# 'rest_framework.permissions.AllowAny', # 所有用户# 'rest_framework.permissions.IsAuthenticated', # 普通用户'rest_framework.permissions.IsAdminUser', # 管理员用户]}
局部设置:在视图中设置
class UserExtraActionViewSet(ModelViewSet):''' 视图集额外的动作 '''queryset = UserProfile.objects.all()serializer_class = UserProfileSerializerslookup_url_kwarg = 'id'# 局部认证authentication_classes = [SessionAuthentication]# 局部权限设置permission_classes = [AllowAny]# 通过创建时间来查询用户信息@action(methods=["GET"], detail=False)def createTiem(self, request):starttime = datetime.date(2020, 11, 11)endtime = datetime.date(2020, 11, 13)user = UserProfile.objects.filter(date_joined__range=[starttime, endtime])serializer = self.get_serializer(instance=user, many=True)return Response(serializer.data)
-
(三)、限流
(一)、基础设置
drf只是提供了基础的限流设置,如果需要复杂的或者具体业务场景的限流则需要自定义限流中间件了
- 分类:
- UserRateThrottle:针对认证用户进行限流
- AnonRateThrottle:对非认证用户限流
- 全局设置:可以设置天、时、分、秒
```python
REST_FRAMEWORK = {
‘DEFAULT_THROTTLE_CLASSES’: [
], ‘DEFAULT_THROTTLE_RATES’: {'rest_framework.throttling.AnonRateThrottle','rest_framework.throttling.UserRateThrottle'
} }'anon': '4/minute','user': '10/minute'
- 局部设置:在视图中设置```pythonclass UserExtraActionViewSet(ModelViewSet):''' 视图集额外的动作 '''queryset = UserProfile.objects.all()serializer_class = UserProfileSerializerslookup_url_kwarg = 'id'# 局部认证authentication_classes = [SessionAuthentication]# 权限设置permission_classes = [AllowAny]# 限流设置throttle_classes = [AnonRateThrottle]# 通过创建时间来查询用户信息@action(methods=["GET"], detail=False)def createTiem(self, request):starttime = datetime.date(2020, 11, 11)endtime = datetime.date(2020, 11, 13)user = UserProfile.objects.filter(date_joined__range=[starttime, endtime])serializer = self.get_serializer(instance=user, many=True)return Response(serializer.data)
(二)、可选限流
可选限流相对与基础限流稍加灵活一点,可以做部分自定义限流
- 操作:
- 模块是rest_framework.throttling.ScopedRateThrottle
- 可以限流的种类不在局限于anon和user,而是可以自定义不定数量的限流种类
全局设置
REST_FRAMEWORK = {'DEFAULT_THROTTLE_CLASSES': ['rest_framework.throttling.ScopedRateThrottle',],'DEFAULT_THROTTLE_RATES': {'contacts': '1000/day','uploads': '20/day','downloads': '3/minute',......}}
局部设置:在视图中使用,throttle_scopes对应的值是settings.py中自定义的限流的名字 ```python from rest_framework.throttling import ScopedRateThrottle
class UserExtraActionViewSet(ModelViewSet): ‘’’ 视图集额外的动作 ‘’’ queryset = UserProfile.objects.all() serializer_class = UserProfileSerializers lookup_url_kwarg = ‘id’
# 限流设置throttle_scopes = 'downloads'
```
