前后端分离开发 使用throttle 限制api的访问速率

根据官网 的做法

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        # 'rest_framework.authentication.TokenAuthentication',
        # 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
    # 设置api访问限制
    'DEFAULT_THROTTLE_CLASSES': (
            'rest_framework.throttling.AnonRateThrottle', # ip 匿名用户
            'rest_framework.throttling.UserRateThrottle'  # token(session)
        ),
    'DEFAULT_THROTTLE_RATES': {
        'anon': '2/minute',
        'user': '3/minute'
    }
}

然后在需要的地方设置

from rest_framework.throttling import UserRateThrottle,AnonRateThrottle

访问就会出现这个

猜你喜欢

转载自blog.csdn.net/pzl_pzl/article/details/81103875