""" 注册路由 """ from starlette.middleware.sessions import SessionMiddleware from fastapi.middleware.cors import CORSMiddleware from fastapi import FastAPI, Depends from key.v1 import router as token_router from key.v1 import login_required from api.api import router as api_router app = FastAPI() app.add_middleware(SessionMiddleware, secret_key='casper.com@2021') app.include_router(token_router) app.include_router(api_router, dependencies=[Depends(login_required)], responses={404: {'description': 'Not found!'}}) # --- 支持跨域访问 --- see: https://blog.csdn.net/qq_33801641/article/details/120540963 app.add_middleware( CORSMiddleware, allow_origins=["*"], # 允许访问的源 allow_credentials=True, # 支持 cookie allow_methods=["*"], # 允许使用的请求方法 allow_headers=["*"], # 允许携带的 Headers )