gpt4 book ai didi

php - 拉维尔 : Increase time on second time login attempts

转载 作者:可可西里 更新时间:2023-11-01 12:48:54 26 4
gpt4 key购买 nike

目前,五次登录尝试会阻止用户 1 分钟,并且可以正常使用以下代码:

if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}

我想要的是,当用户在第一次尝试后再次解锁时,在第二次尝试中,阻止时间应增加到 3 分钟。

我四处寻找,但找不到任何东西,有什么办法可以解决吗?

最佳答案

我建议您尝试以下代码。请询问是否有任何不清楚的地方。

$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);
}

关于php - 拉维尔 : Increase time on second time login attempts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51854395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com