fix(后台): 403 不再一律当未登录整页踢回登录页
拦截器把 body.code 401 和 403 并成一支,任何一个业务端点的授权缺口都会表现为 "登录坏了":整页 assign 到 /login,当前页连人带未提交的表单一起没了,下一次同类 缺口还得重新做一次归因。收窄成只有会话引导端点(current-user、current-user/menus) 的 403 才跳登录页 —— 那里 403 才真的等于"这个账号进不了后台"(如拿客户端 token 打开后台)。 业务端点的 403 交回页面自己报错,UsersPage 那句"拉失败时列表照常显示、但要把原因说出来" 从此才成立。 判定做成 envelope.ts 的纯函数 shouldRedirectForbidden,两支拦截器都过它;用例同时钉 正反两向(业务端点不跳、引导端点照跳、前缀相似不算)并断言扫描集非空。
This commit is contained in:
@@ -93,6 +93,24 @@ export function requestRelativePath(url: string): string {
|
||||
return match ? match[1] : noQuery
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话引导端点:只有这里的 403 才等于"这个账号进不了后台"(比如拿客户端 token 打开后台),
|
||||
* 除了去登录页没有别的出路。
|
||||
*
|
||||
* <p>其余端点的 403 是"已经登录、但缺这个模块的权限",和身份无关。把它按未登录整页跳走,
|
||||
* 等于把权限缺口伪装成登录故障:2026-10-04 那次"登录闪退"报的其实是
|
||||
* GET /api/admin/companies 的 403,代价是当前页连人带未提交的表单一起没了。</p>
|
||||
*/
|
||||
export const SESSION_BOOTSTRAP_PATHS: readonly string[] = [
|
||||
'/api/admin/current-user',
|
||||
'/api/admin/current-user/menus',
|
||||
]
|
||||
|
||||
/** 403 是否应执行"跳 /login":仅当失败的请求本身是会话引导端点。 */
|
||||
export function shouldRedirectForbidden(requestUrl: string | undefined): boolean {
|
||||
return SESSION_BOOTSTRAP_PATHS.includes(requestRelativePath(requestUrl || ''))
|
||||
}
|
||||
|
||||
/** 401 失败请求本身是否指向登录/认证端点(登录提交失败不应再被踢回登录页)。 */
|
||||
export function isAuthEndpointRequest(url: string | undefined): boolean {
|
||||
return requestRelativePath(url || '') === LOGIN_PATH
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
isLoginLocation,
|
||||
isUnauthorized,
|
||||
loginRedirectTarget,
|
||||
shouldRedirectForbidden,
|
||||
shouldRedirectUnauthorized,
|
||||
} from './envelope'
|
||||
|
||||
@@ -24,6 +25,12 @@ function redirectToLogin(requestUrl?: string): void {
|
||||
window.location.assign('/admin-vue/login?redirect=' + target)
|
||||
}
|
||||
|
||||
/** 403 只在会话引导端点上才当"进不了后台"处理;其余端点的 403 交给页面自己把原因说出来。 */
|
||||
function redirectToLoginOnForbidden(requestUrl?: string): void {
|
||||
if (!shouldRedirectForbidden(requestUrl)) return
|
||||
redirectToLogin(requestUrl)
|
||||
}
|
||||
|
||||
/** 单设备登录:被新设备顶下线,整页回登录页(整页 reload 顺带重置会话 store,避免守卫弹回)。 */
|
||||
function redirectToLoginKicked(): void {
|
||||
if (typeof window === 'undefined') return
|
||||
@@ -34,25 +41,25 @@ function redirectToLoginKicked(): void {
|
||||
http.interceptors.response.use(
|
||||
(response) => {
|
||||
// Java 未登录以 HTTP 200 + body.code=401 返回,需统一按未登录处理;
|
||||
// 403(已登录但无后台权限,如用工具前端账号 token 访问后台)与 401 同样跳登录页。
|
||||
// 403(已登录但无后台权限)只在会话引导端点上按"进不了后台"跳登录页,
|
||||
// 业务端点的 403 留给页面自己报错——整页跳走会把权限缺口说成登录坏了。
|
||||
// 4011(账号已在其他设备登录)单独提示,不按普通 401 处理。
|
||||
if (isKicked(response.data)) {
|
||||
redirectToLoginKicked()
|
||||
} else if (isUnauthorized(response.data) || isForbidden(response.data)) {
|
||||
} else if (isUnauthorized(response.data)) {
|
||||
redirectToLogin(response.config?.url)
|
||||
} else if (isForbidden(response.data)) {
|
||||
redirectToLoginOnForbidden(response.config?.url)
|
||||
}
|
||||
return response
|
||||
},
|
||||
(error) => {
|
||||
if (isKicked(error?.response?.data)) {
|
||||
redirectToLoginKicked()
|
||||
} else if (
|
||||
error?.response?.status === 401 ||
|
||||
error?.response?.status === 403 ||
|
||||
isUnauthorized(error?.response?.data) ||
|
||||
isForbidden(error?.response?.data)
|
||||
) {
|
||||
} else if (error?.response?.status === 401 || isUnauthorized(error?.response?.data)) {
|
||||
redirectToLogin(error?.config?.url)
|
||||
} else if (error?.response?.status === 403 || isForbidden(error?.response?.data)) {
|
||||
redirectToLoginOnForbidden(error?.config?.url)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user