fix(cr-01/02/03/08/09/10/11/13): 会话世代、损耗共享基线、任务活性与工作台包

- CR-01:users.session_epoch(V172)+ JWT sessionEpoch;登录/登出原子推进,
  check_login/requireUser 先比世代再做设备比对;登出不再依赖 machine CAS
- CR-02/09:cost-guard 基线落 Redis(CostGuardBaselineStore),按 elapsed 归一化;
  非持锁实例刷新共享密钥快照;pause() 返回写成功,失败不通知且不推进基线
- CR-03:stuck_requeue_count(V173,上限 20)与 retry_count 分账;终态父任务心跳
  停止 touchRunning 并 markFailedPermanent;良性停续期 markLost=false
- CR-08:工作台包对象 key 加 UUID、0 字节拒绝、同 objectKey 幂等、同版本重复拒绝、
  前端 confirm 超时抬到 10 分钟
- CR-10:Excel onSheetTotal 统一在 onHeader 之后回调(xlsx/xls)
- CR-13/11:迁移头/文件名一致断言(V170 唯一豁免);V167 前置由 CompanyService 同事务
  复制菜单树,已应用迁移未回改
This commit is contained in:
2026-10-25 03:40:15 +08:00
parent 112c61afa8
commit c2f429f112
43 changed files with 1003 additions and 95 deletions
@@ -3,7 +3,14 @@ package com.nanri.aiimage.modules.auth.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface LoginUserMapper extends BaseMapper<LoginUserEntity> {
/**
* 会话世代 +1(登录、登出各调用一次)。原子自增而不是「读出来再写回」:
* 双节点同时登录时后者不能把前者的世代覆盖回去。返回 0 行表示用户已不存在。
*/
int incrementSessionEpoch(@Param("userId") Long userId);
}
@@ -43,4 +43,10 @@ public class LoginUserEntity {
*/
@TableField("disabled_at")
private LocalDateTime disabledAt;
/**
* 会话世代(V172 新增):登录/登出各 +1,JWT 的 epoch claim 必须与它相等。
* 与 {@code AdminUserEntity.sessionEpoch} 同列,改一处必须同改两处。
*/
@TableField("session_epoch")
private Long sessionEpoch;
}
@@ -116,6 +116,7 @@ public class AuthService {
accountExpiryService.assertLoginAllowed(user);
boolean isAdmin = user.getIsAdmin() != null && user.getIsAdmin() == 1;
advanceSessionEpoch(user);
// 单设备登录:登录成功即把账号绑定到当前设备(last-login-wins),原设备下一次请求被顶下线
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.logBindOnLogin(user.getMachine(), deviceId, user.getId(), user.getUsername());
@@ -125,7 +126,8 @@ public class AuthService {
} else if (DeviceSessionPolicy.isRevoked(user.getMachine())) {
// 开关关掉后登录不再写 machine,登出留下的吊销标记就没人清:此后每次登录都会先成功、
// 再被 requireUser 的 assertNotRevoked 判成「登录已过期」,账号永久锁死。
// 登录成功即视为新会话,清空标记(空=未绑定,放行语义);仍不写设备号。
// 登录成功即视为新会话(上面已推进 session_epoch,旧令牌另有世代校验兜底),
// 清空标记(空=未绑定,放行语义);仍不写设备号。
log.warn("[auth] 登录清除登出吊销标记 userId={} username={}", user.getId(), user.getUsername());
loginUserMapper.update(null, new LambdaUpdateWrapper<LoginUserEntity>()
.eq(LoginUserEntity::getId, user.getId())
@@ -149,6 +151,9 @@ public class AuthService {
if (userId == null) {
return;
}
// 世代 +1 是真正的吊销;它不依赖 machine 是否等于本次 deviceId,
// 因此 machine 为空(单设备关闭时的常态)也一定留得下吊销效果。
int epochUpdated = loginUserMapper.incrementSessionEpoch(userId);
String marker = DeviceSessionPolicy.REVOKED_PREFIX + userId + "#"
+ java.time.LocalDateTime.now().format(REVOKE_STAMP);
String bound = deviceId == null ? "" : deviceId.trim();
@@ -156,7 +161,8 @@ public class AuthService {
.set(LoginUserEntity::getMachine, marker)
.eq(LoginUserEntity::getId, userId)
.eq(!bound.isEmpty(), LoginUserEntity::getMachine, bound));
log.info("[auth] 登出吊销 userId={} deviceBound={} updated={}", userId, bound, updated);
log.info("[auth] 登出吊销 userId={} deviceBound={} epochUpdated={} markerUpdated={}",
userId, bound, epochUpdated, updated);
}
/** 从令牌里取出身份再吊销;解析不出(已过期/伪造)就什么都不做——cookie 由调用方清。 */
@@ -173,7 +179,20 @@ public class AuthService {
log.warn("[auth] 登出时 subject 非数字,跳过吊销(cookie 仍由调用方清)");
return;
}
logout(Long.parseLong(subject), DeviceSessionPolicy.claimDeviceId(claims));
Long userId = Long.parseLong(subject);
LoginUserEntity current = loginUserMapper.selectById(userId);
if (current == null) {
return;
}
// 旧令牌不能把新会话一起登出:只有当前世代的令牌才有权推进世代。
long claimEpoch = DeviceSessionPolicy.claimSessionEpoch(claims);
long storedEpoch = current.getSessionEpoch() == null ? 0L : current.getSessionEpoch();
if (claimEpoch != storedEpoch) {
log.warn("[auth] 登出令牌的会话世代已过期,跳过吊销 userId={} stored={} claim={}",
userId, storedEpoch, claimEpoch);
return;
}
logout(userId, DeviceSessionPolicy.claimDeviceId(claims));
}
public LoginResultVo checkLogin(String token, String currentDeviceId) {
@@ -196,6 +215,9 @@ public class AuthService {
}
boolean isAdmin = user.getIsAdmin() != null && user.getIsAdmin() == 1;
String claimDeviceId = DeviceSessionPolicy.claimDeviceId(claims);
// 世代校验先于设备比对:登出/新登录后旧 token 在这里直接 401,不能靠 check_login 续期复活
DeviceSessionPolicy.assertSessionEpoch(user.getSessionEpoch(),
DeviceSessionPolicy.claimSessionEpoch(claims), user.getId(), user.getUsername());
// 单设备登录:不拦截的话,被顶下线的旧 token 会在这里续期「复活」
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.assertSameDevice(user.getMachine(), claimDeviceId,
@@ -238,8 +260,20 @@ public class AuthService {
return authProperties.getCookieName();
}
/** 登录推进世代;返回 0 行说明用户行已消失,不能再签发一个「世代比库还新」的令牌。 */
private void advanceSessionEpoch(LoginUserEntity user) {
int updated = loginUserMapper.incrementSessionEpoch(user.getId());
if (updated != 1) {
log.warn("[auth] 登录推进会话世代失败 userId={} updated={}", user.getId(), updated);
throw new BusinessException(500, "登录状态更新失败,请重试");
}
long current = user.getSessionEpoch() == null ? 0L : user.getSessionEpoch();
user.setSessionEpoch(current + 1);
}
private LoginResultVo buildResult(LoginUserEntity user, String deviceId, boolean isAdmin) {
String token = jwtService.issue(user.getId(), user.getUsername(), deviceId);
String token = jwtService.issue(user.getId(), user.getUsername(), deviceId,
user.getSessionEpoch() == null ? 0L : user.getSessionEpoch());
LoginResultVo vo = new LoginResultVo();
vo.setUserId(user.getId());
vo.setUsername(user.getUsername());