CORS最后一commit

This commit is contained in:
高子兴 2024-11-20 22:08:21 +08:00
parent 298830cd1e
commit b2fbe2cf4a
2 changed files with 10 additions and 2 deletions

View File

@ -43,7 +43,14 @@ async def login(response: Response, user_data: dict, session: SessionDep):
token = create_access_token(data={"id": user.id, "role": user.role, "tanant_id": user.tenant.id})
# 设置cookie
response.set_cookie(key="session_token", value=token, httponly=True)
response.set_cookie(
key="session_token",
value=token,
httponly=True,
# domain=".ecs2.heshunme.xyz", # 确保域正确
samesite="none", # 根据需要设置samesite属性
secure=True,
)
# 关闭数据库会话
session.close()

View File

@ -24,7 +24,8 @@ app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许所有来源,也可以指定具体的来源,例如 ["http://example.com", "https://example.com"]
# allow_origins=["*"],
allow_origins=["http://localhost:8080","http://localhost:5000"], # 允许所有来源,也可以指定具体的来源,例如 ["http://example.com", "https://example.com"]
allow_credentials=True, # 允许携带凭证如cookies
allow_methods=["*"], # 允许所有方法,也可以指定具体的方法,例如 ["GET", "POST", "PUT", "DELETE"]
allow_headers=["*"], # 允许所有头部,也可以指定具体的头部,例如 ["Content-Type", "Authorization"]