chore: 整理项目结构,清理归档文件,更新部署配置
This commit is contained in:
+33
-10
@@ -29,6 +29,7 @@ from typing import Optional
|
||||
from urllib.parse import quote
|
||||
from uuid import UUID
|
||||
|
||||
import redis
|
||||
import redis.asyncio as aioredis
|
||||
from fastapi import APIRouter, Depends, Header, Query, Request
|
||||
from slowapi import Limiter
|
||||
@@ -169,16 +170,38 @@ async def _get_current_employee(
|
||||
# =====================================================================
|
||||
if authorization:
|
||||
token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization
|
||||
if token and redis_client:
|
||||
try:
|
||||
employee_id_bytes = await redis_client.get(f"employee:token:{token}")
|
||||
if employee_id_bytes:
|
||||
# Redis 返回 bytes,需要解码
|
||||
return employee_id_bytes.decode("utf-8") if isinstance(employee_id_bytes, bytes) else employee_id_bytes
|
||||
except AppException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Redis 读取失败: {e}")
|
||||
if token:
|
||||
if redis_client:
|
||||
try:
|
||||
employee_id_bytes = await redis_client.get(f"employee:token:{token}")
|
||||
if employee_id_bytes:
|
||||
# Redis 返回 bytes,需要解码
|
||||
return employee_id_bytes.decode("utf-8") if isinstance(employee_id_bytes, bytes) else employee_id_bytes
|
||||
except AppException:
|
||||
raise
|
||||
except redis.exceptions.TimeoutError as e:
|
||||
logger.error(f"Redis 连接超时,无法验证 Bearer Token: {e}")
|
||||
# Redis 不可用时:开发模式下降级使用 X-Employee-Id 明文头
|
||||
if x_employee_id and settings.mock_login_enabled:
|
||||
logger.warning("Redis 超时,降级使用 X-Employee-Id 明文认证(仅开发环境)")
|
||||
return x_employee_id
|
||||
# 生产环境:返回明确的业务错误码,而非让框架转 500
|
||||
raise AppException(code=1003, message="认证服务暂不可用,请稍后重试")
|
||||
except Exception as e:
|
||||
logger.error(f"Redis 读取失败,无法验证 Bearer Token: {e}")
|
||||
# Redis 不可用时:开发模式下降级使用 X-Employee-Id 明文头
|
||||
if x_employee_id and settings.mock_login_enabled:
|
||||
logger.warning("Redis 异常,降级使用 X-Employee-Id 明文认证(仅开发环境)")
|
||||
return x_employee_id
|
||||
# 生产环境:返回明确的业务错误码,而非让框架转 500
|
||||
raise AppException(code=1003, message="认证服务暂不可用,请稍后重试")
|
||||
else:
|
||||
# redis_client 为 None:dep_redis 返回了 None(Redis 连接创建失败)
|
||||
logger.error("Redis 客户端不可用,无法验证 Bearer Token")
|
||||
if x_employee_id and settings.mock_login_enabled:
|
||||
logger.warning("Redis 不可用,降级使用 X-Employee-Id 明文认证(仅开发环境)")
|
||||
return x_employee_id
|
||||
raise AppException(code=1003, message="认证服务暂不可用,请稍后重试")
|
||||
|
||||
# =====================================================================
|
||||
# 方式2:X-Employee-Id 明文头(仅开发环境,生产环境禁用)
|
||||
|
||||
Reference in New Issue
Block a user