feat: 首页系统状态对访客开放

/api/system/stats 改为公开只读并移除仅管理员限制(不再返回 401),前端首页对访客与管理员展示一致;README 同步权限表与接口说明。
This commit is contained in:
xiaozhou233 committed 2026-10-08 18:00:30 +08:00
1 parent f80fe0df71
commit 4375485821
4 files changed
+13 -51

No files matched your search

+2 -2
View File
@@ -22,7 +22,7 @@ Jenkinsfile
| 容器资源占用(CPU / 内存 / 网络 / 磁盘) | ❌ | ✅ | | 容器资源占用(CPU / 内存 / 网络 / 磁盘) | ❌ | ✅ |
| 端口分配、容器 ID | ❌ | ✅ | | 端口分配、容器 ID | ❌ | ✅ |
| Docker 版本 | ❌ | ✅ | | Docker 版本 | ❌ | ✅ |
| 系统状态(CPU / 内存 / 开机时间) | ❌ | ✅ | | 系统状态(CPU / 内存 / 开机时间) | ✅ | ✅ |
脱敏在后端完成:访客拿到的 JSON 里敏感字段已被抹掉,而不是靠前端隐藏。 脱敏在后端完成:访客拿到的 JSON 里敏感字段已被抹掉,而不是靠前端隐藏。
@@ -30,7 +30,7 @@ Jenkinsfile
| 方法 | 路径 | 说明 | | 方法 | 路径 | 说明 |
| --- | --- | --- | | --- | --- | --- |
| GET | `/api/system/stats` | 系统状态(仅管理员,未登录返回 401) | | GET | `/api/system/stats` | 系统状态(公开只读) |
| GET | `/api/docker/stats` | Docker 容器监控(访客自动脱敏) | | GET | `/api/docker/stats` | Docker 容器监控(访客自动脱敏) |
| POST | `/api/auth/login` | 管理员登录,成功后下发 HttpOnly 会话 Cookie | | POST | `/api/auth/login` | 管理员登录,成功后下发 HttpOnly 会话 Cookie |
| POST | `/api/auth/logout` | 退出登录 | | POST | `/api/auth/logout` | 退出登录 |
-13
View File
@@ -94,19 +94,6 @@ func isAuthenticated(r *http.Request) bool {
return verifySessionToken(c.Value) 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)
}
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// 登录失败节流(简单的内存计数,防暴力破解) // 登录失败节流(简单的内存计数,防暴力破解)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
+2 -2
View File
@@ -2,7 +2,7 @@
// //
// 这是原 Spring Boot 版本的 Go 重写,对外接口与 JSON 结构保持一致: // 这是原 Spring Boot 版本的 Go 重写,对外接口与 JSON 结构保持一致:
// //
// GET /api/system/stats 返回系统状态 JSON(仅管理员) // GET /api/system/stats 返回系统状态 JSON(公开只读)
// GET /api/docker/stats 返回 Docker 容器监控 JSON(见 docker.go,访客只读脱敏) // GET /api/docker/stats 返回 Docker 容器监控 JSON(见 docker.go,访客只读脱敏)
// POST /api/auth/login 管理员登录 // POST /api/auth/login 管理员登录
// POST /api/auth/logout 退出登录 // POST /api/auth/logout 退出登录
@@ -352,7 +352,7 @@ func main() {
startCPUSampler() startCPUSampler()
mux := http.NewServeMux() 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/docker/stats", handleDockerStats)
mux.HandleFunc("/api/auth/login", handleLogin) mux.HandleFunc("/api/auth/login", handleLogin)
mux.HandleFunc("/api/auth/logout", handleLogout) mux.HandleFunc("/api/auth/logout", handleLogout)
+9 -34
View File
@@ -1,7 +1,6 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import { apiFetch } from '../lib/api' import { apiFetch } from '../lib/api'
import { useAuth } from '../lib/auth'
interface CpuStats { interface CpuStats {
model: string model: string
@@ -81,18 +80,12 @@ const iconProps = {
} as const } as const
export default function Home() { export default function Home() {
const { authenticated, loading, openLogin } = useAuth()
const [stats, setStats] = useState<SystemStats | null>(null) const [stats, setStats] = useState<SystemStats | null>(null)
const [error, setError] = useState(false) const [error, setError] = useState(false)
const [lastUpdated, setLastUpdated] = useState<Date | null>(null) const [lastUpdated, setLastUpdated] = useState<Date | null>(null)
useEffect(() => { useEffect(() => {
// 系统状态仅管理员可见:未登录时直接跳过请求,渲染层会展示锁定提示。 // 系统状态对访客与管理员都开放。
// 这里不同步清空已有 state,避免 effect 内 setState 造成级联渲染。
if (!authenticated) {
return
}
let cancelled = false let cancelled = false
async function load() { async function load() {
@@ -115,7 +108,7 @@ export default function Home() {
cancelled = true cancelled = true
clearInterval(timer) clearInterval(timer)
} }
}, [authenticated]) }, [])
return ( return (
<div> <div>
@@ -139,16 +132,14 @@ export default function Home() {
<div> <div>
<h2 className="text-2xl font-semibold text-slate-900">系统状态</h2> <h2 className="text-2xl font-semibold text-slate-900">系统状态</h2>
<p className="mt-1 text-sm text-slate-500"> <p className="mt-1 text-sm text-slate-500">
{!authenticated {error
? '系统状态仅对管理员可见' ? '无法获取系统信息,请确认后端服务已启动'
: error : stats
? '无法获取系统信息,请确认后端服务已启动' ? `实时运行状态一览,每 ${REFRESH_INTERVAL / 1000} 秒自动刷新`
: stats : '正在获取系统信息…'}
? `实时运行状态一览,每 ${REFRESH_INTERVAL / 1000} 秒自动刷新`
: '正在获取系统信息…'}
</p> </p>
</div> </div>
{authenticated && stats && lastUpdated && ( {stats && lastUpdated && (
<p className="text-xs text-slate-400"> <p className="text-xs text-slate-400">
最后更新于 {lastUpdated.toLocaleTimeString()} 最后更新于 {lastUpdated.toLocaleTimeString()}
</p> </p>
@@ -156,23 +147,7 @@ export default function Home() {
</div> </div>
<div className="mt-10 rounded-xl border border-slate-200 bg-white p-6 shadow-sm"> <div className="mt-10 rounded-xl border border-slate-200 bg-white p-6 shadow-sm">
{!authenticated ? ( {stats ? (
<div className="py-12 text-center">
<div className="mx-auto grid size-14 place-items-center rounded-2xl bg-slate-100 text-2xl">
🔒
</div>
<p className="mt-4 text-sm font-medium text-slate-700">需要管理员登录</p>
<p className="mt-1 text-sm text-slate-500">登录后可查看 CPU、内存与开机时间等系统状态</p>
<button
type="button"
onClick={openLogin}
disabled={loading}
className="mt-5 rounded-lg bg-indigo-600 px-5 py-2.5 text-sm font-medium text-white transition-colors hover:bg-indigo-500 disabled:opacity-50"
>
管理员登录
</button>
</div>
) : stats ? (
<div> <div>
<div className="flex flex-wrap items-center gap-x-6 gap-y-2 border-b border-slate-100 pb-4"> <div className="flex flex-wrap items-center gap-x-6 gap-y-2 border-b border-slate-100 pb-4">
<span className="text-sm text-slate-500"> <span className="text-sm text-slate-500">