Merge pull request #134 from 1014TaoTao/v2.0.0

feat(中间件): 添加演示环境IP白名单功能
This commit is contained in:
fastapiadmin
2025-08-24 22:23:34 +08:00
committed by GitHub
2 changed files with 6 additions and 1 deletions
+4
View File
@@ -179,6 +179,10 @@ class Settings(BaseSettings):
DEMO_BLACK_LIST_PATH: List[str] = [ # 演示黑名单 DEMO_BLACK_LIST_PATH: List[str] = [ # 演示黑名单
"/auth/login" "/auth/login"
] ]
DEMO_IP_WHITE_LIST: List[str] = [ # 演示白名单IP
"192.168.18.7",
"192.168.1.3"
]
# ================================================= # # ================================================= #
# ***************** 静态文件配置 ***************** # # ***************** 静态文件配置 ***************** #
+2 -1
View File
@@ -77,7 +77,8 @@ class DemoEnvMiddleware(BaseHTTPMiddleware):
async def dispatch( async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint self, request: Request, call_next: RequestResponseEndpoint
) -> Response: ) -> 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") path = request.scope.get("path")
if path in settings.DEMO_BLACK_LIST_PATH: if path in settings.DEMO_BLACK_LIST_PATH:
return ErrorResponse(msg="演示环境,禁止操作",) return ErrorResponse(msg="演示环境,禁止操作",)