Django REST framework 如何实现内置访问频率控制
作者:little_pd 时间:2023-08-01 17:11:00
对匿名用户采用 IP 控制访问频率,对登录用户采用 用户名 控制访问频率。
from rest_framework.throttling import SimpleRateThrottle
class VisitThrottle(SimpleRateThrottle):
"""匿名用户访问频率限制"""
scope = "AnonymousUser" # 随便写的,可以作为key保存在缓存中
def get_cache_key(self, request, view):
return self.get_ident(request)
class UserThrottle(SimpleRateThrottle):
"""登录用户访问频率限制"""
scope = "LoginUser"
def get_cache_key(self, request, view):return request.user
可以配置redis
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
# "PASSWORD": "密码",
}
}
}
匿名用户的访问频率限制,这里设置在全站下,如下:
REST_FRAMEWORK = {
"DEFAULT_THROTTLE_CLASSES": ["appxx.utils.VisitThrottle"],
"DEFAULT_THROTTLE_RATES":{
"AnonymousUser": "3/m", # 匿名用户一分钟可以访问3次,秒(s)、分(m)、时(h)、天(d)
"LoginUser": "10/m", # 登录用户一分钟可以访问10次
}
}
登录用户的访问频率设置在单独的视图中,而视图依赖身份认证才能辨别用户是否登陆了,所以设置如下:
class BookViewSet(viewsets.ModelViewSet):
authentication_classes = [TokenAuthentication]
throttle_classes = [UserThrottle]
queryset = models.Book.objects.all()
serializer_class = serializers.BookSerializer
用户身份认证如下:
from rest_framework import authentication
from rest_framework import exceptionsfrom appxx import models
class TokenAuthentication(authentication.BaseAuthentication):
"""身份认证"""
def authenticate(self, request):
token = request.GET.get("token")
obj = models.UserAuthToken.objects.filter(token=token).first()
if not obj:
raise exceptions.AuthenticationFailed("验证失败!")
else:
return (obj.user.username, obj.token)
来源:https://www.cnblogs.com/believepd/p/10278207.html
标签:django,rest,framework,内置,访问,频率,控制
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
thinkphp 多表 事务详解
2023-07-08 05:43:36
python爬虫爬取微博评论案例详解
2022-12-23 13:31:53
![](https://img.aspxhome.com/file/2023/7/92497_0s.png)
Go语言标准库之strconv的使用
2024-04-23 09:42:56
SQL多表联合查询时如何采用字段模糊匹配
2024-01-19 18:06:40
![](https://img.aspxhome.com/file/2023/6/115306_0s.png)
vue监听路由变化的几种方式小结
2024-05-21 10:28:41
Vue中ref和$refs的介绍以及使用方法示例
2023-07-02 17:00:10
![](https://img.aspxhome.com/file/2023/1/139881_0s.png)
Python实现的排列组合计算操作示例
2022-03-06 19:36:43
![](https://img.aspxhome.com/file/2023/4/118614_0s.png)
Python制作一个随机抽奖小工具的实现
2022-10-22 03:53:29
![](https://img.aspxhome.com/file/2023/3/104973_0s.png)
MySQL UPDATE delete 语句的速度
2008-03-12 12:22:00
mysql获得60天前unix时间思路及代码
2024-01-29 01:44:07
python字符串过滤性能比较5种方法
2021-09-26 18:35:25
解决Navicat无法连接 VMware中Centos系统中的 MySQL服务器的问题
2024-01-16 03:12:17
![](https://img.aspxhome.com/file/2023/3/86513_0s.png)
asp中最新新闻显示new图片的实现代码
2012-11-30 20:31:42
详解Python不同版本之间的切换方法
2023-12-28 05:22:06
![](https://img.aspxhome.com/file/2023/9/124689_0s.png)
chr(9)、chr(10)、chr(13)、chr(32)与特殊空格
2009-07-03 15:26:00
MySQL百万级数据量分页查询方法及其优化建议
2024-01-17 20:56:11
微信小程序使用npm支持踩坑
2024-04-22 13:07:40
PHP+Ajax实现无刷新分页实例详解(附demo源码下载)
2024-06-07 15:50:03
![](https://img.aspxhome.com/file/2023/9/122469_0s.jpg)
css:小技巧大问题,cellSpacing用css样式代替方法,其它样式类似解决!
2009-10-04 20:35:00
python 字典(dict)遍历的四种方法性能测试报告
2023-08-21 21:27:08