From f75ebffa5def8e541e8c6f2542fd3732e1b4688e Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Tue, 4 Mar 2025 17:26:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E6=96=B0=E5=A2=9E=E9=99=90?= =?UTF-8?q?=E6=B5=81=E5=99=A8=20add=20rate=20limiting=20middleware=20for?= =?UTF-8?q?=20backend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add rate limiting functionality to the admin panel - Implement rate limiting middleware to control API request frequency - Update login controller to use rate limiting Add helper functions for getting IP and admin UID - Update route configuration to include rate limiting middleware - Add Redis configuration to .env file - Update composer.json to include rate limiting package dependency --- .example.env | 10 ++++++ app/admin/config/route.php | 3 ++ app/admin/controller/Login.php | 3 ++ app/admin/middleware/RateLimiting.php | 45 +++++++++++++++++++++++++++ app/common/utils/Helper.php | 28 +++++++++++++++++ composer.json | 1 + 6 files changed, 90 insertions(+) create mode 100644 app/admin/middleware/RateLimiting.php create mode 100644 app/common/utils/Helper.php 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": "*"