针对特定方法的 Laravel API 速率限制器
有没有办法对POST、PUT等特定方法应用速率限制(节流),以防止在几秒钟内命中多个API
我尝试对中的整个API应用限制>/app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
...
],
'api' => [
'throttle:1,0.008', <<--- its prevent all api within 0.48 sec
],
];
问题:
我们需要仅阻止选定的方法。
Is there any way to apply rate limit (throttle) for specific method like POST, PUT to prevent multiple api hitting within seconds
I tried to apply limit for whole api in /app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
...
],
'api' => [
'throttle:1,0.008', <<--- its prevent all api within 0.48 sec
],
];
Problem :
we need to prevent only selected methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以做到这一点,您可以创建另一个中间件,您可以使用它并对要应用自定义限制的路由进行分组。
此外,您可以在定义路线时直接应用油门
或
There are number of ways to do it, You can create another middleware which you can use and group the routes you want to apply custom throttle.
Additionally, you can straightly apply the throttle when defining a route
or
您可以使用多种方式在 Laravel 中进行速率限制。
方法之一是中间件。银已经描述了方式。
第二种方法是使用
Illuminate\Support\Facades\RateLimiter
(Laravel 8 或更高版本)例如,如果您想发送电子邮件验证消息,速率限制为每 60 秒 1 条消息。
关于
RateLimiter
You can use multiple ways to make rate limit in Laravel.
One of ways is Middleware. silver already describe the way.
Second way is using
Illuminate\Support\Facades\RateLimiter
(Laravel 8 or higher)For example, if you want to send email verification messages with rate limit 1 message per 60 seconds.
About
RateLimiter
使用 RateLimiter 的另一种方法如下:
OR
参考 Laravel速率限制文档
Another way to use the RateLimiter is as follows:
OR
Reference Laravel Rate Limiting docs