docker build 构建修改

This commit is contained in:
2025-11-06 17:15:46 +08:00
parent 33e09afc54
commit ecdb4321e8
58 changed files with 72216 additions and 237 deletions

View File

@@ -24,25 +24,43 @@ from api.utils.api_utils import get_json_result
http_bearer = HTTPBearer(auto_error=False)
def get_current_user(credentials: Optional[HTTPAuthorizationCredentials] = Security(http_bearer)):
def get_current_user(
authorization: Optional[str] = Header(None, alias="Authorization"),
credentials: Optional[HTTPAuthorizationCredentials] = Security(http_bearer)
):
"""FastAPI 依赖注入:获取当前用户(替代 Flask 的 login_required 和 current_user
支持两种格式的 Authorization 头:
1. 标准格式Bearer <token>
2. 简化格式:<token>(不带 Bearer 前缀)
使用 Security(http_bearer) 可以让 FastAPI 自动在 OpenAPI schema 中添加安全要求,
这样 Swagger UI 就会显示授权输入框并自动在请求中添加 Authorization 头。
"""
# 延迟导入以避免循环导入
from api.apps.__init___fastapi import get_current_user_from_token
if not credentials:
token = None
# 优先从 HTTPBearer 获取标准格式Bearer <token>
if credentials:
token = credentials.credentials
# 如果 HTTPBearer 没有获取到,尝试直接从 Header 获取(可能是简化格式)
elif authorization:
# 如果包含 "Bearer " 前缀,则去除它
if authorization.startswith("Bearer "):
token = authorization[7:] # 去除 "Bearer " 前缀7个字符
else:
# 不带 Bearer 前缀,直接使用
token = authorization
if not token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authorization header is required"
)
# HTTPBearer 已经提取了 Bearer tokencredentials.credentials 就是 token 本身
authorization = credentials.credentials
user = get_current_user_from_token(authorization)
user = get_current_user_from_token(token)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,