我正在使用Laravel 5.4只是试图从控制器获取失败的登录尝试次数,如下所示。它总是返回0,但hasTooManyLoginAttempts
工作正常。有人有什么想法吗?
在LoginController.php
protected function hasTooManyLoginAttempts(Request $request)
{
return $this->limiter()->tooManyAttempts(
$this->throttleKey($request), 3, 1
);
}
如果登录尝试失败3次,上面的代码可以通过锁定用户1分钟来正常工作。但我希望得到登录尝试的计数。基于以下代码
public function tooManyAttempts($key, $maxAttempts, $decayMinutes = 1)
{
echo $this->attempts($key);
exit;
if ($this->cache->has($key.':lockout')) {
return true;
}
if ($this->attempts($key) > $maxAttempts) {
$this->lockout($key, $decayMinutes);
$this->resetAttempts($key);
return true;
}
return false;
}
echo $this->attempts($key);
总是返回0.我们怎么弄清楚这个?
答案 0 :(得分:2)
好$this->attempts($key)
确实会返回尝试次数。但问题是它总是会返回1,因为你dd()
它会从缓存中获取数字。
因此,如果您在尝试失败后重新加载页面,缓存将被清除,并且在您的情况下它将显示0(它显示为0,因为tooManyAttempts
是从hasTooManyLoginAttempts
调用的AuthenticatesUsers
特征,在第48行$this->incrementLoginAttempts($request);
之前。)
我做的是在公共文件夹中创建了一个名为" test.txt"的文件,然后找到了incrementLoginAttempts
函数,该函数位于ThrottlesLogins
特征(行32,laravel 5.4),最后我把它放在这个函数中:file_put_contents('test.txt', $this->limiter()->attempts($this->throttleKey($request)));
(或者只使用\Log::info(...)
帮助器。)
它允许您检查失败的登录尝试次数,这样您就可以链接失败并观察数量的增加。因此$this->limiter()->attempts($this->throttleKey($request))
或$this->attempts($key)
会返回正确的数字。
我可能已经忘记了一些事情,因为我对代码进行了快速研究,但它基本上解释了为什么你的价值是"总是"同样的: - )。
答案 1 :(得分:1)
尝试一下(在LoginController.php中):
$this->limiter()->attempts($this->throttleKey($request));
如果hasTooManyLoginAttempts
函数执行了。结果为0。您可以在类tooManyAttempts
上阅读函数RateLimiter
以了解更多信息。