fix(鉴权): 单设备开关关闭时登录清除登出吊销标记
开关关掉后登录不再写 users.machine,而登出吊销借 machine 写 revoked@ 标记, 没人覆盖就永久留存——此后每次登录都先成功、再被 assertNotRevoked 判成 401「登录已过期,请重新登录」(2026-10-24 现网 admin 被锁死即此)。 登录成功即视为新会话,遇吊销标记时清空(仍不写设备号)。
This commit is contained in:
@@ -122,6 +122,14 @@ public class AuthService {
|
|||||||
loginUserMapper.update(null, new LambdaUpdateWrapper<LoginUserEntity>()
|
loginUserMapper.update(null, new LambdaUpdateWrapper<LoginUserEntity>()
|
||||||
.eq(LoginUserEntity::getId, user.getId())
|
.eq(LoginUserEntity::getId, user.getId())
|
||||||
.set(LoginUserEntity::getMachine, deviceId));
|
.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<LoginUserEntity>()
|
||||||
|
.eq(LoginUserEntity::getId, user.getId())
|
||||||
|
.set(LoginUserEntity::getMachine, ""));
|
||||||
}
|
}
|
||||||
log.info("[auth] login userId={} isAdmin={} device={}", user.getId(), isAdmin, deviceId);
|
log.info("[auth] login userId={} isAdmin={} device={}", user.getId(), isAdmin, deviceId);
|
||||||
|
|
||||||
|
|||||||
+24
-1
@@ -1,6 +1,7 @@
|
|||||||
package com.nanri.aiimage.modules.auth.service;
|
package com.nanri.aiimage.modules.auth.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
import com.nanri.aiimage.common.exception.BusinessException;
|
import com.nanri.aiimage.common.exception.BusinessException;
|
||||||
import com.nanri.aiimage.common.security.AccountExpiryService;
|
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.assertj.core.api.Assertions.catchThrowableOfType;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.argThat;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
@@ -62,7 +64,7 @@ class AuthServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void loginSkipsBindingWhenSingleDeviceDisabled() {
|
void loginSkipsBindingWhenSingleDeviceDisabledAndNotRevoked() {
|
||||||
Fixture f = fixture(false);
|
Fixture f = fixture(false);
|
||||||
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
|
when(f.loginUserMapper.selectOne(any())).thenReturn(f.user("devA"));
|
||||||
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
|
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
|
||||||
@@ -78,6 +80,27 @@ class AuthServiceTest {
|
|||||||
verify(f.loginUserMapper, never()).update(any(), any());
|
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:被顶下线必须拦截且不续期 ----------
|
// ---------- check_login:被顶下线必须拦截且不续期 ----------
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user