php 在 Laravel 中禁用速率限制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43058219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable rate limiter in Laravel?
提问by SimpleJ
Is there a way to disable rate limiting on every/individual routes in Laravel?
有没有办法禁用 Laravel 中每个/单个路由的速率限制?
I'm trying to test an endpoint that receives a lot of requests, but randomly Laravel will start responding with { status: 429, responseText: 'Too Many Attempts.' }
for a few hundred requests which makes testing a huge pain.
我正在尝试测试接收大量请求的端点,但随机 Laravel 将开始响应{ status: 429, responseText: 'Too Many Attempts.' }
几百个请求,这使得测试非常痛苦。
回答by EddyTheDove
In app/Http/Kernel.php
Laravel has a default throttle limit for all api routes.
在app/Http/Kernel.php
Laravel 中,所有 api 路由都有一个默认的节流限制。
protected $middlewareGroups = [
...
'api' => [
'throttle:60,1',
],
];
Comment or increase it.
评论或增加它。
回答by Mostafa Bahri
You can actually disable onlya certain middleware in tests.
您实际上只能在测试中禁用某个中间件。
use Illuminate\Routing\Middleware\ThrottleRequests;
class YourTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
...
}
回答by cyclops1101
Assuming you are using the API routes then you can change the throttle in app/Http/Kernel.php or take it off entirely. If you need to throttle for the other routes you can register the middleware for them separately.
假设您正在使用 API 路由,那么您可以更改 app/Http/Kernel.php 中的节流阀或完全取消它。如果您需要对其他路由进行节流,您可以分别为它们注册中间件。
(example below: throttle - 60 attempts then locked out for 1 minute)
(以下示例:油门 - 60 次尝试,然后锁定 1 分钟)
'api' => [
'throttle:60,1',
'bindings',
],
回答by 120DEV
In Laravel 5.7
在 Laravel 5.7 中
Dynamic Rate LimitingYou may specify a dynamic request maximum based on an attribute of the authenticated User model. For example, if your User model contains a rate_limit attribute, you may pass the name of the attribute to the throttle middleware so that it is used to calculate the maximum request count:
动态速率限制您可以根据经过身份验证的用户模型的属性指定动态请求最大值。例如,如果您的 User 模型包含 rate_limit 属性,您可以将该属性的名称传递给油门中间件,以便使用它来计算最大请求数:
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
Route::get('/user', function () {
//
});
});
回答by Elias Soares
If you want to disable just for automated tests, you can use the WithoutMiddleware
trait on your tests.
如果您只想为自动化测试禁用,您可以WithoutMiddleware
在测试中使用trait。
use Illuminate\Foundation\Testing\WithoutMiddleware;
class YourTest extends TestCase {
use WithoutMiddleware;
...
Otherwise, just remove the 'throttle:60,1',
line from your Kernelfile (app/Http/Kernel.php
), and your problem will be solved.
否则,只需'throttle:60,1',
从内核文件 ( app/Http/Kernel.php
) 中删除该行,您的问题就会得到解决。
回答by Rhys
A non-hacky way to increase the throttle in unit tests to avoid the dreaded 429:
在单元测试中增加油门以避免可怕的 429 的非黑客方法:
- Remove the throttle:60,1 from the kernel file middleware.
- Add the throttle middleware back in to the route group, using an environment variable instead:
- 从内核文件中间件中删除油门:60,1。
- 使用环境变量将油门中间件重新添加到路由组中:
$requestsPerMinute = ENV("REQUESTS_PER_MINUTE", 60);
Route::middleware(["auth:api", "throttle:$requestsPerMinute,1"])->group(function(){
// your routes
});
- Define the REQUESTS_PER_MINUTE environment variable much higher in phpunit.xml, to allow more requests in test environment before throttling.
- 在 phpunit.xml 中定义更高的 REQUESTS_PER_MINUTE 环境变量,以在限制之前在测试环境中允许更多请求。
<server name="REQUESTS_PER_MINUTE" value="500"/>
- (Also define the new REQUESTS_PER_MINUTE var in .env, even though it'll fall back to 60).
- (还要在 .env 中定义新的 REQUESTS_PER_MINUTE 变量,即使它会回退到 60)。
回答by Petar Vasilev
You can use cache:clear
command to clear your cache, like so:
您可以使用cache:clear
命令清除缓存,如下所示:
php artisan cache:clear
回答by NIKHIL NEDIYODATH
You can add the following lines in your app/Http/Kernel.php
您可以在您的 app/Http/Kernel.php
'api' => [
'throttle:120,1',
'bindings',
\App\Library\Cobalt\Http\Middleware\LogMiddleware::class,
],
If the problem persists try the artisan command php artisan cache:clear
如果问题仍然存在,请尝试使用 artisan 命令 php artisan cache:clear