diff --git a/README.md b/README.md index 139d816..a6f18f0 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Jenkinsfile | 容器资源占用(CPU / 内存 / 网络 / 磁盘) | ❌ | ✅ | | 端口分配、容器 ID | ❌ | ✅ | | Docker 版本 | ❌ | ✅ | -| 系统状态(CPU / 内存 / 开机时间) | ❌ | ✅ | +| 系统状态(CPU / 内存 / 开机时间) | ✅ | ✅ | 脱敏在后端完成:访客拿到的 JSON 里敏感字段已被抹掉,而不是靠前端隐藏。 @@ -30,7 +30,7 @@ Jenkinsfile | 方法 | 路径 | 说明 | | --- | --- | --- | -| GET | `/api/system/stats` | 系统状态(仅管理员,未登录返回 401) | +| GET | `/api/system/stats` | 系统状态(公开只读) | | GET | `/api/docker/stats` | Docker 容器监控(访客自动脱敏) | | POST | `/api/auth/login` | 管理员登录,成功后下发 HttpOnly 会话 Cookie | | POST | `/api/auth/logout` | 退出登录 | diff --git a/backend/auth.go b/backend/auth.go index abf226a..6422030 100644 --- a/backend/auth.go +++ b/backend/auth.go @@ -94,19 +94,6 @@ func isAuthenticated(r *http.Request) bool { return verifySessionToken(c.Value) } -// requireAuth 用于只允许管理员访问的接口。 -func requireAuth(next http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if !isAuthenticated(r) { - writeJSON(w, http.StatusUnauthorized, map[string]any{ - "error": "unauthorized", - }) - return - } - next(w, r) - } -} - // --------------------------------------------------------------------------- // 登录失败节流(简单的内存计数,防暴力破解) // --------------------------------------------------------------------------- diff --git a/backend/main.go b/backend/main.go index f12a612..a087eda 100644 --- a/backend/main.go +++ b/backend/main.go @@ -2,7 +2,7 @@ // // 这是原 Spring Boot 版本的 Go 重写,对外接口与 JSON 结构保持一致: // -// GET /api/system/stats 返回系统状态 JSON(仅管理员) +// GET /api/system/stats 返回系统状态 JSON(公开只读) // GET /api/docker/stats 返回 Docker 容器监控 JSON(见 docker.go,访客只读脱敏) // POST /api/auth/login 管理员登录 // POST /api/auth/logout 退出登录 @@ -352,7 +352,7 @@ func main() { startCPUSampler() mux := http.NewServeMux() - mux.HandleFunc("/api/system/stats", requireAuth(handleSystemStats)) + mux.HandleFunc("/api/system/stats", handleSystemStats) mux.HandleFunc("/api/docker/stats", handleDockerStats) mux.HandleFunc("/api/auth/login", handleLogin) mux.HandleFunc("/api/auth/logout", handleLogout) diff --git a/dashboard/src/pages/Home.tsx b/dashboard/src/pages/Home.tsx index 52cd8fe..4fdd8aa 100644 --- a/dashboard/src/pages/Home.tsx +++ b/dashboard/src/pages/Home.tsx @@ -1,7 +1,6 @@ import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { apiFetch } from '../lib/api' -import { useAuth } from '../lib/auth' interface CpuStats { model: string @@ -81,18 +80,12 @@ const iconProps = { } as const export default function Home() { - const { authenticated, loading, openLogin } = useAuth() const [stats, setStats] = useState(null) const [error, setError] = useState(false) const [lastUpdated, setLastUpdated] = useState(null) useEffect(() => { - // 系统状态仅管理员可见:未登录时直接跳过请求,渲染层会展示锁定提示。 - // 这里不同步清空已有 state,避免 effect 内 setState 造成级联渲染。 - if (!authenticated) { - return - } - + // 系统状态对访客与管理员都开放。 let cancelled = false async function load() { @@ -115,7 +108,7 @@ export default function Home() { cancelled = true clearInterval(timer) } - }, [authenticated]) + }, []) return (
@@ -139,16 +132,14 @@ export default function Home() {

系统状态

- {!authenticated - ? '系统状态仅对管理员可见' - : error - ? '无法获取系统信息,请确认后端服务已启动' - : stats - ? `实时运行状态一览,每 ${REFRESH_INTERVAL / 1000} 秒自动刷新` - : '正在获取系统信息…'} + {error + ? '无法获取系统信息,请确认后端服务已启动' + : stats + ? `实时运行状态一览,每 ${REFRESH_INTERVAL / 1000} 秒自动刷新` + : '正在获取系统信息…'}

- {authenticated && stats && lastUpdated && ( + {stats && lastUpdated && (

最后更新于 {lastUpdated.toLocaleTimeString()}

@@ -156,23 +147,7 @@ export default function Home() {
- {!authenticated ? ( -
-
- 🔒 -
-

需要管理员登录

-

登录后可查看 CPU、内存与开机时间等系统状态

- -
- ) : stats ? ( + {stats ? (