multi-store/app/common/cache/AdminAccountSafeCache.php
mkm de60b1b1ca 统一使用真实IP获取方式
将`getRemoteIp`和`getLocalIp`方法替换为`getRealIp`,确保在`OperationLogMiddleware`和`AdminAccountSafeCache`中获取的客户端IP地址一致。这个修改可能是为了应对代理或负载平衡器等中间件影响IP识别的情况。
2024-10-08 21:19:02 +08:00

63 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\cache;
use support\Cache;
class AdminAccountSafeCache extends BaseCache
{
private $key;//缓存次数名称
public $minute = 15;//缓存设置为15分钟即密码错误次数达到锁定15分钟
public $count = 15; //设置连续输错次数即15分钟内连续输错误15次后锁定
public function __construct($prefix = 'admin_')
{
parent::__construct();
$ip = \request()->getRealIp();
// $this->key = $this->tagName . $ip;
$this->key = $prefix . $ip;
}
/**
* @notes 记录登录错误次数
* @author 令狐冲
* @date 2021/6/30 01:51
*/
public function record()
{
if (Cache::get($this->key)) {
//缓存存在,记录错误次数
Cache::set($this->key, Cache::get($this->key) + 1);
} else {
//缓存不存在,第一次设置缓存
Cache::set($this->key, 1, $this->minute * 60);
}
}
/**
* @notes 判断是否安全
* @return bool
* @author 令狐冲
* @date 2021/6/30 01:53
*/
public function isSafe()
{
$count = Cache::get($this->key);
if ($count >= $this->count) {
return false;
}
return true;
}
/**
* @notes 删除该ip记录错误次数
* @author 令狐冲
* @date 2021/6/30 01:55
*/
public function relieve()
{
Cache::delete($this->key);
}
}