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
@@ -126,16 +126,13 @@ public final class ExcelStreamReader {
SheetCollector collector = new SheetCollector(sheetName, sheetNo, handler, recorder);
XSSFSheetXMLHandler sheetHandler =
new XSSFSheetXMLHandler(styles, sharedStrings, collector, formatter, false);
DimensionFilter filter = new DimensionFilter(parser, maxRows -> {
try {
handler.onSheetTotal(sheetName, sheetNo, maxRows);
} catch (Exception ex) {
throw recorder.record(ex);
}
});
// dimension 在 sheetData 之前出现,先缓存;等 onHeader 回调之后再对外发 onSheetTotal,
// 保持旧 EasyExcel「先表头、后总行数」的顺序(CR-10)。
DimensionFilter filter = new DimensionFilter(parser, collector::setApproximateTotalRows);
filter.setContentHandler(sheetHandler);
parser.setContentHandler(filter);
parser.parse(new InputSource(sheet));
collector.reportTotalIfNeeded();
}
private static void readHssf(InputStream inputStream, boolean readAllSheets, SheetRowHandler handler)
@@ -152,11 +149,12 @@ public final class ExcelStreamReader {
final String sheetName = sheet.getSheetName();
final int currentSheetNo = sheetNo;
final int lastRow = sheet.getLastRowNum();
callRecorder(recorder, () -> handler.onSheetTotal(sheetName, currentSheetNo, lastRow + 1));
final Map<Integer, String> header = rowToMap(sheet.getRow(0), formatter);
if (!header.isEmpty()) {
callRecorder(recorder, () -> handler.onHeader(sheetName, currentSheetNo, header));
}
// 旧顺序是「表头 → 总行数」;xls 路径此前反了,统一回来(CR-10)
callRecorder(recorder, () -> handler.onSheetTotal(sheetName, currentSheetNo, lastRow + 1));
for (int rowIndex = 1; rowIndex <= lastRow; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
@@ -274,6 +272,9 @@ public final class ExcelStreamReader {
private final FailureRecorder recorder;
private Map<Integer, String> header = Map.of();
private Map<Integer, String> row = new LinkedHashMap<>();
private Integer approximateTotalRows;
private boolean headerReported;
private boolean totalReported;
private SheetCollector(String sheetName, int sheetNo, SheetRowHandler handler, FailureRecorder recorder) {
this.sheetName = sheetName;
@@ -282,6 +283,21 @@ public final class ExcelStreamReader {
this.recorder = recorder;
}
/** dimension 过滤器的回调:只缓存,等 onHeader 之后才对外发。 */
private void setApproximateTotalRows(Integer rows) {
this.approximateTotalRows = rows;
reportTotalIfNeeded();
}
private void reportTotalIfNeeded() {
if (!headerReported || totalReported) {
return;
}
totalReported = true;
callRecorder(recorder,
() -> handler.onSheetTotal(sheetName, sheetNo, approximateTotalRows));
}
@Override
public void startRow(int rowNum) {
row = new LinkedHashMap<>();
@@ -294,7 +310,9 @@ public final class ExcelStreamReader {
}
if (rowNum == 0) {
header = new LinkedHashMap<>(row);
headerReported = true;
callRecorder(recorder, () -> handler.onHeader(sheetName, sheetNo, header));
reportTotalIfNeeded();
} else {
Map<Integer, String> values = new LinkedHashMap<>(row);
int rowIndex = rowNum;
@@ -49,4 +49,10 @@ public class AdminUserEntity {
@TableField(value = "updated_at", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
private LocalDateTime updatedAt;
private String machine;
/**
* 会话世代(V172 新增):登录/登出各 +1,JWT 的 epoch claim 必须与它相等。
* 与 {@code LoginUserEntity.sessionEpoch} 同列,改一处必须同改两处。
*/
@TableField("session_epoch")
private Long sessionEpoch;
}
@@ -72,6 +72,9 @@ public class AdminAuthSupport {
// 吊销先于单设备比对:那条路径第一行就是 if (exempt) return;(超管豁免),
// 把吊销挂在它下面等于超管永不见吊销
DeviceSessionPolicy.assertNotRevoked(user.getMachine(), user.getId(), user.getUsername());
// 世代校验同样先于单设备比对,且不受 singleDevice 开关支配:登出/新登录后旧令牌一律失效
DeviceSessionPolicy.assertSessionEpoch(user.getSessionEpoch(),
DeviceSessionPolicy.claimSessionEpoch(claims), user.getId(), user.getUsername());
// 单设备登录:被新设备顶下线的旧 token 在此统一拦截(全站 requireUser 调用点自动生效)
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.assertSameDevice(user.getMachine(), DeviceSessionPolicy.claimDeviceId(claims),
@@ -91,6 +91,30 @@ public final class DeviceSessionPolicy {
return raw == null ? "" : raw.toString().trim();
}
/**
* 从 JWT claims 中提取会话世代;缺失(本次升级前签发的旧令牌)按 0 读取,
* 与 {@code users.session_epoch} 的列默认值一致,升级不会统一踢掉存量会话。
*/
public static long claimSessionEpoch(Claims claims) {
Object raw = claims == null ? null : claims.get("sessionEpoch");
// JwtService 签发的是 Number;非 Number 一律视同缺失(按 0),不在这里做字符串兼容,
// 避免为一种不会出现的 claim 形状新增 catch(CatchSilenceRatchet 满格)。
return raw instanceof Number number ? number.longValue() : 0L;
}
/**
* 会话世代必须与库中值相等。登出与每次新登录都会 +1,因此登出前签发的令牌在
* 下一次请求即失效;不依赖 {@code machine} 上的 revoked@ 标记,也不受单设备开关支配。
*/
public static void assertSessionEpoch(Long storedEpoch, long claimEpoch, Long userId, String username) {
long current = storedEpoch == null ? 0L : storedEpoch;
if (current != claimEpoch) {
log.warn("[auth] 会话世代失效被拒 userId={} username={} stored={} claim={}",
userId, username, current, claimEpoch);
throw new BusinessException(401, "登录已过期,请重新登录");
}
}
/**
* 校验请求携带的 token 是否仍属于账号当前绑定的设备。
* 超管豁免;machine 为空(尚未绑定)放行;不匹配抛 4011。
@@ -85,13 +85,19 @@ public class JwtService {
return key;
}
public String issue(Long userId, String username, String deviceId) {
/**
* 签发令牌。{@code sessionEpoch} 必须取自 users.session_epoch(登录/登出各推进一次),
* 鉴权时与库中值比对;旧令牌没有这个 claim 时按 0 读取,见
* {@link DeviceSessionPolicy#claimSessionEpoch(Claims)}。
*/
public String issue(Long userId, String username, String deviceId, long sessionEpoch) {
Instant now = Instant.now();
Instant exp = now.plus(Duration.ofHours(props.getJwtTtlHours()));
return Jwts.builder()
.subject(String.valueOf(userId))
.claim("username", username)
.claim("deviceId", deviceId)
.claim("sessionEpoch", sessionEpoch)
.issuedAt(Date.from(now))
.expiration(Date.from(exp))
.signWith(signingKey())
@@ -74,6 +74,22 @@ public final class NodeSharedStore<K, V> {
return remote;
}
/**
* 远端优先;远端没有才回退本地。用于「锁在实例间轮换、新持锁方必须读到上一持锁方
* 写入的基线」这类状态——{@link #get} 的本地优先会让每个实例 forever 读自己的旧值。
*/
public V getFresh(K key) {
if (key == null) {
return null;
}
V remote = readRemote(key);
if (remote != null) {
local.put(key, remote);
return remote;
}
return local.get(key);
}
public boolean containsKey(K key) {
return get(key) != null;
}