1119c9a41f
- Java:AdminUserUpdateRequest 加 disabled 开关(不能停自己、归属门沿用既有闸), AccountExpiryService/AdminAuthSupport 抛停用/删除子类型,GlobalExceptionHandler 打 X-Auth-Reason: disabled|deleted 响应头;业务码保持 401 不变,不惊动三个既有消费者 - 后台管理:用户页停用/解除停用按钮 + 已停用徽章;拦截器把成因随 &auth= 带给登录页, 登录页分停用/删除两种文案;源码钉子棘轮基线按正路重生成 - 用户端:kick-handler 按 X-Auth-Reason 分支亮停用/删除标记(互斥清理), 登录页两段专属文案且各自关自动登录;跳转目标仍是 /login?expired=1 - 反假绿:摘掉成因头注入实测 2/4 红(变异自证),还原等值后复跑回绿
74 lines
3.0 KiB
TypeScript
74 lines
3.0 KiB
TypeScript
import axios from 'axios'
|
|
import {
|
|
isForbidden,
|
|
isKicked,
|
|
isLoginLocation,
|
|
isUnauthorized,
|
|
loginRedirectTarget,
|
|
shouldRedirectForbidden,
|
|
shouldRedirectUnauthorized,
|
|
} from './envelope'
|
|
|
|
export { unwrap } from './envelope'
|
|
|
|
export const http = axios.create({
|
|
baseURL: '/',
|
|
withCredentials: true,
|
|
timeout: 30_000,
|
|
})
|
|
|
|
/** 会话失效成因(后端 X-Auth-Reason 头):disabled=已停用、deleted=已删除;其余 401 无成因。 */
|
|
function authReasonOf(headers: unknown): string {
|
|
const value = (headers as Record<string, unknown> | undefined)?.['x-auth-reason']
|
|
return value === 'disabled' || value === 'deleted' ? value : ''
|
|
}
|
|
|
|
function redirectToLogin(requestUrl?: string, reason = ''): void {
|
|
if (typeof window === 'undefined') return
|
|
// 已在登录页或失败请求即登录端点时不再跳转,避免登录页循环与吞掉登录失败反馈。
|
|
if (!shouldRedirectUnauthorized(window.location.pathname, requestUrl)) return
|
|
const target = loginRedirectTarget(window.location)
|
|
// 成因随 query 带给登录页点名(停用/删除),登录页据此显示对应提示
|
|
window.location.assign('/admin-vue/login?redirect=' + target + (reason ? `&auth=${reason}` : ''))
|
|
}
|
|
|
|
/** 403 只在会话引导端点上才当"进不了后台"处理;其余端点的 403 交给页面自己把原因说出来。 */
|
|
function redirectToLoginOnForbidden(requestUrl?: string): void {
|
|
if (!shouldRedirectForbidden(requestUrl)) return
|
|
redirectToLogin(requestUrl)
|
|
}
|
|
|
|
/** 单设备登录:被新设备顶下线,整页回登录页(整页 reload 顺带重置会话 store,避免守卫弹回)。 */
|
|
function redirectToLoginKicked(): void {
|
|
if (typeof window === 'undefined') return
|
|
if (isLoginLocation(window.location.pathname)) return
|
|
window.location.assign('/admin-vue/login?kicked=1')
|
|
}
|
|
|
|
http.interceptors.response.use(
|
|
(response) => {
|
|
// Java 未登录以 HTTP 200 + body.code=401 返回,需统一按未登录处理;
|
|
// 403(已登录但无后台权限)只在会话引导端点上按"进不了后台"跳登录页,
|
|
// 业务端点的 403 留给页面自己报错——整页跳走会把权限缺口说成登录坏了。
|
|
// 4011(账号已在其他设备登录)单独提示,不按普通 401 处理。
|
|
if (isKicked(response.data)) {
|
|
redirectToLoginKicked()
|
|
} else if (isUnauthorized(response.data)) {
|
|
redirectToLogin(response.config?.url, authReasonOf(response.headers))
|
|
} else if (isForbidden(response.data)) {
|
|
redirectToLoginOnForbidden(response.config?.url)
|
|
}
|
|
return response
|
|
},
|
|
(error) => {
|
|
if (isKicked(error?.response?.data)) {
|
|
redirectToLoginKicked()
|
|
} else if (error?.response?.status === 401 || isUnauthorized(error?.response?.data)) {
|
|
redirectToLogin(error?.config?.url, authReasonOf(error?.response?.headers))
|
|
} else if (error?.response?.status === 403 || isForbidden(error?.response?.data)) {
|
|
redirectToLoginOnForbidden(error?.config?.url)
|
|
}
|
|
return Promise.reject(error)
|
|
},
|
|
)
|