zoukankan      html  css  js  c++  java
  • drf 频率类

      

      频率类源码

        入口:

    入口:
    
        1)APIView的dispath方法的 self.initial(request,*args,**keargs)点进去
        2) self。check_throttles(request)进行频率认证
    
    
    
        def check_throttles(self, request):
            """
            Check if request should be throttled.
            Raises an appropriate exception if the request is throttled.
            """
            throttle_durations = []
            # 遍历配置的频率认证类,初始化得到一个个频率认证类对象,会调用频率认证类的__init__方法
            # 频率认证类的对象调用allow_request方法,判断是否限次(没有限次可访问,限次不可访问)
            # 频率认证类对象在限次后调用wait方法,获取还需等待多长时间可以进行下一次访问
            # 注:频率认证组件都是继承SimpleRateThrottle类
            for throttle in self.get_throttles():
                if not throttle.allow_request(request, self):
                    throttle_durations.append(throttle.wait())
                # 只要频率限制了,allow_request 返回False了,才会调用wait
            if throttle_durations:
    
                durations = [
                    duration for duration in throttle_durations
                    if duration is not None
                ]
    
                duration = max(durations, default=None)
                self.throttled(request, duration)
            

    自定义频率类 

        1) 自定义一个继承SimpleRateThrottle 类 的频率类
        2)设置一个scope 类属性,属性名为任意的见名知意的字符串
        3)在settings.py文件中进行配置,配置drf 的CEFALUT_THROTTLE_RATES,格式为{
    scope字符串:'次数/时间'}
        4) 在自定义频率类中重写get_cache_key方法
                # 限制的对象返回  与限制信息有关的字符串
                # 不限制的对象返回None,只能返回None,不能是False或者为"",

        短信接口 3/min 频率限制    以(3/min为例)

        

    自定义频率类案列:
    
    from rest_framework.throttling import SimpleRateThrottle
    
    class SMSRateThrottle(SimpleRateThrottle):
        scope = 'sms'
    
        # 只对提交手机号的get方法进行限制
        def get_cache_key(self, request, view):
            mobile = request.query_params.get('mobile')
            # 没有手机号,就不做频率限制
            if not mobile:
                return None
            # 返回可以根据手机号动态变化,且不易重复的字符串,作为操作缓存的key
            return 'throttle_%(scope)s_%(ident)s' % {'scope': self.scope, 'ident': mobile}

      在settings.py中配置

    # drf配置
    REST_FRAMEWORK = {
        # 频率限制条件配置
        'DEFAULT_THROTTLE_RATES': {
            'sms': '3/min'
        },
    }

      在视图:views.py中         

    from .throttles import SMSRateThrottle
    class TestSMSAPIView(APIView):
        # 局部配置频率认证
        throttle_classes = [SMSRateThrottle]
        def get(self, request, *args, **kwargs):
            return APIResponse(0, 'get 获取验证码 OK')
        def post(self, request, *args, **kwargs):
            return APIResponse(0, 'post 获取验证码  OK')

      路由:api/url.py

    url(r'^sms/$',views.TestSMSAPIView.as_view())

      限制的接口

    # 只会对/api/sms/?mobile=具体的手机号  接口才会有频率限制
        #1)对/api/sms/ 或者其他=接口发送无限制
           例如:tp://127.0.0.1:8000/api/sms/
        #2)对数据库提交的mobile的/api/sms/接口无限制
           例如:tp://127.0.0.1:8000/api/sms/?mobile=123
        #3)对不是mobile(如phone)字段提交的电话接口无限制

      认证规则图:

        django不分离

         

        drf分类

            

      认证规则演变图

          数据库session认证:低效                                                                

        缓存认证:高效

           

         jwt认证: 高效                                                                             

        缓存认证:不易并发

          

         jwt认证:易并发

          

           

        

                                                 

         

     

      



      

  • 相关阅读:
    sql server 数据库可疑处理方法。
    执行gpedit.msc 提示找不到程序
    uni-app Post springboot 后台接收数据为null 解决办法
    组件接口升级商品编码文件
    mysql 拼接字符
    mysql 截取前7位
    为什么具有编程思维的孩子更容易成功?孩子为什么要学编程?你想要的答案都在这儿!
    利用支持MicroPython的TPYBoard开发板自制PM2.5检测仪(萝卜教育学科式编程)
    Micropython教程实例之USB-HID应用(萝卜学科编程)
    教程Micropython自制小型家庭气象站(萝卜教育)
  • 原文地址:https://www.cnblogs.com/Fzhiyuan/p/11728763.html
Copyright © 2011-2022 走看看