feat: 管理员白名单与个性化配置权限控制

系统配置仅允许 admin_list.txt 白名单 IP 编辑;localhost 访问时解析为 LAN IP;个性化配置支持按 IP 权限隔离,新增时自动填充当前 IP。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-31 11:02:16 +08:00
co-authored by Cursor
parent 2b8cb3ca9a
commit 9bec3208f8
6 changed files with 281 additions and 42 deletions
+103 -21
View File
@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>API Proxy Mock 配置管理</title>
<title>企业级数据Mock管理系统 - by tony</title>
<link rel="stylesheet" href="/assets/element-ui/index.css" />
<style>
body {
@@ -56,6 +56,10 @@
overflow-y: auto;
max-height: calc(100vh - 120px);
}
.basic-config-readonly {
pointer-events: none;
opacity: 0.65;
}
</style>
</head>
<body>
@@ -227,11 +231,21 @@
</el-card>
</el-tab-pane>
<el-tab-pane label="基础配置" name="basic">
<el-tab-pane label="系统配置" name="basic">
<el-card class="section-card">
<div style="display:flex;justify-content:flex-end;align-items:center;margin-bottom:12px;">
<el-button size="mini" icon="el-icon-refresh" :loading="configRefreshing" @click="refreshConfig">刷新</el-button>
</div>
<el-alert
v-if="!canEditBasicConfig"
type="warning"
:closable="false"
show-icon
style="margin-bottom:16px;"
:title="'当前 IP(' + (clientIp || '未知') + ')不在管理员白名单内,无法修改系统配置'"
description="系统配置仅允许 admin_list.txt 中的 IP 访问与编辑。"
></el-alert>
<div :class="{ 'basic-config-readonly': !canEditBasicConfig }">
<el-form :model="form.config" label-width="180px">
<el-row :gutter="16">
<el-col :span="12">
@@ -294,24 +308,30 @@
</el-col>
</el-row>
</el-form>
</div>
</el-card>
</el-tab-pane>
<el-tab-pane label="IP管理" name="ip">
<el-tab-pane label="个性化配置" name="ip">
<el-card class="section-card">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
<div>
<strong>IP 访问策略</strong>
<strong>个性化配置</strong>
<div class="small-text" style="margin-top:4px;">
按客户端 IP 拉黑或配置独立目标环境(不含本地代理端口),便于开发/测试分流。
通过 localhost 访问时会自动匹配本机唯一 LAN IP 规则。
<a href="/__client-ip" target="_blank" style="color:#409EFF;">查看当前请求识别到的 IP</a>
为当前 IP 配置独立目标环境(不含本地代理端口),覆盖全局系统配置。
普通用户仅可管理 IP 为 <strong>{{ clientIp || "未知" }}</strong> 的配置;管理员可管理全部。
<a href="/__client-ip" target="_blank" style="color:#409EFF;">查看当前识别 IP</a>
</div>
</div>
<div style="display:flex;gap:8px;align-items:center;">
<el-input size="small" v-model="ipRuleSearch" placeholder="搜索 IP 或备注" clearable style="width:220px;" prefix-icon="el-icon-search"></el-input>
<el-button size="mini" icon="el-icon-refresh" :loading="ipRulesRefreshing" @click="refreshIpRules">刷新</el-button>
<el-button size="mini" type="primary" @click="openIpRuleDialogForCreate">新增 IP 规则</el-button>
<el-button
v-if="canCreateIpRule"
size="mini"
type="primary"
@click="openIpRuleDialogForCreate"
>{{ isAdmin ? "新增配置" : "配置我的环境" }}</el-button>
</div>
</div>
<el-table :data="pagedIpRules" border style="width: 100%;">
@@ -346,8 +366,11 @@
</el-table-column>
<el-table-column label="操作" width="160">
<template slot-scope="scope">
<el-button size="mini" type="primary" plain @click="openIpRuleDialogForEdit(scope.row)">修改</el-button>
<el-button size="mini" type="danger" @click="removeIpRule(scope.row)">删除</el-button>
<template v-if="canManageIpRule(scope.row)">
<el-button size="mini" type="primary" plain @click="openIpRuleDialogForEdit(scope.row)">修改</el-button>
<el-button size="mini" type="danger" @click="removeIpRule(scope.row)">删除</el-button>
</template>
<span v-else class="small-text">无权限</span>
</template>
</el-table-column>
</el-table>
@@ -619,7 +642,7 @@
</el-dialog>
<el-dialog
:title="ipRuleDialog.mode === 'edit' ? '修改 IP 规则' : '新增 IP 规则'"
:title="ipRuleDialog.mode === 'edit' ? '修改个性化配置' : (isAdmin ? '新增个性化配置' : '配置我的环境')"
:visible.sync="ipRuleDialog.visible"
width="760px"
>
@@ -627,21 +650,24 @@
<el-form-item label="IP 地址">
<el-input
v-model="ipRuleDialog.form.ip"
placeholder="如 192.168.1.100"
:disabled="ipRuleDialog.mode === 'edit'"
placeholder="自动识别当前 IP"
disabled
></el-input>
<div class="small-text" style="margin-top:4px;">
{{ ipRuleDialog.mode === 'create' ? '自动填充当前 IP,不可修改' : 'IP 地址不可修改' }}
</div>
</el-form-item>
<el-form-item label="备注">
<el-input v-model="ipRuleDialog.form.remark" placeholder="如:测试组、开发组"></el-input>
</el-form-item>
<el-form-item label="拉黑访问">
<el-form-item v-if="isAdmin" label="拉黑访问">
<el-switch v-model="ipRuleDialog.form.blocked" @change="onIpRuleBlockedChange"></el-switch>
<div class="small-text" style="margin-top:4px;">开启后该 IP 的所有请求将被拒绝(403)</div>
</el-form-item>
<template v-if="!ipRuleDialog.form.blocked">
<el-form-item label="个性化配置">
<el-switch v-model="ipRuleDialog.form.useCustomConfig"></el-switch>
<div class="small-text" style="margin-top:4px;">开启后可为此 IP 单独配置目标环境,覆盖全局基础配置</div>
<div class="small-text" style="margin-top:4px;">开启后可为此 IP 单独配置目标环境,覆盖全局系统配置</div>
</el-form-item>
<template v-if="ipRuleDialog.form.useCustomConfig">
<el-divider content-position="left">独立环境配置</el-divider>
@@ -714,6 +740,9 @@
mocksRefreshing: false,
apiRefreshing: false,
configRefreshing: false,
canEditBasicConfig: false,
clientIp: "",
isAdmin: false,
ipRulesRefreshing: false,
ipRules: [],
ipRuleSearch: "",
@@ -907,7 +936,7 @@
this.configRefreshing = true;
try {
await this.loadConfig();
this.$message.success("基础配置已刷新");
this.$message.success("系统配置已刷新");
} finally {
this.configRefreshing = false;
}
@@ -916,7 +945,7 @@
this.ipRulesRefreshing = true;
try {
await this.loadIpRules();
this.$message.success("IP 规则已刷新");
this.$message.success("个性化配置已刷新");
} finally {
this.ipRulesRefreshing = false;
}
@@ -929,16 +958,31 @@
throw new Error(data.error || "加载 IP 规则失败");
}
this.ipRules = Array.isArray(data.list) ? data.list : [];
if (data.clientIp) {
this.clientIp = data.clientIp;
}
if (data.isAdmin === true) {
this.isAdmin = true;
} else if (data.isAdmin === false) {
this.isAdmin = false;
}
this.ipRulePagination.currentPage = 1;
} catch (err) {
this.ipRules = [];
this.$message.error("加载 IP 规则失败: " + err.message);
this.$message.error("加载个性化配置失败: " + err.message);
}
},
canManageIpRule: function (rule) {
if (!rule) return false;
if (this.isAdmin) return true;
var ruleIp = (rule.ip || "").trim();
var mine = (this.clientIp || "").trim();
return ruleIp && mine && ruleIp === mine;
},
getDefaultIpRuleForm: function () {
var global = this.form.config || {};
return {
ip: "",
ip: this.clientIp || "",
remark: "",
blocked: false,
useCustomConfig: true,
@@ -967,12 +1011,23 @@
this.ipRuleDialog.form.useCustomConfig = false;
}
},
openIpRuleDialogForCreate: function () {
openIpRuleDialogForCreate: async function () {
if (!this.canCreateIpRule) {
this.$message.warning("您已有个性化配置,请直接修改");
return;
}
if (!this.clientIp) {
await this.loadIpRules();
}
this.ipRuleDialog.mode = "create";
this.ipRuleDialog.form = this.getDefaultIpRuleForm();
this.ipRuleDialog.visible = true;
},
openIpRuleDialogForEdit: function (rule) {
if (!this.canManageIpRule(rule)) {
this.$message.error("只能修改与当前 IP 匹配的个性化配置");
return;
}
var cfg = rule.config || {};
this.ipRuleDialog.mode = "edit";
this.ipRuleDialog.form = {
@@ -993,19 +1048,28 @@
submitIpRuleDialog: async function () {
var form = this.ipRuleDialog.form;
var ip = (form.ip || "").trim();
if (!this.isAdmin || this.ipRuleDialog.mode === "create") {
ip = (this.clientIp || "").trim();
form.blocked = false;
}
if (!ip) {
this.$message.error("IP 地址不能为空");
return;
}
if (!form.blocked && !form.useCustomConfig) {
if (this.isAdmin && !form.blocked && !form.useCustomConfig) {
this.$message.error("请至少启用「拉黑访问」或「个性化配置」");
return;
}
if (!this.isAdmin && !form.useCustomConfig) {
this.$message.error("请启用个性化配置");
return;
}
try {
var resp = await fetch("/__ip-rules", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
mode: this.ipRuleDialog.mode,
ip: ip,
remark: form.remark,
blocked: !!form.blocked,
@@ -1025,6 +1089,10 @@
}
},
removeIpRule: async function (rule) {
if (!this.canManageIpRule(rule)) {
this.$message.error("只能删除与当前 IP 匹配的个性化配置");
return;
}
try {
await this.$confirm("确定删除 IP「" + rule.ip + "」的规则吗?", "提示", {
type: "warning",
@@ -1353,6 +1421,11 @@
var resp = await fetch("/__config");
var data = await resp.json();
this.form.config = Object.assign({}, this.form.config, data.config || {});
this.canEditBasicConfig = data.canEditBasicConfig === true;
this.clientIp = data.clientIp || "";
if (data.canEditBasicConfig === true) {
this.isAdmin = true;
}
} catch (err) {
this.$message.error("加载配置失败: " + err.message);
}
@@ -1379,6 +1452,9 @@
}
},
saveServerConfig: async function () {
if (!this.canEditBasicConfig) {
return;
}
try {
var resp = await fetch("/__config", {
method: "POST",
@@ -1770,6 +1846,12 @@
pagedIpRules: function () {
return this.getPagedData(this.filteredIpRules, this.ipRulePagination);
},
canCreateIpRule: function () {
if (this.isAdmin) return true;
return !(this.ipRules || []).some(function (rule) {
return rule && rule.ip;
});
},
routeDialogMockFiles: function () {
var groupFilter = this.routeDialog.form.groupFilter;
if (groupFilter === null || groupFilter === undefined) {