From 630619e2aa8215c2cb158cb9fac331613fe29f83 Mon Sep 17 00:00:00 2001 From: zhangtao <9480807882@qq.com> Date: Sun, 24 Aug 2025 22:22:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=B8=AD=E9=97=B4=E4=BB=B6):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=BC=94=E7=A4=BA=E7=8E=AF=E5=A2=83IP=E7=99=BD?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在演示环境中间件中添加IP白名单检查,只有白名单中的IP才能执行非GET请求 --- backend/app/config/setting.py | 4 ++++ backend/app/core/middlewares.py | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/app/config/setting.py b/backend/app/config/setting.py index 2304f6c5..77668295 100755 --- a/backend/app/config/setting.py +++ b/backend/app/config/setting.py @@ -179,6 +179,10 @@ class Settings(BaseSettings): DEMO_BLACK_LIST_PATH: List[str] = [ # 演示黑名单 "/auth/login" ] + DEMO_IP_WHITE_LIST: List[str] = [ # 演示白名单IP + "192.168.18.7", + "192.168.1.3" + ] # ================================================= # # ***************** 静态文件配置 ***************** # diff --git a/backend/app/core/middlewares.py b/backend/app/core/middlewares.py index 09c3c141..d0651757 100644 --- a/backend/app/core/middlewares.py +++ b/backend/app/core/middlewares.py @@ -77,7 +77,8 @@ class DemoEnvMiddleware(BaseHTTPMiddleware): async def dispatch( self, request: Request, call_next: RequestResponseEndpoint ) -> Response: - if settings.DEMO_ENABLE and request.method != "GET": + client_ip = request.client.host + if settings.DEMO_ENABLE and request.method != "GET" and client_ip not in settings.DEMO_IP_WHITE_LIST: path = request.scope.get("path") if path in settings.DEMO_BLACK_LIST_PATH: return ErrorResponse(msg="演示环境,禁止操作",)