【问题标题】:Laravel 6: Throttle Password ResetLaravel 6:油门密码重置
【发布时间】:2020-06-01 17:32:59
【问题描述】:

在 laravel 6 中,密码代理现在具有以下限制密码重置 (https://github.com/laravel/framework/blob/6.x/src/Illuminate/Auth/Passwords/PasswordBroker.php#L58)

public function sendResetLink(array $credentials)
{
    // First we will check to see if we found a user at the given credentials and
    // if we did not we will redirect back to this current URI with a piece of
    // "flash" data in the session to indicate to the developers the errors.
    $user = $this->getUser($credentials);

    if (is_null($user)) {
        return static::INVALID_USER;
    }

    if (method_exists($this->tokens, 'recentlyCreatedToken') &&
        $this->tokens->recentlyCreatedToken($user)) {
        return static::RESET_THROTTLED;
    }

    // Once we have the reset token, we are ready to send the message out to this
    // user with a link to reset their password. We will then redirect back to
    // the current URI having nothing set in the session to indicate errors.
    $user->sendPasswordResetNotification(
        $this->tokens->create($user)
    );

    return static::RESET_LINK_SENT;
}

但是,当我反复提交密码重置时,为什么密码重置没有受到限制 - 我仍然收到重置通知?

我注意到 6.x 版本的 TokenRepositoryInterface 中不存在 recentlyCreatedToken 方法 https://github.com/laravel/framework/blob/6.x/src/Illuminate/Auth/Passwords/TokenRepositoryInterface.php

但是已经在7.x版本中添加了

https://github.com/laravel/framework/blob/master/src/Illuminate/Auth/Passwords/TokenRepositoryInterface.php

这只是 v7.x 的一个功能,还是我需要做一些我缺少的事情?

【问题讨论】:

    标签: throttling forgot-password laravel-6.2


    【解决方案1】:

    密码重置节流在 Laravel 6.x 中有效,但由于某种原因,您需要在配置文件 config/auth.php 中手动设置 throttle 参数:

        'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
                'throttle' => 60, // Allows a user to request 1 token per 60 seconds
            ],
        ],
    

    DatabaseTokenRepository 将节流时间的默认值定义为 60 秒。但是当 DatabaseTokenRepository 在 PasswordBrokerManager 中初始化时,它会检查配置文件,如果没有找到值,则将油门时间设置为 0(意味着禁用油门)。

    您还需要将消息字符串添加到resources/lang/en/passwords.php 以向用户显示可理解的错误消息:

    'throttled' => 'You have requested password reset recently, please check your email.',
    

    P。 S. 使用php artisan config:clear 编辑配置文件后不要忘记刷新配置缓存。

    【讨论】:

    • 密码重置限制仅适用于 Laravel 7.x。此功能在 6.x 中不可用,请参阅 github.com/laravel/framework/issues/31513
    • @adam78,这不是真的。它适用于 Laravel 6.x。几天前,我使用 Laravel v6.15.1 在我的项目中实现了密码重置节流。只有在那之后,我才在这里写下我的答案。所以你可以试试上面的解决方案,自己看看。我还附上了 Laravel 源代码的链接,它是在哪里实现的,但是你给我一个链接到一个带有假设的讨论。 :-/
    • 打扰一下,你想让我看到什么?在第一条评论中提到了 PR #30340,它为 Laravel 6.x 添加了密码重置限制 - github.com/laravel/framework/pull/30340
    猜你喜欢
    • 2021-08-23
    • 2019-03-16
    • 2018-10-12
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2020-10-14
    • 2018-06-25
    • 2015-05-27
    相关资源
    最近更新 更多