Laravel 8 速率限制器不适用于路线

mah*_*nke 8 php throttling rate-limiting laravel laravel-8

在 web.php 路由中,我有以下内容:

Route::middleware('throttle:3,1')->group(function () {
    Route::get('/about', function () {
        return "About Info";
    });
});
Run Code Online (Sandbox Code Playgroud)

Laravel 框架是 8.19.0。

理想情况下,当有人在 1 分钟内点击页面超过 3 次时,laravel 应该给出 429 Too Many Attempts 响应。但事实并非如此。3 次后我没有收到 429 响应。

如何解决这个问题?

谢谢

小智 21

Laravel 8configureRateLimiting()开始,您可以在App\Providers\RouteServiceProvider.

例如:

protected function configureRateLimiting()
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
    });
}
Run Code Online (Sandbox Code Playgroud)

如果您已从 Laravel 7 更新,请不要忘记boot()RouteServiceProvider. 否则限制将不适用。

public function boot()
{
    $this->configureRateLimiting();

    parent::boot();
}
Run Code Online (Sandbox Code Playgroud)

另请参阅文档:https://laravel.com/docs/8.x/routing#rate-limiting 和 Laracasts 视频:https://laracasts.com/series/whats-new-in-laravel-8/episodes/9


Ram*_*ian 5

转到该.env文件并检查您的缓存驱动程序,如果CACHE_DRIVER=none,请设置缓存驱动程序。
Laravel 支持 支持:“apc”、“数组”、“数据库”、“文件”、“memcached”、“redis”、“dynamodb”


mah*_*nke 0

我有这个问题。在 config/cache.php 中,默认设置为“null”。我改为“数据库”。现在,这工作正常。