当前位置:网站首页>laravel8更新之速率限制改进
laravel8更新之速率限制改进
2020-11-08 13:07:00 【神兵小将】
原文地址:https://www.wjcms.net/archives/laravel8%E6%9B%B4%E6%96%B0%E4%B9%8B%E9%80%9F%E7%8E%87%E9%99%90%E5%88%B6%E6%94%B9%E8%BF%9B
Laravel的请求速率限制器功能已增强,具有更大的灵活性和功能,同时仍保持与先前版本的throttle中间件API的向后兼容性。
速率限制器是使用RateLimiter立面的for方法定义的。该for方法接受一个速率限制器名称和一个Closure,该Closure返回应应用于分配了该速率限制器的路由的限制配置:
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('global', function (Request $request) {
return Limit::perMinute(1000);
});
由于速率限制器回调接收传入的HTTP请求实例,因此您可以根据传入的请求或经过身份验证的用户动态构建适当的速率限制:
RateLimiter::for('uploads', function (Request $request) {
return $request->user()->vipCustomer()
? Limit::none()
: Limit::perMinute(100);
});
有时您可能希望将速率限制按任意值进行细分。例如,您可能希望允许用户每个IP地址每分钟100次访问给定路由。为此,您可以by在建立速率限制时使用以下方法:
RateLimiter::for('uploads', function (Request $request) {
return $request->user()->vipCustomer()
? Limit::none()
: Limit::perMinute(100)->by($request->ip());
});
可以使用throttle 中间件将速率限制器附加到路由或路由组。油门中间件接受您希望分配给路线的速率限制器的名称:
Route::middleware(['throttle:uploads'])->group(function () {
Route::post('/audio', function () {
//
});
Route::post('/video', function () {
//
});
});
版权声明
本文为[神兵小将]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3380391/blog/4707811
边栏推荐
- This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
- 分布式文档存储数据库之MongoDB基础入门
- 在51CTO学院Get到PMP证书
- This time Kwai tiktok is faster than shaking.
- Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
- On the confirmation of original data assets
- 阿里云视频云技术专家 LVS 演讲全文:《“云端一体”的智能媒体生产制作演进之路》
- Python basic syntax
- 【Python 1-6】Python教程之——数字
- 运维人员常用到的 11 款服务器监控工具
猜你喜欢
2035我们将建成这样的国家
Introduction to mongodb foundation of distributed document storage database
小米、OPPO在欧洲市场继续飙涨,小米更是直逼苹果
Suitable for C / C + + novice learning some projects, do not give me to miss!
Drink soda, a bottle of soda water 1 yuan, two empty bottles can change a bottle of soda, give 20 yuan, how much soda can you
Shell uses. Net objects to send mail
PMP心得分享
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Python basic syntax variables
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
随机推荐
Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
On the confirmation of original data assets
Powershell 使用.Net对象发送邮件
在51CTO学院Get到PMP证书
Python basic syntax
[Python 1-6] Python tutorial 1 -- number
阿里出品!视觉计算开发者系列手册(附网盘链接)
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
This time Kwai tiktok is faster than shaking.
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
PMP experience sharing
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
笔试面试题目:盛水最多的容器
漫画:寻找股票买入卖出的最佳时机(整合版)
小米、OPPO在欧洲市场继续飙涨,小米更是直逼苹果
Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
Ubuntu20.04下访问FTP服务器乱码问题+上传文件