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:
logan committed 2026-05-25 12:57:59 +08:00
1 parent 0576b253e4
commit 344c519209
15 files changed
+510 -97

No files matched your search

+22 -29
View File
@@ -1,40 +1,33 @@
import { useState, useEffect, useCallback } from 'react';
import { CheckCircle, XCircle, X } from 'lucide-react';
import { useCallback, useState } from 'react';
export interface Toast {
id: number;
type: 'success' | 'error' | 'warning' | 'info';
message: string;
type: 'success' | 'error';
}
let toastId = 0;
let _addToast: ((t: Toast) => void) | null = null;
export function toast(type: Toast['type'], message: string) {
if (_addToast) {
_addToast({ id: ++toastId, type, message });
}
}
export function useToast() {
const [toast, setToast] = useState<Toast | null>(null);
const [toasts, setToasts] = useState<Toast[]>([]);
useEffect(() => {
if (!toast) return;
const t = setTimeout(() => setToast(null), 3000);
return () => clearTimeout(t);
}, [toast]);
const showToast = useCallback((message: string, type: 'success' | 'error' = 'success') => {
setToast({ message, type });
_addToast = useCallback((t: Toast) => {
setToasts(prev => [...prev, t]);
setTimeout(() => {
setToasts(prev => prev.filter(x => x.id !== t.id));
}, 4000);
}, []);
const ToastComponent = () => {
if (!toast) return null;
const isSuccess = toast.type === 'success';
return (
<div
className={`fixed bottom-6 right-6 z-[100] flex items-center gap-2 rounded-lg px-4 py-3 text-sm shadow-lg
animate-[fadeInUp_0.25s_ease-out] ${isSuccess ? 'bg-green-500/15 text-green-400' : 'bg-red-500/15 text-red-400'}`}
>
{isSuccess ? <CheckCircle className="h-4 w-4 shrink-0" /> : <XCircle className="h-4 w-4 shrink-0" />}
<span>{toast.message}</span>
<button onClick={() => setToast(null)} className="ml-1 hover:opacity-70">
<X className="h-3 w-3" />
</button>
</div>
);
};
const remove = useCallback((id: number) => {
setToasts(prev => prev.filter(x => x.id !== id));
}, []);
return { toast, showToast, ToastComponent };
return { toasts, remove };
}