feat: 后台管理员独立账号体系,Casdoor 配置改为后台管理
- 新增 admin_users/admin_sessions 表与 adminAuth 工具(scrypt 密码 + 独立会话 Cookie) - /admin/init 初始化首个管理员,未初始化时服务端/客户端双守卫引导 - 后台鉴权改为管理员会话,前台 Casdoor 用户一律普通用户、与后台解耦 - Casdoor 配置改存 settings 表并由「统一认证」页管理,移除全部 CASDOOR 环境变量 - 侧边栏增加统一认证/管理员入口与登出;LoginModal 感知 SSO 启用状态 - 补充 adminAuth 测试,重写 casdoor 测试,全部 23 项通过
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 后台统一认证配置页(/admin/casdoor):
|
||||
* 管理 Casdoor 登录所需配置(服务器地址、Client 凭据、证书、回调路径)。
|
||||
* 配置持久化到 settings 表;保存后前台登录入口即时生效。
|
||||
*/
|
||||
import { onMounted, reactive, ref } from 'vue'
|
||||
import { useAdminStore } from '~/stores/admin'
|
||||
import { useToastStore } from '~/stores/toast'
|
||||
|
||||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||||
|
||||
useHead({ title: '统一认证' })
|
||||
|
||||
interface CasdoorConfigForm {
|
||||
enabled: boolean
|
||||
serverUrl: string
|
||||
clientId: string
|
||||
clientSecret: string
|
||||
orgName: string
|
||||
appName: string
|
||||
certificate: string
|
||||
callbackPath: string
|
||||
}
|
||||
|
||||
const toast = useToastStore()
|
||||
const adminStore = useAdminStore()
|
||||
|
||||
const loading = ref(true)
|
||||
const loadError = ref('')
|
||||
const saving = ref(false)
|
||||
|
||||
const form = reactive<CasdoorConfigForm>({
|
||||
enabled: false,
|
||||
serverUrl: '',
|
||||
clientId: '',
|
||||
clientSecret: '',
|
||||
orgName: '',
|
||||
appName: '',
|
||||
certificate: '',
|
||||
callbackPath: '/api/auth/callback',
|
||||
})
|
||||
|
||||
const loadConfig = async () => {
|
||||
loading.value = true
|
||||
loadError.value = ''
|
||||
try {
|
||||
const data = await $fetch<{ config: CasdoorConfigForm }>('/api/admin/casdoor-config')
|
||||
Object.assign(form, data.config)
|
||||
} catch (err: any) {
|
||||
loadError.value = err?.data?.message || '配置加载失败'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(loadConfig)
|
||||
|
||||
const save = async () => {
|
||||
saving.value = true
|
||||
try {
|
||||
await $fetch('/api/admin/casdoor-config', { method: 'PATCH', body: { config: { ...form } } })
|
||||
toast.success('统一认证配置已保存')
|
||||
} catch (err: any) {
|
||||
toast.error(err?.data?.message || '保存失败,请重试')
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<AdminPageHeader title="统一认证" subtitle="配置 Casdoor 单点登录(SSO),前台登录入口将跳转认证中心">
|
||||
<template #actions>
|
||||
<AdminButton :loading="saving" :disabled="loading || !!loadError" @click="save">
|
||||
<Icon v-if="!saving" name="ph:floppy-disk" class="w-4 h-4" />
|
||||
{{ saving ? '保存中…' : '保存配置' }}
|
||||
</AdminButton>
|
||||
</template>
|
||||
</AdminPageHeader>
|
||||
|
||||
<div v-if="loading" class="space-y-6 max-w-5xl">
|
||||
<AdminCard title="">
|
||||
<AdminSkeleton :rows="6" />
|
||||
</AdminCard>
|
||||
</div>
|
||||
|
||||
<div v-else-if="loadError" class="max-w-5xl rounded-2xl bg-white border border-gray-100 shadow-sm p-10 text-center">
|
||||
<Icon name="ph:warning-circle" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
|
||||
<p class="text-gray-500 mb-4">{{ loadError }}</p>
|
||||
<AdminButton variant="secondary" @click="loadConfig">
|
||||
<Icon name="ph:arrow-clockwise" class="w-4 h-4" /> 重新加载
|
||||
</AdminButton>
|
||||
</div>
|
||||
|
||||
<div v-else class="space-y-6 max-w-5xl">
|
||||
<!-- 启用开关 -->
|
||||
<AdminCard title="启用统一认证" subtitle="开启后前台登录入口跳转 Casdoor 认证中心">
|
||||
<template #actions><Icon name="ph:plug" class="w-5 h-5 text-gray-300" /></template>
|
||||
<div class="flex items-center gap-3">
|
||||
<AdminToggle v-model="form.enabled" />
|
||||
<span class="text-sm text-gray-600">{{ form.enabled ? '已开启,前台登录走 Casdoor' : '未开启,前台登录不可用' }}</span>
|
||||
</div>
|
||||
</AdminCard>
|
||||
|
||||
<!-- 连接信息 -->
|
||||
<AdminCard title="连接信息" subtitle="必填:服务器地址、Client ID、组织名与应用名">
|
||||
<template #actions><Icon name="ph:link" class="w-5 h-5 text-gray-300" /></template>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
|
||||
<AdminInput v-model="form.serverUrl" label="服务器地址" placeholder="https://casdoor.example.com" />
|
||||
<AdminInput v-model="form.clientId" label="Client ID" />
|
||||
<AdminInput v-model="form.clientSecret" label="Client Secret" type="password" placeholder="留空则保留旧值" />
|
||||
<AdminInput v-model="form.orgName" label="组织名 (orgName)" />
|
||||
<AdminInput v-model="form.appName" label="应用名 (appName)" />
|
||||
<AdminInput v-model="form.callbackPath" label="回调路径" placeholder="/api/auth/callback" />
|
||||
</div>
|
||||
</AdminCard>
|
||||
|
||||
<!-- 证书 -->
|
||||
<AdminCard title="证书" subtitle="用于验证 Casdoor 签发的 JWT,可在认证中心复制 PEM 原文或 base64">
|
||||
<template #actions><Icon name="ph:certificate" class="w-5 h-5 text-gray-300" /></template>
|
||||
<AdminTextarea v-model="form.certificate" label="证书公钥 (JWT 证书 PEM)" :rows="6" placeholder="-----BEGIN CERTIFICATE-----……" />
|
||||
</AdminCard>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user