Laravel:不同路径的API速率限制不同 [英] Laravel: different api rate limits for different paths
问题描述
我需要为不同的路径设置不同的速率限制. 敌人的例子:
I need to setup different rate limits for different paths. Foe example:
在路径 /users 上,我希望每分钟60个请求的速率限制,而对于路径 /stats 我希望每分钟只能有5个请求的速率限制.
On path /users I want to have rate limit of 60 requests per minute, while for path /stats I want to have rate limit of only 5 requests per minute.
我尝试了下一种方法
Route::group(['middleware' => ['auth', 'throttle:60']], function(){
Route::get('users', 'User@list');
});
Route::group(['middleware' => ['auth', 'throttle:5']], function(){
Route::get('stats', 'User@stats');
});
以某种方式,应用最后的速率限制.但是,在用户路径上发出请求时,X-Rate-Limit-Limit标头设置为60,但在到达第6个请求时会引发太多请求"错误.
Somehow, last rate limit is applied. However, when making requests on users path, X-Rate-Limit-Limit header is set to 60, but it throws "Too many requests" error when it reaches 6th request.
推荐答案
You may want to try commenting out the default rate on line 40 of the Kernel.php
since you are specifying it in each middleware group to avoid conflict.
您可能还希望更改中间件,使其包含第二个参数,该参数表示等待下一个请求可以进入的等待时间.(例如throttle:60,1
)
You may also want to change the middleware to include the second parameter of how long the waiting period is until the next request can come in. (e.g. throttle:60,1
)
这篇关于Laravel:不同路径的API速率限制不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!