Laravel 5.2+修改默认登录行为+使用缓存驱动程序Array时Throllting停止工作


Laravel 5.2 + Modify default login Behavior + Throllting stopped working when cache driver Array is used

我在Laravel 5.2中创建了新项目,并试图修改Auth Login的默认行为。

创建了一个名为postLogin的新方法,该方法在提交登录表单时被调用。

以下是我为在postRegister方法中实现登录节流而编写的代码。

protected function postLogin(AuthLoginRequest $request){
    $credentials = $this->getCredentials($request);
    $credentials['is_activated'] = "Yes";
    $remember = $request->has('remember');
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }
    if (Auth::guard($this->getGuard())->attempt($credentials, $remember)) {
        // add login in case of success
        return $this->handleUserWasAuthenticated($request, $throttles);
    } else {
        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }
        return redirect("/login")
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                $this->loginUsername() => $this->getFailedLoginMessage(),
            ]);
    }
}

当我将缓存驱动程序保持为"文件"时,这一点很好当我将缓存驱动程序保持为"array"时,此代码不起作用。

当缓存驱动程序="array"时,有人能通知我我缺少什么吗?

有两件事需要注意:

  • array驱动程序只是将任何缓存的值存储在内部数组中。这些值不会在请求之间保留。

  • 如果您查看ThrottlesLogins特性,您可以看到它在每次登录失败时都会增加一个缓存计数器。

这就是它不起作用的原因。

来自Laravel文档:

注意:数组驱动程序通常用于运行测试,以防止会话数据持久化。