feat(停用账号): 停用/删除即强制下线并点名成因——写入口、X-Auth-Reason 头、三面接线
- Java:AdminUserUpdateRequest 加 disabled 开关(不能停自己、归属门沿用既有闸), AccountExpiryService/AdminAuthSupport 抛停用/删除子类型,GlobalExceptionHandler 打 X-Auth-Reason: disabled|deleted 响应头;业务码保持 401 不变,不惊动三个既有消费者 - 后台管理:用户页停用/解除停用按钮 + 已停用徽章;拦截器把成因随 &auth= 带给登录页, 登录页分停用/删除两种文案;源码钉子棘轮基线按正路重生成 - 用户端:kick-handler 按 X-Auth-Reason 分支亮停用/删除标记(互斥清理), 登录页两段专属文案且各自关自动登录;跳转目标仍是 /login?expired=1 - 反假绿:摘掉成因头注入实测 2/4 红(变异自证),还原等值后复跑回绿
This commit is contained in:
@@ -52,6 +52,8 @@
|
||||
<h1 class="login-title">登录</h1>
|
||||
<p v-if="kickedNotice" class="kicked-msg">该账号已在其他设备登录,本设备已下线。如非本人操作,请及时修改密码。</p>
|
||||
<p v-if="expiredNotice" class="kicked-msg">登录状态已失效(登录凭证过期,或该账号已被管理员删除),请重新登录。</p>
|
||||
<p v-if="disabledNotice" class="kicked-msg">该账号已被管理员停用,本设备已退出登录。如有疑问请联系管理员恢复后重新登录。</p>
|
||||
<p v-if="deletedNotice" class="kicked-msg">该账号已被管理员删除,本设备已退出登录。</p>
|
||||
<p v-if="errorMessage" class="error-msg">{{ errorMessage }}</p>
|
||||
<form id="loginForm" @submit.prevent="submitLogin">
|
||||
<div class="form-group">
|
||||
@@ -112,7 +114,7 @@
|
||||
import { onMounted, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { loginWithDevice } from '@/shared/api/user'
|
||||
import { KICK_NOTICE_KEY, SESSION_EXPIRED_NOTICE_KEY } from '@/shared/auth/kick-handler'
|
||||
import { ACCOUNT_DELETED_NOTICE_KEY, ACCOUNT_DISABLED_NOTICE_KEY, KICK_NOTICE_KEY, SESSION_EXPIRED_NOTICE_KEY } from '@/shared/auth/kick-handler'
|
||||
import { COMPANY_NAME_STORAGE_KEY, writeCompanyIdentity } from '@/shared/auth/company-identity'
|
||||
import { clearAccountExpiry, writeAccountExpiry } from '@/shared/auth/account-expiry'
|
||||
import { useVersionUpdate } from '@/shared/composables/useVersionUpdate'
|
||||
@@ -137,6 +139,8 @@ const loggingIn = ref(false)
|
||||
const errorMessage = ref('')
|
||||
const kickedNotice = ref(false)
|
||||
const expiredNotice = ref(false)
|
||||
const disabledNotice = ref(false)
|
||||
const deletedNotice = ref(false)
|
||||
const rememberPassword = ref(false)
|
||||
const autoLogin = ref(false)
|
||||
const updateOpen = ref(false)
|
||||
@@ -295,6 +299,16 @@ function isSessionExpiredNav(): boolean {
|
||||
return lsGet(SESSION_EXPIRED_NOTICE_KEY) === '1'
|
||||
}
|
||||
|
||||
/** 账号被管理员停用后跳回登录页:标记由 kick-handler 按后端 X-Auth-Reason 写入 */
|
||||
function isAccountDisabledNav(): boolean {
|
||||
return lsGet(ACCOUNT_DISABLED_NOTICE_KEY) === '1'
|
||||
}
|
||||
|
||||
/** 账号被删除后跳回登录页:同上,与停用/过期分开存 */
|
||||
function isAccountDeletedNav(): boolean {
|
||||
return lsGet(ACCOUNT_DELETED_NOTICE_KEY) === '1'
|
||||
}
|
||||
|
||||
function togglePassword() {
|
||||
passwordVisible.value = !passwordVisible.value
|
||||
}
|
||||
@@ -510,12 +524,27 @@ onMounted(() => {
|
||||
lsRemove(SESSION_EXPIRED_NOTICE_KEY)
|
||||
}
|
||||
|
||||
if (isAccountDisabledNav()) {
|
||||
// 账号已被管理员停用:同样关自动登录,标记展示后即清。
|
||||
disabledNotice.value = true
|
||||
autoLogin.value = false
|
||||
lsSet(AUTO_LOGIN_KEY, '0')
|
||||
lsRemove(ACCOUNT_DISABLED_NOTICE_KEY)
|
||||
}
|
||||
|
||||
if (isAccountDeletedNav()) {
|
||||
deletedNotice.value = true
|
||||
autoLogin.value = false
|
||||
lsSet(AUTO_LOGIN_KEY, '0')
|
||||
lsRemove(ACCOUNT_DELETED_NOTICE_KEY)
|
||||
}
|
||||
|
||||
// 恢复记住的凭据并尝试自动登录(桌面与网页形态一致;登出/切号导航由 tryAutoLogin 内部豁免)
|
||||
// loadCredentials 现为异步(AES 解密),须先恢复密码再触发自动登录
|
||||
void (async () => {
|
||||
await loadCredentials()
|
||||
if (kickedNotice.value || expiredNotice.value) {
|
||||
// 刚被顶下线或登录态失效:不自动重登,等用户手动点登录(此时顶掉对方,最后登录者胜)
|
||||
if (kickedNotice.value || expiredNotice.value || disabledNotice.value || deletedNotice.value) {
|
||||
// 刚被顶下线/登录态失效/停用/删除:不自动重登,等用户手动点登录(此时顶掉对方,最后登录者胜)
|
||||
autoLogin.value = false
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import axios, {
|
||||
type AxiosResponse,
|
||||
type InternalAxiosRequestConfig,
|
||||
} from 'axios'
|
||||
import { handleKicked, handleSessionExpired, isKickedPayload, isUnauthorizedPayload } from '../auth/kick-handler.ts'
|
||||
import { authReasonOf, handleKicked, handleSessionExpired, isKickedPayload, isUnauthorizedPayload } from '../auth/kick-handler.ts'
|
||||
|
||||
export interface LegacyApiSuccess<T> {
|
||||
success: true
|
||||
@@ -61,12 +61,17 @@ function responseStatusOf(error: unknown): unknown {
|
||||
return (error as { response?: { status?: unknown } } | null | undefined)?.response?.status
|
||||
}
|
||||
|
||||
/** 从 axios 错误对象里取响应头(兼容真实 AxiosError 与测试桩)。 */
|
||||
function responseHeadersOf(error: unknown): unknown {
|
||||
return (error as { response?: { headers?: unknown } } | null | undefined)?.response?.headers
|
||||
}
|
||||
|
||||
/** 响应成功分支:Java 业务错误是 HTTP 200 + body.code,下线收口必须在这里也判。 */
|
||||
export function onResponseFulfilled(response: AxiosResponse): AxiosResponse {
|
||||
if (isKickedPayload(response.data)) {
|
||||
handleKicked()
|
||||
} else if (isUnauthorizedPayload(response.data)) {
|
||||
handleSessionExpired()
|
||||
handleSessionExpired(authReasonOf(response.headers))
|
||||
}
|
||||
return response
|
||||
}
|
||||
@@ -77,7 +82,7 @@ export function onResponseRejected(error: unknown): Promise<never> {
|
||||
if (isKickedPayload(payload)) {
|
||||
handleKicked()
|
||||
} else if (isUnauthorizedPayload(payload) || responseStatusOf(error) === 401) {
|
||||
handleSessionExpired()
|
||||
handleSessionExpired(authReasonOf(responseHeadersOf(error)))
|
||||
}
|
||||
return Promise.reject(new Error(extractErrorMessage(error)))
|
||||
}
|
||||
|
||||
@@ -28,6 +28,21 @@ export const UNAUTHORIZED_CODE = 401
|
||||
/** 登录页据此标记显示"登录已失效"。与互踢分开存:两种成因不能互相冒充。 */
|
||||
export const SESSION_EXPIRED_NOTICE_KEY = 'aiimage_session_expired_notice'
|
||||
|
||||
/** 登录页据此标记显示"账号已被停用"。与其它成因分开存:四种成因不能互相冒充。 */
|
||||
export const ACCOUNT_DISABLED_NOTICE_KEY = 'aiimage_account_disabled_notice'
|
||||
|
||||
/** 登录页据此标记显示"账号已被删除"。 */
|
||||
export const ACCOUNT_DELETED_NOTICE_KEY = 'aiimage_account_deleted_notice'
|
||||
|
||||
/** 后端 X-Auth-Reason 头携带的会话失效成因;空串=未点名(令牌过期等)。 */
|
||||
export type AuthReason = 'disabled' | 'deleted' | ''
|
||||
|
||||
/** 从 axios 响应头里读会话失效成因(axios 归一化为全小写键名)。 */
|
||||
export function authReasonOf(headers: unknown): AuthReason {
|
||||
const value = (headers as Record<string, unknown> | null | undefined)?.['x-auth-reason']
|
||||
return value === 'disabled' || value === 'deleted' ? value : ''
|
||||
}
|
||||
|
||||
/** 本次页面生命周期内是否已发起跳转(并发 4011 只跳一次;整页跳转后 window 重建自然重置)。 */
|
||||
type KickWindow = Window & { __aiimageKickRedirected?: boolean }
|
||||
|
||||
@@ -96,12 +111,14 @@ function clearLocalSession(): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** 亮出这一种成因的标记,同时清掉另一种:两个标记都在时登录页会说出错的那句。 */
|
||||
function setAuthNotice(key: string, otherKey: string): void {
|
||||
/** 亮出这一种成因的标记,同时清掉其它三种:多个标记都在时登录页会说出错的那句。 */
|
||||
function setAuthNotice(key: string, otherKeys: readonly string[]): void {
|
||||
try {
|
||||
const storage = window.localStorage
|
||||
storage.setItem(key, '1')
|
||||
storage.removeItem(otherKey)
|
||||
for (const other of otherKeys) {
|
||||
storage.removeItem(other)
|
||||
}
|
||||
} catch {
|
||||
/* 忽略存储异常 */
|
||||
}
|
||||
@@ -127,7 +144,7 @@ export function handleKicked(): void {
|
||||
return
|
||||
}
|
||||
clearLocalSession()
|
||||
setAuthNotice(KICK_NOTICE_KEY, SESSION_EXPIRED_NOTICE_KEY)
|
||||
setAuthNotice(KICK_NOTICE_KEY, [SESSION_EXPIRED_NOTICE_KEY, ACCOUNT_DISABLED_NOTICE_KEY, ACCOUNT_DELETED_NOTICE_KEY])
|
||||
clearDesktopCurrentUid()
|
||||
redirectToLogin('/login?kicked=1', '[auth] 账号已在其他设备登录,本设备下线,跳转登录页')
|
||||
}
|
||||
@@ -141,7 +158,7 @@ export function handleKicked(): void {
|
||||
* 只在"本端自认为已登录"时收口:匿名请求拿 401 是常态噪声(未登录打开首页、登录前的
|
||||
* 引导接口),为它清存储并整页跳转,会把一次无害的 401 变成"页面自己弹回登录页"。
|
||||
*/
|
||||
export function handleSessionExpired(): void {
|
||||
export function handleSessionExpired(reason: AuthReason = ''): void {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
@@ -149,7 +166,15 @@ export function handleSessionExpired(): void {
|
||||
return
|
||||
}
|
||||
clearLocalSession()
|
||||
setAuthNotice(SESSION_EXPIRED_NOTICE_KEY, KICK_NOTICE_KEY)
|
||||
// 成因点名:后端 X-Auth-Reason 说停用/删除就亮对应的标记,登录页照它说话;
|
||||
// 没带成因(旧 jar 部署窗口、非本成因的 401)回落到通用「登录已失效」。
|
||||
if (reason === 'disabled') {
|
||||
setAuthNotice(ACCOUNT_DISABLED_NOTICE_KEY, [KICK_NOTICE_KEY, SESSION_EXPIRED_NOTICE_KEY, ACCOUNT_DELETED_NOTICE_KEY])
|
||||
} else if (reason === 'deleted') {
|
||||
setAuthNotice(ACCOUNT_DELETED_NOTICE_KEY, [KICK_NOTICE_KEY, SESSION_EXPIRED_NOTICE_KEY, ACCOUNT_DISABLED_NOTICE_KEY])
|
||||
} else {
|
||||
setAuthNotice(SESSION_EXPIRED_NOTICE_KEY, [KICK_NOTICE_KEY, ACCOUNT_DISABLED_NOTICE_KEY, ACCOUNT_DELETED_NOTICE_KEY])
|
||||
}
|
||||
clearDesktopCurrentUid()
|
||||
redirectToLogin('/login?expired=1', '[auth] 登录态已失效,本设备下线,跳转登录页')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { resolve, dirname } from 'node:path'
|
||||
import { onResponseFulfilled, onResponseRejected } from '../src/shared/api/http.ts'
|
||||
import {
|
||||
ACCOUNT_DELETED_NOTICE_KEY,
|
||||
ACCOUNT_DISABLED_NOTICE_KEY,
|
||||
KICK_NOTICE_KEY,
|
||||
SESSION_EXPIRED_NOTICE_KEY,
|
||||
UNAUTHORIZED_CODE,
|
||||
authReasonOf,
|
||||
} from '../src/shared/auth/kick-handler.ts'
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
/** 登录页是 .vue 组件,本树无挂载测试(无 @vue/test-utils),按既有惯例用源码尺子钉。 */
|
||||
const loginPageSource = readFileSync(
|
||||
resolve(here, '../src/pages/login/DesktopLoginPage.vue'),
|
||||
'utf-8',
|
||||
)
|
||||
|
||||
function createStorage(initial: Record<string, string> = {}) {
|
||||
const store = new Map(Object.entries(initial))
|
||||
return {
|
||||
getItem: (key: string) => (store.has(key) ? (store.get(key) as string) : null),
|
||||
setItem: (key: string, value: string) => void store.set(key, String(value)),
|
||||
removeItem: (key: string) => void store.delete(key),
|
||||
}
|
||||
}
|
||||
|
||||
function setupWindow(initial: Record<string, string> = {}, pathname = '/home') {
|
||||
const localStorage = createStorage(initial)
|
||||
const assigned: string[] = []
|
||||
;(globalThis as Record<string, unknown>).window = {
|
||||
localStorage,
|
||||
location: {
|
||||
pathname,
|
||||
search: '',
|
||||
assign: (url: string) => {
|
||||
assigned.push(url)
|
||||
},
|
||||
},
|
||||
addEventListener: () => undefined,
|
||||
}
|
||||
return { localStorage, assigned }
|
||||
}
|
||||
|
||||
function loggedInStorage() {
|
||||
return {
|
||||
aiimage_auth_token: 'jwt-token',
|
||||
uid: '1206',
|
||||
username: '先锋',
|
||||
aiimage_auto_login: '1',
|
||||
}
|
||||
}
|
||||
|
||||
/** 后端把「停用/删除/过期」都编译成 HTTP 200 + body.code=401;成因在 X-Auth-Reason 头上。 */
|
||||
function unauthorizedEnvelope(message = '账号已停用,请联系管理员') {
|
||||
return { success: false, code: UNAUTHORIZED_CODE, message }
|
||||
}
|
||||
|
||||
test('authReasonOf 只认 disabled/deleted,其余回落空串', () => {
|
||||
assert.equal(authReasonOf({ 'x-auth-reason': 'disabled' }), 'disabled')
|
||||
assert.equal(authReasonOf({ 'x-auth-reason': 'deleted' }), 'deleted')
|
||||
assert.equal(authReasonOf({ 'x-auth-reason': 'expired' }), '')
|
||||
assert.equal(authReasonOf({}), '')
|
||||
assert.equal(authReasonOf(undefined), '')
|
||||
})
|
||||
|
||||
test('停用必须强制下线且点名停用:清登录态、亮停用标记、清掉其它成因', () => {
|
||||
const { localStorage, assigned } = setupWindow({
|
||||
...loggedInStorage(),
|
||||
[KICK_NOTICE_KEY]: '1',
|
||||
})
|
||||
|
||||
onResponseFulfilled({ data: unauthorizedEnvelope(), headers: { 'x-auth-reason': 'disabled' } })
|
||||
|
||||
assert.equal(localStorage.getItem('aiimage_auth_token'), null, '停用不清令牌=用户停在原页面')
|
||||
assert.equal(localStorage.getItem('aiimage_auto_login'), '0', '不关自动登录=被停用账号每次启动静默重试')
|
||||
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), '1')
|
||||
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null, '停用不能说成被顶下线')
|
||||
assert.deepEqual(assigned, ['/login?expired=1'])
|
||||
})
|
||||
|
||||
test('删除点名:亮删除标记并清掉停用/互踢标记', () => {
|
||||
const { localStorage } = setupWindow({
|
||||
...loggedInStorage(),
|
||||
[ACCOUNT_DISABLED_NOTICE_KEY]: '1',
|
||||
})
|
||||
|
||||
onResponseFulfilled({ data: unauthorizedEnvelope('用户不存在'), headers: { 'x-auth-reason': 'deleted' } })
|
||||
|
||||
assert.equal(localStorage.getItem(ACCOUNT_DELETED_NOTICE_KEY), '1')
|
||||
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), null, '删除不能说成停用')
|
||||
assert.equal(localStorage.getItem(KICK_NOTICE_KEY), null)
|
||||
})
|
||||
|
||||
test('401 头里没有成因:回落通用「登录已失效」,不得误亮停用/删除标记', () => {
|
||||
const { localStorage } = setupWindow(loggedInStorage())
|
||||
|
||||
onResponseFulfilled({ data: unauthorizedEnvelope('登录已过期,请重新登录') })
|
||||
|
||||
assert.equal(localStorage.getItem(SESSION_EXPIRED_NOTICE_KEY), '1')
|
||||
assert.equal(localStorage.getItem(ACCOUNT_DISABLED_NOTICE_KEY), null)
|
||||
assert.equal(localStorage.getItem(ACCOUNT_DELETED_NOTICE_KEY), null)
|
||||
})
|
||||
|
||||
test('成因只改标记不改收口动作:带头 401 照样跳 /login?expired=1', async () => {
|
||||
const { assigned } = setupWindow(loggedInStorage())
|
||||
|
||||
await assert.rejects(
|
||||
() => onResponseRejected({
|
||||
response: { status: 401, data: { message: '' }, headers: { 'x-auth-reason': 'disabled' } },
|
||||
message: '请求失败',
|
||||
}),
|
||||
/请求失败/,
|
||||
)
|
||||
|
||||
assert.deepEqual(assigned, ['/login?expired=1'])
|
||||
})
|
||||
|
||||
// ---------- 登录页接线(.vue 无挂载测试,源码尺子钉) ----------
|
||||
|
||||
test('登录页认停用/删除标记:各有判定入口、各自关自动登录', () => {
|
||||
assert.match(loginPageSource, /function isAccountDisabledNav\(\): boolean/, '缺停用判定入口')
|
||||
assert.match(loginPageSource, /function isAccountDeletedNav\(\): boolean/, '缺删除判定入口')
|
||||
assert.match(loginPageSource, /lsGet\(ACCOUNT_DISABLED_NOTICE_KEY\)/)
|
||||
assert.match(loginPageSource, /lsGet\(ACCOUNT_DELETED_NOTICE_KEY\)/)
|
||||
assert.match(loginPageSource, /disabledNotice\.value = true/, '判定到了却没显示停用提示')
|
||||
assert.match(loginPageSource, /deletedNotice\.value = true/, '判定到了却没显示删除提示')
|
||||
// 自动登录豁免必须包含全部四种成因:漏一种,那种账号每次启动就静默重登
|
||||
assert.match(
|
||||
loginPageSource,
|
||||
/kickedNotice\.value \|\| expiredNotice\.value \|\| disabledNotice\.value \|\| deletedNotice\.value/,
|
||||
'自动登录豁免漏了停用/删除成因',
|
||||
)
|
||||
})
|
||||
|
||||
test('停用/删除文案必须互不冒充:停用句不说删除、删除句不说停用', () => {
|
||||
const disabledLine = loginPageSource
|
||||
.split('\n')
|
||||
.find((line) => line.includes('v-if="disabledNotice"')) || ''
|
||||
const deletedLine = loginPageSource
|
||||
.split('\n')
|
||||
.find((line) => line.includes('v-if="deletedNotice"')) || ''
|
||||
assert.ok(disabledLine, '模板里没有 disabledNotice 那一行')
|
||||
assert.ok(deletedLine, '模板里没有 deletedNotice 那一行')
|
||||
assert.match(disabledLine, /停用/)
|
||||
assert.ok(!disabledLine.includes('删除'), '停用提示不能说成被删除')
|
||||
assert.match(deletedLine, /删除/)
|
||||
assert.ok(!deletedLine.includes('停用'), '删除提示不能说成被停用')
|
||||
})
|
||||
Reference in New Issue
Block a user