diff --git a/.example.env b/.example.env index d2ab1c2..e32794e 100644 --- a/.example.env +++ b/.example.env @@ -14,6 +14,16 @@ DB_PORT=3306 DB_CHARSET=utf8mb4 DB_PREFIX=ea8_ +# 限流器开关 +RATE_LIMITING_STATUS=false + +# Redis配置 +REDIS_HOST=127.0.0.1 +REDIS_PORT=6379 +REDIS_PASSWORD= +REDIS_PREFIX= +REDIS_DATABASE=0 + # 后台配置项组 [EASYADMIN] diff --git a/app/admin/config/route.php b/app/admin/config/route.php index a8eac1d..f65bcd0 100644 --- a/app/admin/config/route.php +++ b/app/admin/config/route.php @@ -4,6 +4,7 @@ use app\admin\middleware\CheckInstall; use app\admin\middleware\CheckLogin; use app\admin\middleware\CheckAuth; use app\admin\middleware\SystemLog; +use app\admin\middleware\RateLimiting; // 你可以在这里继续写你需要的路由 @@ -16,6 +17,8 @@ use app\admin\middleware\SystemLog; return [ 'middleware' => [ + // 限流中间件 + RateLimiting::class, // 判断是否已经安装后台系统 CheckInstall::class, // 检测是否登录 diff --git a/app/admin/controller/Login.php b/app/admin/controller/Login.php index 362249b..5fbbc51 100644 --- a/app/admin/controller/Login.php +++ b/app/admin/controller/Login.php @@ -4,12 +4,14 @@ namespace app\admin\controller; use app\admin\model\SystemAdmin; use app\common\controller\AdminController; +use app\common\utils\Helper; use think\captcha\facade\Captcha; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use app\Request; use think\Response; +use Wolfcode\RateLimiting\Attributes\RateLimitingMiddleware; class Login extends AdminController { @@ -34,6 +36,7 @@ class Login extends AdminController * @throws DbException * @throws ModelNotFoundException */ + #[RateLimitingMiddleware(key: [Helper::class, 'getIp'], seconds: 1, limit: 1, message: '请求过于频繁')] public function index(Request $request): string { $captcha = env('EASYADMIN.CAPTCHA', 1); diff --git a/app/admin/middleware/RateLimiting.php b/app/admin/middleware/RateLimiting.php new file mode 100644 index 0000000..e4c64f1 --- /dev/null +++ b/app/admin/middleware/RateLimiting.php @@ -0,0 +1,45 @@ +controller(); + $module = app('http')->getName(); + $appNamespace = config('app.app_namespace'); + $controllerClass = "app\\{$module}\\controller\\{$controller}{$appNamespace}"; + $controllerClass = str_replace('.', '\\', $controllerClass); + $action = $request->action(); + try { + Bootstrap::init($controllerClass, $action, [ + # Redis 相关配置 + 'host' => env('REDIS_HOST', '127.0.0.1'), + 'port' => env('REDIS_PORT, 6379'), + 'password' => env('REDIS_PASSWORD', ''), + 'prefix' => env('REDIS_PREFIX', ''), + 'database' => env('REDIS_DATABASE', 0), + ]); + }catch (\Throwable $exception) { + $this->error($exception->getMessage()); + } + return $next($request); + } +} \ No newline at end of file diff --git a/app/common/utils/Helper.php b/app/common/utils/Helper.php new file mode 100644 index 0000000..cef2cf1 --- /dev/null +++ b/app/common/utils/Helper.php @@ -0,0 +1,28 @@ +ip(); + } + + /** + * 获取当前登录用户ID + * @return int|string + */ + public static function getAdminUid(): int|string + { + return session('admin.id') ?: 0; + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index d823813..33bbe22 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "qiniu/php-sdk": "^7.11.0", "wolf-leo/phplogviewer": "^0.11.3", "wolfcode/authenticator": "^0.0.6", + "wolfcode/rate-limiting": "^0.1.0", "ext-json": "*", "ext-mysqli": "*", "ext-pdo": "*"