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

+15 -18
View File
@@ -1,4 +1,5 @@
import axios from 'axios';
import { toast } from '@/hooks/useToast';
const request = axios.create({
baseURL: '/api/v1',
@@ -10,40 +11,36 @@ request.interceptors.request.use((config) => {
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
// Debug logging
console.log(
`[API] ${config.method?.toUpperCase()} ${config.baseURL}${config.url}`,
config.params ? 'params=' + JSON.stringify(config.params) : '',
config.data ? 'data=' + JSON.stringify(config.data).substring(0, 200) : ''
);
return config;
});
request.interceptors.response.use(
(response) => {
console.log(
`[API] ${response.status} ${response.config.url}`,
Array.isArray(response.data) ? `(${response.data.length} items)` : ''
);
return response.data;
},
(error) => {
const status = error.response?.status;
const url = error.config?.url;
const detail = error.response?.data?.detail;
const msg = detail || error.message || '未知错误';
if (status === 401) {
console.warn(`[API] 401 Unauthorized: ${url} — redirecting to login`);
localStorage.removeItem('token');
window.location.href = '/login';
} else if (status === 500) {
console.error(`[API] 500 Internal Error: ${url}`, detail || error.message);
} else if (status === 429) {
toast('warning', msg);
} else if (status === 422) {
console.warn(`[API] 422 Validation Error: ${url}`, error.response?.data);
// 验证错误 — 取第一个错误信息
const errors = error.response?.data?.detail;
if (Array.isArray(errors)) {
const first = errors[0];
toast('error', first?.msg || '请求参数错误');
} else {
toast('error', typeof errors === 'string' ? errors : '请求参数错误');
}
} else if (status && status >= 400) {
toast('error', msg);
} else {
console.warn(`[API] ${status || 'NETWORK'} Error: ${url}`, detail || error.message);
toast('error', '网络连接失败');
}
return Promise.reject(error);