Django DRF限流组件

复制代码

在DRF中,限流发生在认证、权限之后,限流组件的使用步骤: 1、编写自定义限流类; 2、在settings.py中配置redis; 3、安装django-redis; 4、启动redis服务; 5、局部应用,一般是在核心的视图中使用,不会全局使用。限流组件的应用案例如下:

一、自定义限流类,throttle.py,设计了 2个限流类,一个是针对匿名用户的限流,匿名用户的唯一标识选择IP地址;一个针对登录用户的限流,登录用户的唯一标识是用户名。

python 复制代码
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  #用户ID
        return self.cache_format % {'scope': self.scope, 'ident': ident}

二、全局配置,settings.py

python 复制代码
REST_FRAMEWORK = {
    # 限流全局配置
    "DEFAULT_THROTTLE_RATES":{
        "ip":"10/m",
        "user":"5/m",
    }
}

三、 局部应用,views.py

python 复制代码
from ext.throttle import IpThrottle,UserThrottle



class 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(username=user,password=pwd).first()
        if not user_object:
            return Response({"status":False,"msg":"用户名或者密码错误"})
        # 用户名密码正确为用户生产token
        token = str(uuid.uuid4())
        user_object.token = token
        user_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]})
相关推荐
Penge6665 小时前
Go 接口编译期断言
后端
我是一颗柠檬5 小时前
【MySQL全面教学】MySQL面试高频考点汇总Day15(2026年)
数据库·后端·mysql·面试
星空椰6 小时前
Python 面向对象高级:继承与类定义详解
开发语言·python
拽着尾巴的鱼儿6 小时前
springboot openfeign 自定义feign 接口重试机制
java·spring boot·后端
Ceelog6 小时前
久坐党自救指南:屏幕前 8 小时,身体到底在经历什么
前端·后端
凯瑟琳.奥古斯特6 小时前
高阶子查询题目精炼
开发语言·数据库·python·职场和发展·数据库开发
风之所往_6 小时前
Python 3.4 新特性全面总结
python
太阳上的雨天7 小时前
任何格式的文件转Markdown
python·ai
XS0301067 小时前
并发编程 六
java·后端
yaoxin5211237 小时前
419. 现代 Java IO 最佳实践 - 写入文本文件
java·windows·python