登录次数限制

This commit is contained in:
2024-12-04 14:19:43 +08:00
parent a9d80997b6
commit 5eca489b1c

View File

@@ -31,8 +31,9 @@ class LoginController extends BaseAdminController
$expireTime = 1800; // 30 分钟
$redis = Yii::$app->redis;
$errNum = $redis->get($key);
$txt = '当前账号因频繁登录失败已被锁定30分钟内无法重试预计解锁时间';
if ($errNum >= $maxAttempts) {
throw new Exception("当前账号因频繁登录已被锁定30分钟内无法重试预计解锁时间". date('Y-m-d H:i:s', time() + $redis->ttl($key)));
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
}
$model = new LoginForm();
@@ -82,9 +83,9 @@ class LoginController extends BaseAdminController
$currentValue = $redis->incr($key);
// 如果是第一次累加,设置过期时间
if ($currentValue == 5) {
if (in_array($currentValue, [1, 5])) {
$redis->expire($key, $expireTime);
throw new Exception("当前账号因频繁登录已被锁定30分钟内无法重试预计解锁时间". date('Y-m-d H:i:s', time() + $redis->ttl($key)));
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
}
}
throw new Exception($e->getMessage().' 还有'.($maxAttempts - $currentValue).'次机会');