feat(容忍): requireUser 接三态——先认人、再吊销、再到期,最后才谈在跑
判定顺序就是语义:selectById 拿不到行就 401(硬删账号留下的孤儿 RUNNING 行不许继续供令牌), assertNotRevoked 挂在 assertSameDevice 之前(那条路径第一行豁免超管)。 读库失败/超时按「没有在跑」处理,方向必须是拒。 既有用例里 4 处 jwtService.parse 打桩同步补 parseWithVerdict——接缝动了测试跟着走, 不给产品留兼容层;那条「JWT 一律解析失败」的内部令牌用例映射成 INVALID, 留 null 会让 requireUser 直接 NPE。
This commit is contained in:
@@ -50,10 +50,13 @@ public class AdminAuthSupport {
|
||||
if (token == null || token.isBlank()) {
|
||||
throw new BusinessException(401, "未登录");
|
||||
}
|
||||
Claims claims = jwtService.parse(token);
|
||||
if (claims == null) {
|
||||
JwtService.ParseResult parsed = jwtService.parseWithVerdict(token);
|
||||
if (parsed.verdict() == JwtService.Verdict.INVALID) {
|
||||
throw new BusinessException(401, "登录已过期,请重新登录");
|
||||
}
|
||||
// 三态里只有「签名有效但已过期」继续往下走,且最终仍要被 tolerateExpiredSession 拦住;
|
||||
// 伪造与格式错在这里就出局,与改造前逐字同语义
|
||||
Claims claims = parsed.claims();
|
||||
Long userId;
|
||||
try {
|
||||
userId = Long.parseLong(claims.getSubject());
|
||||
@@ -62,8 +65,12 @@ public class AdminAuthSupport {
|
||||
}
|
||||
AdminUserEntity user = adminUserMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
// 硬删账号会留下孤儿在跑行:认人必须排在容忍之前,否则已删账号被自己的孤儿行继续供着
|
||||
throw new BusinessException(401, "用户不存在");
|
||||
}
|
||||
// 吊销先于单设备比对:那条路径第一行就是 if (exempt) return;(超管豁免),
|
||||
// 把吊销挂在它下面等于超管永不见吊销
|
||||
DeviceSessionPolicy.assertNotRevoked(user.getMachine(), user.getId(), user.getUsername());
|
||||
// 单设备登录:被新设备顶下线的旧 token 在此统一拦截(全站 requireUser 调用点自动生效)
|
||||
if (authProperties.isSingleDeviceEnabled()) {
|
||||
DeviceSessionPolicy.assertSameDevice(user.getMachine(), DeviceSessionPolicy.claimDeviceId(claims),
|
||||
@@ -73,9 +80,36 @@ public class AdminAuthSupport {
|
||||
// 到期拦截(含级联):管理员过期→组下账户停、公司超管过期→全公司停。
|
||||
// 任务心跳接口刻意匿名、不走本方法,过期账号的在跑任务仍能靠 alive=false 协作停止。
|
||||
accountExpiryService.assertSessionAllowed(user);
|
||||
if (parsed.isExpiredSigned()) {
|
||||
tolerateExpiredSession(user, claims);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 容忍不是宽限:只有确有任务在 15 分钟内还在跑,才让这张过期令牌再活一次。
|
||||
*
|
||||
* <p>读库失败(含 {@code timeout="2"} 超时)按「没有在跑」处理——这条分支的默认方向必须是拒绝,
|
||||
* 否则一次库抖动就把"过期即拒"变成"过期即放行"。</p>
|
||||
*
|
||||
* <p>命中打 {@code [session-tolerance]} 日志:上线后要回答"到底放行了多少次、放行了谁"只能靠它。</p>
|
||||
*/
|
||||
private void tolerateExpiredSession(AdminUserEntity user, Claims claims) {
|
||||
String deviceId = DeviceSessionPolicy.claimDeviceId(claims);
|
||||
int live;
|
||||
try {
|
||||
live = adminUserMapper.countLiveWorkFor(user.getId(), deviceId);
|
||||
} catch (RuntimeException e) {
|
||||
log.warn("[session-tolerance] 在跑判定读库失败,按拒绝处理 userId={} err={}", user.getId(), e);
|
||||
live = 0;
|
||||
}
|
||||
if (live <= 0) {
|
||||
throw new BusinessException(401, "登录已过期,请重新登录");
|
||||
}
|
||||
log.info("[session-tolerance] 容忍已过期令牌 userId={} username={} liveWork={} device={}",
|
||||
user.getId(), user.getUsername(), live, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析当前请求 JWT 中**签名的**设备标识(deviceId claim);识别不出时返回空串。
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user