From a7682bffefa6eaeb918c919f7905f7507b9366fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sat, 24 Oct 2026 20:01:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=89=B4=E6=9D=83):=20=E5=8D=95=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=BC=80=E5=85=B3=E5=85=B3=E9=97=AD=E6=97=B6=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B8=85=E9=99=A4=E7=99=BB=E5=87=BA=E5=90=8A=E9=94=80?= =?UTF-8?q?=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 开关关掉后登录不再写 users.machine,而登出吊销借 machine 写 revoked@ 标记, 没人覆盖就永久留存——此后每次登录都先成功、再被 assertNotRevoked 判成 401「登录已过期,请重新登录」(2026-10-24 现网 admin 被锁死即此)。 登录成功即视为新会话,遇吊销标记时清空(仍不写设备号)。 --- .../modules/auth/service/AuthService.java | 8 ++++++ .../modules/auth/service/AuthServiceTest.java | 25 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) 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