feat: 首页系统状态对访客开放
/api/system/stats 改为公开只读并移除仅管理员限制(不再返回 401),前端首页对访客与管理员展示一致;README 同步权限表与接口说明。
This commit is contained in:
1 parent
f80fe0df71
commit
4375485821
4 files changed
+9
-47
No files matched your search
@@ -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` | 退出登录 |
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 登录失败节流(简单的内存计数,防暴力破解)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
+2
-2
@@ -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)
|
||||
|
||||
@@ -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<SystemStats | null>(null)
|
||||
const [error, setError] = useState(false)
|
||||
const [lastUpdated, setLastUpdated] = useState<Date | null>(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 (
|
||||
<div>
|
||||
@@ -139,16 +132,14 @@ export default function Home() {
|
||||
<div>
|
||||
<h2 className="text-2xl font-semibold text-slate-900">系统状态</h2>
|
||||
<p className="mt-1 text-sm text-slate-500">
|
||||
{!authenticated
|
||||
? '系统状态仅对管理员可见'
|
||||
: error
|
||||
{error
|
||||
? '无法获取系统信息,请确认后端服务已启动'
|
||||
: stats
|
||||
? `实时运行状态一览,每 ${REFRESH_INTERVAL / 1000} 秒自动刷新`
|
||||
: '正在获取系统信息…'}
|
||||
</p>
|
||||
</div>
|
||||
{authenticated && stats && lastUpdated && (
|
||||
{stats && lastUpdated && (
|
||||
<p className="text-xs text-slate-400">
|
||||
最后更新于 {lastUpdated.toLocaleTimeString()}
|
||||
</p>
|
||||
@@ -156,23 +147,7 @@ export default function Home() {
|
||||
</div>
|
||||
|
||||
<div className="mt-10 rounded-xl border border-slate-200 bg-white p-6 shadow-sm">
|
||||
{!authenticated ? (
|
||||
<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 ? (
|
||||
{stats ? (
|
||||
<div>
|
||||
<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">
|
||||
|
||||
Reference in new issue
Block a user