From 8b4a44c8d4f1e36bd0b5cb38ae11723b635268c2 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 19 Jun 2023 12:41:17 +0800 Subject: [PATCH] fix CORSMiddleware --- app/core/config.py | 2 ++ app/main.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/app/core/config.py b/app/core/config.py index 1825d42a..5dfda803 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -11,6 +11,8 @@ class Settings(BaseSettings): API_V1_STR: str = "/api/v1" # 密钥 SECRET_KEY: str = secrets.token_urlsafe(32) + # 允许的域名 + ALLOWED_HOSTS: list = ["*"] # TOKEN过期时间 ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 # 时区 diff --git a/app/main.py b/app/main.py index 75d78f9f..98bac2c3 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,6 @@ import uvicorn as uvicorn from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware from uvicorn import Config from app.api.apiv1 import api_router @@ -17,6 +18,15 @@ from app.scheduler import Scheduler App = FastAPI(title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json") +# 跨域 +App.add_middleware( + CORSMiddleware, + allow_origins=settings.ALLOWED_HOSTS, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + # API路由 App.include_router(api_router, prefix=settings.API_V1_STR)