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)
|
||||
},
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import {
|
||||
SESSION_BOOTSTRAP_PATHS,
|
||||
isForbidden,
|
||||
isUnauthorized,
|
||||
shouldRedirectForbidden,
|
||||
} from '../src/api/envelope.ts'
|
||||
|
||||
/**
|
||||
* 403 的分流(2026-10-04 生产:"阿武登录后台闪退")。
|
||||
*
|
||||
* Java 的 BusinessException 一律 HTTP 200 + body.code,401/403 都藏在 body 里;
|
||||
* 拦截器原先把 403 与 401 并成一类整页跳 /login,于是「用户管理」页那个
|
||||
* GET /api/admin/companies 的菜单闸门 403,在管理员眼里就成了"登录坏了"。
|
||||
*/
|
||||
|
||||
test('业务端点的 403 不再按未登录处理:这就是那次闪退的出口', () => {
|
||||
for (const url of ['/api/admin/companies', '/api/admin/users', '/api/admin/shop-center']) {
|
||||
assert.equal(shouldRedirectForbidden(url), false, `${url} 的 403 不该把人踢回登录页`)
|
||||
}
|
||||
})
|
||||
|
||||
test('会话引导端点的 403 仍然跳登录页:非管理员账号进不了后台这条出路要留着', () => {
|
||||
assert.deepEqual([...SESSION_BOOTSTRAP_PATHS], [
|
||||
'/api/admin/current-user',
|
||||
'/api/admin/current-user/menus',
|
||||
])
|
||||
for (const url of SESSION_BOOTSTRAP_PATHS) {
|
||||
assert.equal(shouldRedirectForbidden(url), true, `${url} 的 403 应跳登录页`)
|
||||
}
|
||||
})
|
||||
|
||||
test('403 判定认绝对 URL 与带查询串的形态,但不认前缀相似的路径', () => {
|
||||
assert.equal(shouldRedirectForbidden('http://api.aishufu.top/api/admin/current-user'), true)
|
||||
assert.equal(shouldRedirectForbidden('/api/admin/current-user?device=web'), true)
|
||||
// 前缀像但不是同一个端点:/api/admin/current-user-anything 不该被当成会话引导
|
||||
assert.equal(shouldRedirectForbidden('/api/admin/current-users/2'), false)
|
||||
assert.equal(shouldRedirectForbidden(undefined), false)
|
||||
assert.equal(shouldRedirectForbidden(''), false)
|
||||
})
|
||||
|
||||
test('401/403 仍能各自认出,收窄 403 不会顺手把 401 也放过', () => {
|
||||
assert.equal(isUnauthorized({ code: 401 }), true)
|
||||
assert.equal(isForbidden({ code: 403 }), true)
|
||||
assert.equal(isForbidden({ status: 403 }), true)
|
||||
assert.equal(isUnauthorized({ code: 403 }), false)
|
||||
assert.equal(isForbidden({ code: 401 }), false)
|
||||
})
|
||||
|
||||
test('拦截器两支都必须先过 403 分流,且不再把 403 并进 401 那一支', () => {
|
||||
const http = readSource('src/api/http.ts')
|
||||
assert.match(http, /shouldRedirectForbidden/, '403 分支要经分流判定')
|
||||
assert.match(http, /redirectToLoginOnForbidden\(response\.config\?\.url\)/, '成功分支(200+body.code)走分流')
|
||||
assert.match(http, /redirectToLoginOnForbidden\(error\?\.config\?\.url\)/, '异常分支同样走分流')
|
||||
assert.doesNotMatch(
|
||||
http,
|
||||
/isUnauthorized\([^\)]*\)\s*\|\|\s*isForbidden/,
|
||||
'403 不许再和 401 共用一个无条件跳转分支',
|
||||
)
|
||||
// 401 那条路原样保留
|
||||
assert.match(http, /redirectToLogin\(response\.config\?\.url\)/)
|
||||
})
|
||||
Reference in New Issue
Block a user