Laravel 8 Fortify 登录说 429 请求太多

Laravel 8 Fortify login says 429 TOO MANY REQUESTS

我遇到了与 429 TOO MANY REQUESTS 有关的问题。我使用了 Laravel fortify,我的网络路由就像

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard','DashboardController@dashboardView')
    ->name('dashboard')->middleware('auth');

问题间歇性出现,成功登录后,如果我立即点击注销并立即尝试登录,我将重定向到仪表板,它给出了 429 TOO MANY REQUESTS,地址栏中的 URL 是 http://127.0.0.1:8000/login。现在,如果我在那里等待一秒钟并刷新页面,它会重定向到仪表板页面。

我在网上搜索过,每个人都在谈论油门,但我认为这不是解决方案。请帮我。 谢谢

我今天偶然发现了同样的问题并进行了一些调试。注册 /login 路由时,Fortify 将 Illuminate\Routing\Middleware\ThrottleRequests:login 中间件应用于它。这意味着,对于对该路由的每个请求,ThrottleRequests 中间件将为该指定键调用 RateLimiter 实例。显然,Fortify 没有为 login 键注册 RateLimiter

由于 RateLimiter 实例的 $limiters 属性 中缺少键,ThrottleRequests 中间件使用其默认回退,不处理边缘案例“应该有那个密钥的速率限制器,但没有。”真的很好。 $maxAttempts 变量设置为 0,将导致不稳定的速率限制行为。

我觉得这是 Fortify 中的一个错误,因为速率限制也发生在 \Laravel\Fortify\Actions\EnsureLoginIsNotThrottled 操作中,它在 \Laravel\Fortify\Http\Controllers\AuthenticatedSessionController 控制器中调用。不过,我没有在全新的 Laravel 安装中检查这一点,所以我不想在这里下结论。

总之,长话短说:作为一种解决方法,您可以简单地在您的某些提供商中为“登录”键注册一个速率限制器,例如。 G。 AppServiceProviderAuthServiceProvider:

public function boot()
{
    RateLimiter::for("login", function () {
        Limit::perMinute(5);
    });
}

编辑: 我刚刚意识到“登录”键的速率限制器确实由 Fortify 在 FortifyServiceProvider class 中提供。如果您碰巧遇到与上述问题类似的问题,请确保将 FortifyServiceProvider class 添加到 config/app.php.[=28= 中的 providers 数组]

转到

app/http/kernel.php

并从 routeMiddleware 列表中删除行

'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,

我尝试了所有方法,包括最佳答案,但还是没有用。

因此,甚至 FortifyServiceProvider class 中的 RateLimiter 都没有更改。

我尝试登录并在仅一次登录尝试后收到 429 错误

这对我来说是什么问题,是 config/fortify.php 文件。

我不得不改变:

    /*
    |--------------------------------------------------------------------------
    | Rate Limiting
    |--------------------------------------------------------------------------
    |
    | By default, Fortify will throttle logins to five requests per minute for
    | every email and IP address combination. However, if you would like to
    | specify a custom rate limiter to call then you may specify it here.
    |
    */

    'limiters' => [
        'login' => 'login',
        'two-factor' => 'two-factor',
    ],

    /*
    |--------------------------------------------------------------------------
    | Rate Limiting
    |--------------------------------------------------------------------------
    |
    | By default, Fortify will throttle logins to five requests per minute for
    | every email and IP address combination. However, if you would like to
    | specify a custom rate limiter to call then you may specify it here.
    |
    */

    'limiters' => [
        'login' => 5,
        'two-factor' => 5,
    ],

有趣的是,当您 运行:

时,问题是 Fortify 包本身固有的

php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider" 根据他们的 documentation instructions.

这个根本原因是 vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php 中的代码无法正确解析限制:

    /**
     * Resolve the number of attempts if the user is authenticated or not.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int|string  $maxAttempts
     * @return int
     */
    protected function resolveMaxAttempts($request, $maxAttempts)
    {
        if (Str::contains($maxAttempts, '|')) {
            $maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
        }

        if (! is_numeric($maxAttempts) && $request->user()) {
            $maxAttempts = $request->user()->{$maxAttempts};
        }

        return (int) $maxAttempts;
    }

,这意味着,'login' 只是被解析为 0,这就是 returns。

现在我不必 运行 php artisan cache:clear 只是为了测试。