SMTP 配置读取加固与日志降噪

1. 显式加载 backend/.env(不受工作目录影响),并以 utf-8-sig 读取兼容 Windows 记事本 BOM
2. 布尔配置容错:仅 false/0/no/off 视为关闭,SMTP_ENABLED 等拼写错误不再静默失效
3. 启动日志打印配置文件路径与缺失项;邮件测试/忘记密码报错直接列出缺少的配置
4. 认证中间件逐请求日志降为 DEBUG,减少'开始认证'刷屏
5. /api/config/deduction-rules 加入公开路径,修复页面 header 同步请求 401 与扣分规则回退默认值问题
This commit is contained in:
canglan committed 2026-09-20 20:46:25 +08:00
1 parent 1068bc6def
commit 05790c1882
6 files changed
+55 -30

No files matched your search

+7 -6
View File
@@ -28,6 +28,7 @@ PUBLIC_PATHS = [
r'^/api/auth/forgot-password$',
r'^/api/auth/reset-password$',
r'^/api/auth/reset-token/check$',
r'^/api/config/deduction-rules$',
]
def is_public_path(path: str) -> bool:
"""检查是否为公开路径"""
@@ -56,30 +57,30 @@ class AuthMiddleware(BaseHTTPMiddleware):
logger.debug(f"[Auth] {request.method} {path} - 公开路径,跳过认证")
return await call_next(request)
logger.info(f"[Auth] {request.method} {path} - 开始认证")
logger.debug(f"[Auth] {request.method} {path} - 开始认证")
try:
# 获取Authorization头
auth_header = request.headers.get("Authorization")
if not auth_header:
logger.warning(f"[Auth] {path} - 缺少Authorization header")
logger.debug(f"[Auth] {path} - 缺少Authorization header")
return self._cors_response(request, 401, "缺少认证令牌")
# 解析Bearer Token
try:
scheme, token = auth_header.split()
if scheme.lower() != "bearer":
logger.warning(f"[Auth] {path} - Authorization header格式错误")
logger.debug(f"[Auth] {path} - Authorization header格式错误")
return self._cors_response(request, 401, "认证格式错误")
except ValueError:
logger.warning(f"[Auth] {path} - Authorization header格式错误")
logger.debug(f"[Auth] {path} - Authorization header格式错误")
return self._cors_response(request, 401, "认证格式错误")
# 验证Token
payload = jwt_handler.verify_token(token)
if not payload:
logger.warning(f"[Auth] {path} - JWT验证失败")
logger.debug(f"[Auth] {path} - JWT验证失败")
return self._cors_response(request, 401, "令牌无效或已过期")
# 验证Redis中的Token
@@ -87,7 +88,7 @@ class AuthMiddleware(BaseHTTPMiddleware):
stored_token = await RedisClient.get_user_token(user_id)
if not stored_token or stored_token != token:
logger.warning(f"[Auth] {path} - Redis Token不匹配, user_id={user_id}, stored={'有' if stored_token else '无'}")
logger.debug(f"[Auth] {path} - Redis Token不匹配, user_id={user_id}")
return self._cors_response(request, 401, "令牌已失效,请重新登录")
# 将用户信息存储到request.state