resr_framework-访问频率控制

作用

  对访问用户进行访问次数限制,防止恶意访问对服务器造成的压力。

基本使用

创建访问频率控制类

1 from rest_framework.throttling import SimpleRateThrottle
2 
3 
4 class VisitThrottle(SimpleRateThrottle):
5 
6     scope = "xxx"
7 
8     def get_cache_key(self, request, view):
9         return self.get_ident(request)

全局应用

 1 REST_FRAMEWORK = {
 2     # 指定全局认证类
 3     "DEFAULT_AUTHENTICATION_CLASSES":["app01.utils.authclasses.Myauth",],
 4     "UNAUTHENTICATED_USER":None,# 指定匿名用户:request.user = None
 5     "UNAUTHENTICATED_TOKEN":None,# 指定未认证用户:request.auth = None
 6     'DEFAULT_PERMISSION_CLASSES':['app01.utils.premissionclasses.Mypermission'],
 7     "DEFAULT_THROTTLE_CLASSES": ["app01.utils.throttle.VisitThrottle", ],
 8     "DEFAULT_THROTTLE_RATES": {
 9         "xxx": "5/m",# 根据节流控制类中的配置
10     }
11 }

猜你喜欢

转载自www.cnblogs.com/liuyinzhou/p/9418792.html