diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/auth/service/AuthService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/auth/service/AuthService.java index 76191d0a1..62b92754e 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/auth/service/AuthService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/auth/service/AuthService.java @@ -122,6 +122,14 @@ public class AuthService { loginUserMapper.update(null, new LambdaUpdateWrapper() .eq(LoginUserEntity::getId, user.getId()) .set(LoginUserEntity::getMachine, deviceId)); + } else if (DeviceSessionPolicy.isRevoked(user.getMachine())) { + // 开关关掉后登录不再写 machine,登出留下的吊销标记就没人清:此后每次登录都会先成功、 + // 再被 requireUser 的 assertNotRevoked 判成「登录已过期」,账号永久锁死。 + // 登录成功即视为新会话,清空标记(空=未绑定,放行语义);仍不写设备号。 + log.warn("[auth] 登录清除登出吊销标记 userId={} username={}", user.getId(), user.getUsername()); + loginUserMapper.update(null, new LambdaUpdateWrapper() + .eq(LoginUserEntity::getId, user.getId()) + .set(LoginUserEntity::getMachine, "")); } log.info("[auth] login userId={} isAdmin={} device={}", user.getId(), isAdmin, deviceId); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/auth/service/AuthServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/auth/service/AuthServiceTest.java index 4de93d7db..103c5c607 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/auth/service/AuthServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/auth/service/AuthServiceTest.java @@ -1,6 +1,7 @@ package com.nanri.aiimage.modules.auth.service; import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.nanri.aiimage.common.exception.BusinessException; import com.nanri.aiimage.common.security.AccountExpiryService; @@ -22,6 +23,7 @@ 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.anyString; +import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -62,7 +64,7 @@ class AuthServiceTest { } @Test - void loginSkipsBindingWhenSingleDeviceDisabled() { + void loginSkipsBindingWhenSingleDeviceDisabledAndNotRevoked() { Fixture f = fixture(false); when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA")); when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true); @@ -78,6 +80,27 @@ class AuthServiceTest { verify(f.loginUserMapper, never()).update(any(), any()); } + @Test + void loginClearsLogoutRevocationMarkerWhenSingleDeviceDisabled() { + // 开关关掉后登录不再写 machine,登出标记若不清掉,新 token 会被 assertNotRevoked + // 判成「登录已过期」——表现是登录成功后又弹登录页(2026-10-24 现网 admin 锁死)。 + 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"); + + LoginRequest request = new LoginRequest(); + request.setUsername("u7"); + request.setPassword("pwd"); + request.setDeviceId("devB"); + + assertThatCode(() -> f.service.login(request)).doesNotThrowAnyException(); + + verify(f.loginUserMapper, times(1)).update(any(), + argThat(wrapper -> wrapper instanceof LambdaUpdateWrapper + && ((LambdaUpdateWrapper) wrapper).getSqlSet().contains("machine"))); + } + // ---------- check_login:被顶下线必须拦截且不续期 ---------- @Test