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
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -57,6 +58,7 @@ class AuthServiceCompanyTest {
// 本组用例只关心公司身份,单设备互踢那条路径留给 AuthServiceTest
when(authProperties.isSingleDeviceEnabled()).thenReturn(false);
when(jwtService.ttlSeconds()).thenReturn(604800L);
when(loginUserMapper.incrementSessionEpoch(7L)).thenReturn(1);
service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
mock(PermissionMenuService.class), authProperties, companyService,
mock(AccountExpiryService.class));
@@ -85,7 +87,7 @@ class AuthServiceCompanyTest {
private void stubSuccessfulLogin(LoginUserEntity user) {
when(loginUserMapper.selectOne(any())).thenReturn(user);
when(passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
}
@Test
@@ -137,7 +139,7 @@ class AuthServiceCompanyTest {
when(claims.get("deviceId")).thenReturn("devA");
when(jwtService.parse("t")).thenReturn(claims);
when(loginUserMapper.selectById(7L)).thenReturn(user);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
when(companyService.nameOrEmpty(5L)).thenReturn("厦门米椿花");
LoginResultVo vo = service.checkLogin("t", null);
@@ -22,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
@@ -48,7 +49,7 @@ class AuthServiceTest {
Fixture f = fixture(true);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -59,7 +60,9 @@ class AuthServiceTest {
// 绑定写入当前设备(last-login-wins),签发的 token 也用当前设备
verify(f.loginUserMapper, times(1)).update(any(), any());
verify(f.jwtService).issue(7L, "u7", "devB");
verify(f.loginUserMapper).incrementSessionEpoch(7L);
// 登录推进世代(0 → 1),新令牌必须签上推进后的值
verify(f.jwtService).issue(7L, "u7", "devB", 1L);
assertThat(vo.getDeviceId()).isEqualTo("devB");
}
@@ -68,7 +71,7 @@ class AuthServiceTest {
Fixture f = fixture(false);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -87,7 +90,7 @@ class AuthServiceTest {
Fixture f = fixture(false);
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("revoked@7#20261024185618"));
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginRequest request = new LoginRequest();
request.setUsername("u7");
@@ -115,7 +118,7 @@ class AuthServiceTest {
assertThat(ex.getCode()).isEqualTo(4011);
assertThat(ex.getMessage()).contains("已在其他设备登录");
// 关键:被踢的旧 token 不能在这里换到新 token「复活」
verify(f.jwtService, never()).issue(any(), anyString(), anyString());
verify(f.jwtService, never()).issue(any(), anyString(), anyString(), anyLong());
}
@Test
@@ -124,11 +127,11 @@ class AuthServiceTest {
Claims claims = f.claims("devA");
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user("devA"));
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
LoginResultVo vo = f.service.checkLogin("t", null);
verify(f.jwtService).issue(7L, "u7", "devA");
verify(f.jwtService).issue(7L, "u7", "devA", 0L);
assertThat(vo.getDeviceId()).isEqualTo("devA");
}
@@ -155,7 +158,7 @@ class AuthServiceTest {
root.setRole("super_admin");
root.setIsAdmin(1);
when(f.loginUserMapper.selectById(7L)).thenReturn(root);
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
assertThatCode(() -> f.service.checkLogin("t", null)).doesNotThrowAnyException();
}
@@ -166,11 +169,59 @@ class AuthServiceTest {
Claims claims = f.claims("devB");
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user("devA"));
when(f.jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
assertThatCode(() -> f.service.checkLogin("t", null)).doesNotThrowAnyException();
}
// ---------- 会话世代:登出/新登录必须让旧 token 失效 ----------
@Test
void checkLoginRejectsTokenFromBeforeLogoutOrRelogin() {
Fixture f = fixture(false);
LoginUserEntity user = f.user("");
user.setSessionEpoch(2L);
Claims claims = f.claims("devA", 1L);
when(f.jwtService.parse("old-token")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(user);
BusinessException ex = catchThrowableOfType(
() -> f.service.checkLogin("old-token", null), BusinessException.class);
assertThat(ex.getCode()).isEqualTo(401);
assertThat(ex.getMessage()).contains("登录已过期");
verify(f.jwtService, never()).issue(any(), anyString(), anyString(), anyLong());
}
@Test
void logoutAdvancesSessionEpochEvenWhenMachineIsEmpty() {
// 单设备关闭时 machine 往往为空:旧实现按 machine 做 CAS,命中 0 行 = 登出没留下吊销。
Fixture f = fixture(false);
Claims claims = f.claims("devA", 0L);
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(f.user(""));
f.service.logoutByToken("t");
verify(f.loginUserMapper).incrementSessionEpoch(7L);
}
@Test
void staleTokenCannotLogOutANewerSession() {
// 旧令牌(世代 1)不能把已经登录到世代 2 的新会话一起登出
Fixture f = fixture(false);
LoginUserEntity user = f.user("");
user.setSessionEpoch(2L);
Claims claims = f.claims("devA", 1L);
when(f.jwtService.parse("t")).thenReturn(claims);
when(f.loginUserMapper.selectById(7L)).thenReturn(user);
f.service.logoutByToken("t");
verify(f.loginUserMapper, never()).incrementSessionEpoch(anyLong());
verify(f.loginUserMapper, never()).update(any(), any());
}
// ---------- 夹具 ----------
private Fixture fixture(boolean singleDeviceEnabled) {
@@ -181,6 +232,7 @@ class AuthServiceTest {
AuthProperties authProperties = mock(AuthProperties.class);
when(authProperties.isSingleDeviceEnabled()).thenReturn(singleDeviceEnabled);
when(jwtService.ttlSeconds()).thenReturn(604800L);
when(loginUserMapper.incrementSessionEpoch(7L)).thenReturn(1);
AuthService service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
permissionMenuService, authProperties, mock(CompanyService.class),
mock(AccountExpiryService.class));
@@ -202,9 +254,14 @@ class AuthServiceTest {
}
Claims claims(String deviceId) {
return claims(deviceId, 0L);
}
Claims claims(String deviceId, long sessionEpoch) {
Claims claims = mock(Claims.class);
when(claims.getSubject()).thenReturn("7");
when(claims.get("deviceId")).thenReturn(deviceId);
when(claims.get("sessionEpoch")).thenReturn(sessionEpoch);
return claims;
}
}