From 3059f9c2598d9251398bdfe93067a9a81d6db99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E5=B0=8F=E4=B8=98?= Date: Tue, 13 Jan 2026 15:32:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=20CORS=20=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=BC=98=E5=8C=96=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index d9107e9..69edc60 100644 --- a/main.py +++ b/main.py @@ -33,16 +33,6 @@ app = FastAPI( debug=appmeta.debug, openapi_url="/openapi.json" if appmeta.debug else None, ) - -# 配置 CORS 中间件 -app.add_middleware( - CORSMiddleware, - allow_origins=["*"], # 开发环境允许所有来源,生产环境应该限制为具体域名 - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) -======= # 添加跨域 CORS 中间件,仅在调试模式下启用,以允许所有来源访问 API if appmeta.debug: from fastapi.middleware.cors import CORSMiddleware @@ -53,10 +43,12 @@ if appmeta.debug: allow_methods=["*"], allow_headers=["*"], ) ->>>>>>> d2c914cff89e566b0b7ab7a2655ae72905f888f5 @app.exception_handler(Exception) -async def handle_unexpected_exceptions(request: Request, exc: Exception) -> NoReturn: +async def handle_unexpected_exceptions( + request: Request, + exc: Exception +) -> NoReturn: """ 捕获所有未经处理的 FastAPI 异常,防止敏感信息泄露。 """