fix(admin): 统一中文登录和默认管理员
continuous-integration/drone/push Build is passing Details

This commit is contained in:
dongjf 2026-11-30 18:46:09 +08:00
parent 31cd5a2040
commit 1d6d600952
6 changed files with 30 additions and 18 deletions

View File

@ -1,6 +1,6 @@
# 见物管理后台
用平台管理员账号登录。空间列表每页 25 条,可以按名称查找,也可输入完整空间 ID 直达详情。列表只返回名称和 ID,物品和照片数量打开详情时读取。删除需要再次输入空间 ID。服务端记录操作人和时间,并异步清理原图和缩略图。
用平台管理员账号登录。本机预览的默认账号是 `dongjf`,登录页和管理页面使用简体中文。空间列表每页 25 条,可以按名称查找,也可输入完整空间 ID 直达详情。列表只返回名称和 ID,物品和照片数量打开详情时读取。删除需要再次输入空间 ID。服务端记录操作人和时间,并异步清理原图和缩略图。
```sh
npm ci

View File

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>见物 · 空间管理</title>
<title>见物 · 管理后台</title>
</head>
<body>
<div id="app"></div>

View File

@ -16,6 +16,7 @@ const spacesApi = '/api/admin/households'
const auditApi = '/api/admin/space-deletions'
const cleanupApi = '/api/admin/media-cleanup'
const identityUrl = import.meta.env.VITE_OIDC_URL || 'https://api-atlas.tutudog.com/auth'
const loginLocale = 'zh-CN'
const keycloak = new Keycloak({
url: new URL(identityUrl, window.location.origin).href,
@ -23,6 +24,7 @@ const keycloak = new Keycloak({
clientId: 'atlas-admin',
})
const logout = () => keycloak.logout({ redirectUri: window.location.origin + '/' })
const login = () => keycloak.login({ locale: loginLocale })
const initialized = ref(false)
const authenticated = ref(false)
const authorized = ref(false)
@ -48,7 +50,7 @@ const auditDate = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeSt
onMounted(async () => {
try {
await keycloak.init({ onLoad: 'login-required', pkceMethod: 'S256', checkLoginIframe: false })
await keycloak.init({ onLoad: 'login-required', pkceMethod: 'S256', checkLoginIframe: false, locale: loginLocale })
authenticated.value = keycloak.authenticated === true
const roles: unknown = keycloak.tokenParsed?.atlas_roles
authorized.value = authenticated.value && Array.isArray(roles) && roles.includes('atlas-space-admin')
@ -218,7 +220,7 @@ async function deleteSpace() {
<header>
<h1>见物 <small>管理后台</small></h1>
<el-button v-if="authenticated" text @click="logout">退出登录</el-button>
<el-button v-else-if="initialized" text @click="keycloak.login()">重新登录</el-button>
<el-button v-else-if="initialized" text @click="login">重新登录</el-button>
</header>
<el-alert v-if="error" :title="error" type="error" show-icon :closable="false" />
<el-alert v-if="success" :title="success" type="success" show-icon :closable="false" />

View File

@ -24,16 +24,17 @@ kind_cluster=atlas-dev
context="kind-$kind_cluster"
namespace=item-atlas-admin-preview
secret_dir=/path/on/ssd/item-atlas-admin-preview
admin_password_file=/path/on/ssd/managed-preview-admin-password
mvn -B -ntp -f backend/pom.xml -pl inventory-service -am package
docker build -t item-atlas-inventory-service:admin-preview-0.1.3 backend/inventory-service
docker build --build-arg VITE_OIDC_URL=/auth \
--build-arg VITE_OIDC_REALM=item-atlas-admin-preview \
-t item-atlas-admin:admin-preview-0.1.3 apps/admin
-t item-atlas-admin:admin-preview-0.1.4 apps/admin
kind load docker-image --name "$kind_cluster" \
item-atlas-inventory-service:admin-preview-0.1.3 item-atlas-admin:admin-preview-0.1.3
item-atlas-inventory-service:admin-preview-0.1.3 item-atlas-admin:admin-preview-0.1.4
python3 deploy/admin-preview/prepare-secrets.py "$secret_dir"
python3 deploy/admin-preview/prepare-secrets.py "$secret_dir" --admin-password-file "$admin_password_file"
kubectl --context "$context" create namespace "$namespace"
for component in postgres keycloak inventory; do
kubectl --context "$context" -n "$namespace" create secret generic "atlas-admin-$component" \
@ -48,7 +49,7 @@ kubectl --context "$context" -n "$namespace" get pods,pvc
kubectl --context "$context" -n "$namespace" port-forward --address 127.0.0.1 svc/item-atlas-admin 18482:8080
```
首次部署生成两位预览用户 `preview-admin`、`preview-viewer`,密码在私有目录的 `credentials.json`。保留该目录。已有预览更新镜像后重新执行 `apply -k`,无需重新建 Secret。Keycloak 仅在首次创建 realm 时从文件导入用户。
首次部署使用 `dongjf` 作为后台管理员,密码由受控文件提供。生成的 `preview-viewer` 供权限验收,凭据位于私有目录的 `credentials.json`。后台和 Keycloak 登录统一使用简体中文。首次导入后,已有 realm 的语言、账号和角色应通过 Keycloak 管理接口更新并复验。重新生成启动文件用于新环境初始化。 [Keycloak 语言配置](https://www.keycloak.org/docs/latest/server_admin/#_user-locale-selection) · [Keycloak 管理接口](https://www.keycloak.org/docs-api/latest/rest-api/)
## 验收和排错

View File

@ -6,7 +6,7 @@ import secrets
from pathlib import Path
def prepare(directory):
def prepare(directory, admin_password_file):
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
if directory.stat().st_mode & 0o077:
raise SystemExit('Use a private directory (chmod 700) outside the Git worktree.')
@ -15,11 +15,14 @@ def prepare(directory):
if not vault.exists():
values = {
name: secrets.token_urlsafe(24)
for name in ('postgres', 'inventory', 'identity', 'preview-admin', 'preview-viewer')
for name in ('postgres', 'inventory', 'identity', 'preview-viewer')
}
with vault.open('x') as stream:
json.dump(values, stream, indent=2)
values = json.loads(vault.read_text())
admin_password = admin_password_file.read_text().rstrip('\r\n')
if not admin_password or '\r' in admin_password or '\n' in admin_password:
raise SystemExit('The administrator password file must contain exactly one non-empty line.')
write_env(directory / 'postgres.env', {
'POSTGRES_PASSWORD': values['postgres'],
@ -28,14 +31,14 @@ def prepare(directory):
})
write_env(directory / 'keycloak.env', {'KC_DB_PASSWORD': values['identity']})
write_env(directory / 'inventory.env', {'ATLAS_DB_PASSWORD': values['inventory']})
(directory / 'item-atlas-admin-preview-realm.json').write_text(json.dumps(realm(values)))
(directory / 'item-atlas-admin-preview-realm.json').write_text(json.dumps(realm(values, admin_password)))
def write_env(path, values):
path.write_text(''.join(f'{name}={value}\n' for name, value in values.items()))
def realm(values):
def realm(values, admin_password):
origin = 'http://127.0.0.1:18482'
return {
'realm': 'item-atlas-admin-preview',
@ -43,7 +46,11 @@ def realm(values):
'enabled': True,
'sslRequired': 'none',
'registrationAllowed': False,
'loginWithEmailAllowed': False,
'bruteForceProtected': True,
'internationalizationEnabled': True,
'supportedLocales': ['zh-CN'],
'defaultLocale': 'zh-CN',
'roles': {'realm': [{'name': 'atlas-space-admin'}]},
'clients': [{
'clientId': 'atlas-admin',
@ -93,13 +100,13 @@ def realm(values):
'realmRoles': roles,
'credentials': [{
'type': 'password',
'value': values[username],
'value': password,
'temporary': False,
}],
}
for username, roles in (
('preview-admin', ['atlas-space-admin']),
('preview-viewer', []),
for username, password, roles in (
('dongjf', admin_password, ['atlas-space-admin']),
('preview-viewer', values['preview-viewer'], []),
)
],
}
@ -108,5 +115,7 @@ def realm(values):
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('directory', type=Path, help='Private directory outside the Git worktree')
parser.add_argument('--admin-password-file', required=True, type=Path, help='File with the dongjf administrator password')
os.umask(0o077)
prepare(parser.parse_args().directory)
args = parser.parse_args()
prepare(args.directory, args.admin_password_file)

View File

@ -42,7 +42,7 @@ spec:
automountServiceAccountToken: false
containers:
- name: nginx
image: item-atlas-admin:admin-preview-0.1.3
image: item-atlas-admin:admin-preview-0.1.4
imagePullPolicy: Never
env:
- {name: ATLAS_GATEWAY_URL, value: 'http://atlas-admin-gateway:8080'}