feat: P0生产加固 — 认证安全+操作审计+错误提示
P0-1 认证安全: - JWT secret 改为环境变量配置, 未设置则随机生成(重启失效) - 新增 LoginRateLimitMiddleware: 5次/分钟登录频率限制(滑动窗口) - 429 响应 + 前端 toast 提示 P0-2 操作审计日志: - 新增 OperationLog 模型 (user/username/action/resource/detail/ip) - 新增 AuditLogMiddleware 自动记录 POST/PUT/DELETE/PATCH 操作 - 新增 GET /audit-logs API (分页/过滤/统计) - 前端 AuditLogsPage 对接真实 API (字段/路径修正) P0-3 实例密码加密: - 已确认 crypto.py AES-256-GCM 加密存储, 无需修改 P1 前端错误提示: - 新增 useToast hook + ToastContainer 组件 - request.ts 拦截器: 4xx/5xx 错误 toast 显示后端 detail 信息 - 422 验证错误提取第一条 msg - 网络失败显示通用提示 1182 tests passed
This commit is contained in:
1 parent
0576b253e4
commit
344c519209
15 files changed
+510
-97
No files matched your search
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
@@ -12,11 +12,16 @@ router = APIRouter(prefix="/auth", tags=["认证"])
|
||||
|
||||
|
||||
@router.post("/login", response_model=TokenResponse)
|
||||
async def login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
|
||||
async def login(req: LoginRequest, request: Request, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(User).where(User.username == req.username))
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user or not verify_password(req.password, user.hashed_password):
|
||||
# 记录登录失败
|
||||
from app.services.audit_logger import log_operation
|
||||
await log_operation(db, action="login_failed", username=req.username,
|
||||
detail=f"登录失败: 用户名或密码错误", request=request)
|
||||
await db.commit()
|
||||
raise HTTPException(status_code=401, detail="用户名或密码错误")
|
||||
|
||||
if not user.is_active:
|
||||
@@ -26,6 +31,11 @@ async def login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
|
||||
raise HTTPException(status_code=403, detail="访客账户已禁用,请联系管理员")
|
||||
|
||||
user.last_login = datetime.now(timezone.utc)
|
||||
|
||||
# 记录登录成功
|
||||
from app.services.audit_logger import log_operation
|
||||
await log_operation(db, action="login", user_id=user.id, username=user.username,
|
||||
detail=f"登录成功", request=request)
|
||||
await db.commit()
|
||||
|
||||
token = create_access_token(data={"sub": str(user.id), "role": user.role})
|
||||
|
||||
Reference in new issue
Block a user