做编程网站有哪些方面,做网站的图片素材网站有哪些,建筑设计方案汇报ppt,wordpress搜索安全在DRF中#xff0c;限流发生在认证、权限之后#xff0c;限流组件的使用步骤#xff1a; 1、编写自定义限流类#xff1b; 2、在settings.py中配置redis#xff1b; 3、安装django-redis; 4、启动redis服务#xff1b; 5、局部应用#xff0c;一般是在核心的视图中使用限流发生在认证、权限之后限流组件的使用步骤 1、编写自定义限流类 2、在settings.py中配置redis 3、安装django-redis; 4、启动redis服务 5、局部应用一般是在核心的视图中使用不会全局使用。限流组件的应用案例如下
一、自定义限流类,throttle.py设计了 2个限流类一个是针对匿名用户的限流匿名用户的唯一标识选择IP地址一个针对登录用户的限流登录用户的唯一标识是用户名。
from rest_framework.throttling import SimpleRateThrottle
from django.core.cache import cache as default_cache# 限流组件匿名用户访问没有登录的用户肯定是没有user的直接获取IP地址
class IpThrottle(SimpleRateThrottle):scope ip# 局部配置一分钟访问10次也可以配置到全局# THROTTLE_RATES {ip: 10/m}cache default_cache # default_cache 会读取配置文件中redis缓存的配置def get_cache_key(self, request, view):# 获取请求用户的IP地址去request中找请求头ident self.get_ident(request)return self.cache_format % {scope: self.scope, ident: ident}# 限流组件用户限流类
class UserThrottle(SimpleRateThrottle):scope user# 局部配置一分钟访问5次也可以配置到全局# THROTTLE_RATES {user: 5/m}cache default_cache # default_cache 会读取配置文件中redis缓存的配置def get_cache_key(self, request, view):ident request.user.pk #用户IDreturn self.cache_format % {scope: self.scope, ident: ident}
二、全局配置settings.py
REST_FRAMEWORK {# 限流全局配置DEFAULT_THROTTLE_RATES:{ip:10/m,user:5/m,}
}
三、 局部应用views.py
from ext.throttle import IpThrottle,UserThrottleclass LoginView(APIView):# login页面不需要认证就可以登录所以单独设置为空authentication_classes []permission_classes []# 应用限流组件,使用IP限流throttle_classes [IpThrottle,]def post(self,request):# 1、接收用户提交的用户名和密码user request.data.get(username)pwd request.data.get(password)# 2、数据库校验user_object models.UserInfo.objects.filter(usernameuser,passwordpwd).first()if not user_object:return Response({status:False,msg:用户名或者密码错误})# 用户名密码正确为用户生产tokentoken str(uuid.uuid4())user_object.token tokenuser_object.save()return Response({status:True,msg:登录成功,token:token})class AvatarView(NbApiView):# 老板或者员工可以访问permission_classes [UserPermission,BossPermission]# 对登录用户使用登录用户限流throttle_classes [UserThrottle,]def get(self,request):return Response({status:True,data:[11,22,33,44]})