|
@@ -13,7 +13,7 @@ from app.utils.timezone import beijing_now
|
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.responses import JSONResponse
|
|
|
import json
|
|
import json
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
-from app.core.logger import logger
|
|
|
|
|
|
|
+from app.core.logger import logger, get_logger_by_path
|
|
|
|
|
|
|
|
|
|
|
|
|
EXEMPT_PATHS = [
|
|
EXEMPT_PATHS = [
|
|
@@ -51,6 +51,9 @@ class AuthValidationASGIMiddleware:
|
|
|
path = scope.get("path", "")
|
|
path = scope.get("path", "")
|
|
|
method = scope.get("method", "").upper()
|
|
method = scope.get("method", "").upper()
|
|
|
|
|
|
|
|
|
|
+ # 根据路径选择对应的logger
|
|
|
|
|
+ auth_logger = get_logger_by_path(path)
|
|
|
|
|
+
|
|
|
# 跳过不需要验证的路径或 CORS 预检
|
|
# 跳过不需要验证的路径或 CORS 预检
|
|
|
if method == "OPTIONS" or any(path.startswith(p) for p in EXEMPT_PATHS):
|
|
if method == "OPTIONS" or any(path.startswith(p) for p in EXEMPT_PATHS):
|
|
|
await self.app(scope, receive, send)
|
|
await self.app(scope, receive, send)
|
|
@@ -65,9 +68,9 @@ class AuthValidationASGIMiddleware:
|
|
|
|
|
|
|
|
# 临时调试日志:记录客户端传来的鉴别头,帮助定位客户端是否传递了 X-User-Id/X-User-Name 或 Authorization
|
|
# 临时调试日志:记录客户端传来的鉴别头,帮助定位客户端是否传递了 X-User-Id/X-User-Name 或 Authorization
|
|
|
try:
|
|
try:
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG HEADERS] path={path} method={method} X-User-Id={header_user_id} X-User-Name={header_user_name} AuthorizationPresent={bool(auth_header)}")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG HEADERS] path={path} method={method} X-User-Id={header_user_id} X-User-Name={header_user_name} AuthorizationPresent={bool(auth_header)}")
|
|
|
except Exception:
|
|
except Exception:
|
|
|
- logger.debug("[AuthMiddleware][DEBUG HEADERS] failed to read headers")
|
|
|
|
|
|
|
+ auth_logger.debug("[AuthMiddleware][DEBUG HEADERS] failed to read headers")
|
|
|
if not auth_header or not auth_header.startswith("Bearer "):
|
|
if not auth_header or not auth_header.startswith("Bearer "):
|
|
|
resp = JSONResponse(status_code=401, content={"code": 401, "msg": "认证信息缺失或无效"})
|
|
resp = JSONResponse(status_code=401, content={"code": 401, "msg": "认证信息缺失或无效"})
|
|
|
await resp(scope, receive, send)
|
|
await resp(scope, receive, send)
|
|
@@ -86,14 +89,14 @@ class AuthValidationASGIMiddleware:
|
|
|
await resp(scope, receive, send)
|
|
await resp(scope, receive, send)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG] Starting authentication for username: {username}, path: {path}")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG] Starting authentication for username: {username}, path: {path}")
|
|
|
|
|
|
|
|
# 查询用户或管理员
|
|
# 查询用户或管理员
|
|
|
db = SessionLocal()
|
|
db = SessionLocal()
|
|
|
try:
|
|
try:
|
|
|
user = db.query(User).filter(User.username == username, User.is_deleted == 0).first()
|
|
user = db.query(User).filter(User.username == username, User.is_deleted == 0).first()
|
|
|
if user:
|
|
if user:
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG] Found user: {user.username} (ID: {user.id})")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG] Found user: {user.username} (ID: {user.id})")
|
|
|
# 检查状态与过期
|
|
# 检查状态与过期
|
|
|
if user.status != 1:
|
|
if user.status != 1:
|
|
|
resp = JSONResponse(status_code=403, content={"code": 403, "msg": "账号已被禁用"})
|
|
resp = JSONResponse(status_code=403, content={"code": 403, "msg": "账号已被禁用"})
|
|
@@ -123,13 +126,13 @@ class AuthValidationASGIMiddleware:
|
|
|
scope["verified_user_id"] = user.id
|
|
scope["verified_user_id"] = user.id
|
|
|
scope["verified_username"] = user.username
|
|
scope["verified_username"] = user.username
|
|
|
scope["verified_is_admin"] = False
|
|
scope["verified_is_admin"] = False
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG] Set verified_user_id: {user.id} for user: {user.username}")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG] Set verified_user_id: {user.id} for user: {user.username}")
|
|
|
await self.app(scope, receive, send)
|
|
await self.app(scope, receive, send)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
admin = db.query(Admin).filter(Admin.username == username).first()
|
|
admin = db.query(Admin).filter(Admin.username == username).first()
|
|
|
if admin:
|
|
if admin:
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG] Found admin: {admin.username} (ID: {admin.id})")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG] Found admin: {admin.username} (ID: {admin.id})")
|
|
|
|
|
|
|
|
# 验证header中的用户信息是否匹配(组合校验)
|
|
# 验证header中的用户信息是否匹配(组合校验)
|
|
|
if header_user_id and header_user_name:
|
|
if header_user_id and header_user_name:
|
|
@@ -155,17 +158,17 @@ class AuthValidationASGIMiddleware:
|
|
|
scope["verified_admin_id"] = admin.id
|
|
scope["verified_admin_id"] = admin.id
|
|
|
scope["verified_username"] = admin.username
|
|
scope["verified_username"] = admin.username
|
|
|
scope["verified_is_admin"] = True
|
|
scope["verified_is_admin"] = True
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG] Set verified_admin_id: {admin.id} for admin: {admin.username}")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG] Set verified_admin_id: {admin.id} for admin: {admin.username}")
|
|
|
await self.app(scope, receive, send)
|
|
await self.app(scope, receive, send)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
# 未找到对应用户或管理员
|
|
# 未找到对应用户或管理员
|
|
|
- logger.debug(f"[AuthMiddleware][DEBUG] No user or admin found for username: {username}")
|
|
|
|
|
|
|
+ auth_logger.debug(f"[AuthMiddleware][DEBUG] No user or admin found for username: {username}")
|
|
|
resp = JSONResponse(status_code=401, content={"code": 401, "msg": "认证失败:用户不存在"})
|
|
resp = JSONResponse(status_code=401, content={"code": 401, "msg": "认证失败:用户不存在"})
|
|
|
await resp(scope, receive, send)
|
|
await resp(scope, receive, send)
|
|
|
return
|
|
return
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
- logger.exception(f"[AuthMiddleware][ERROR] Exception during authentication: {e}")
|
|
|
|
|
|
|
+ auth_logger.exception(f"[AuthMiddleware][ERROR] Exception during authentication: {e}")
|
|
|
resp = JSONResponse(status_code=500, content={"code": 500, "msg": "认证服务异常"})
|
|
resp = JSONResponse(status_code=500, content={"code": 500, "msg": "认证服务异常"})
|
|
|
await resp(scope, receive, send)
|
|
await resp(scope, receive, send)
|
|
|
return
|
|
return
|