当前有五次登录尝试将用户阻止1分钟,并且使用以下代码可以正常工作:
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
我想要的是,当用户在第一次尝试后再次解除阻止时,在第二次尝试时,阻止时间应增加到3分钟。
我四处搜寻,但找不到任何东西,有什么办法吗?
答案 0 :(得分:7)
我建议您尝试以下代码。请询问是否不清楚。
$minutes = 3;
$key = $this->throttleKey($request);
$rateLimiter = $this->limiter();
if ($this->hasTooManyLoginAttempts($request)) {
$attempts = $rateLimiter->attempts($key);
if ($attempts > 1) {
$attempts === 2 && $rateLimiter->clear($key);
$this->decayMinutes = ($attempts - 1) * $minutes;
$attempts === 2 && $this->incrementLoginAttempts($request);
$this->incrementLoginAttempts($request);
}
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
增量阻止代码:
$minutes = 3;
$key = $this->throttleKey($request);
$rateLimiter = $this->limiter();
if ($this->hasTooManyLoginAttempts($request)) {
$attempts = $rateLimiter->attempts($key);
$rateLimiter->clear($key);
$this->decayMinutes = $attempts === 1 ? 1 : ($attempts - 1) * $minutes;
for ($i = 0; $i < $attempts; $i++) {
$this->incrementLoginAttempts($request);
}
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
使用缓存进行增量阻止的代码:
$minutes = 3;
$key = $this->throttleKey($request);
$rateLimiter = $this->limiter();
if ($this->hasTooManyLoginAttempts($request)) {
$attempts = $rateLimiter->attempts($key);
$rateLimiter->clear($key); // might have to add logic here
$reflection = new \ReflectionClass($rateLimiter);
$property = $reflection->getProperty('cache');
$property->setAccessible(true);
$cache = $property->getValue($rateLimiter);
$reflectionMethod = new \ReflectionMethod($rateLimiter, 'availableAt');
$reflectionMethod->setAccessible(true);
$blockMinutes = $attempts === 1 ? 1 : $attempts > 1 ? ($attempts - 1) * $minutes : 1;
$cache->add($key.':timer', $reflectionMethod->invoke($rateLimiter, $blockMinutes * 60), $blockMinutes);
$added = $cache->add($key, 0, $blockMinutes);
$hits = (int) $cache->increment($key, $attempts);
if (! $added && $hits === 1) {
$cache->put($key, 1, $blockMinutes);
}
$reflectionMethod->setAccessible(false);
$property->setAccessible(false);
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
答案 1 :(得分:3)
我认为您需要在LoginController中设置属性:
<Route exact path="/" component={HomePage} />
<Route path="/event/:id" component={EventView} />
<Route path="/event/create" component={EventCreate} />
您还可以控制尝试次数:
/event/create
有关更多信息,您可以调查: trait AuthenticatesUsers-具有方法“登录”和描述中的代码。 这个特征使用了另一个特征: “ ThrottlesLogins”->此特征具有名为“ decayMinutes”的方法。它返回分钟数。
希望它将对您有帮助!
答案 2 :(得分:2)
我认为laravel默认值无法满足您的需求,因此,如果用户自己阻止一次,则需要保存(缓存,会话或数据库),并根据需要增加decayMinutes
。
if ($this->hasTooManyLoginAttempts($request)) {
if(Cache::has($this->throttleKey($request))){
$this->decayMinutes = 3;
}
Cache::put($this->throttleKey($request), true);
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}