401 认证处理
自动跳转与 skipAuthRedirect 禁用跳转对比
401 认证处理
演示 401 未认证错误的处理:hc 模式下 401 响应经 unwrapData 抛出 BusinessError(code=401)。 retry / 401 跳转为客户端全局配置(见 service/rpc.ts),下方切换演示调用形态。
认证跳转模式:
注意:默认模式下遇到 401 会自动跳转至 /login
客户端:window.location.href = '/login' | 服务端:redirect('/login')
401 错误 (自动跳转)
默认行为,收到 401 后自动跳转登录页
GET
/api/example/request/auth请求配置
{
"context": {
"skipAuthRedirect": false
}
}认证成功
mode=success,模拟已认证状态,正常返回用户数据
GET
/api/example/request/auth?mode=success请求配置
{
"params": {
"mode": "success"
}
}拦截器中的 401 处理逻辑
客户端拦截器
if (response.status === 401 && !options.context?.skipAuthRedirect) {
window.location.href = '/login'
}服务端拦截器
if (response.status === 401 && !options.context?.skipAuthRedirect) {
redirect('/login')
}