fix(登出): logout 此前只清 cookie——现写 machine 吊销标记且先于超管豁免
吊销必须挂在 assertSameDevice 之前:那条路径第一行就是 if (exempt) return;, 放它下面等于超管永不见吊销。也不能清空 machine(本类对空值是「尚未绑定=放行」语义)。 前缀避开 claude-*/e2e-*:users.machine 还被当测试号签名用。写值带 deviceId 做 CAS, 不把别处刚登录的新会话一起打死。
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
|
||||
/**
|
||||
* 登出必须真能吊销。今天 {@code POST /logout} 只清 cookie(LoginController),服务端不留任何痕迹,
|
||||
* 已发令牌在 7 天内继续有效。吊销借 {@code users.machine} 写一个不可匹配的标记——
|
||||
* 而「清空 machine」在这个类里是【放行】语义(尚未绑定不做限制),所以不能用空串表达登出。
|
||||
*/
|
||||
class DeviceSessionRevocationTest {
|
||||
|
||||
@Test
|
||||
void revokedMarkerIsRecognised() {
|
||||
assertThat(DeviceSessionPolicy.isRevoked("revoked@12#20261009120000")).isTrue();
|
||||
assertThat(DeviceSessionPolicy.isRevoked(" revoked@12#20261009120000 ")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokedMarkerRejectsWithTheSameMessageClientsAlreadyHandle() {
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> DeviceSessionPolicy.assertNotRevoked("revoked@12#20261009120000", 12L, "超管"),
|
||||
BusinessException.class);
|
||||
assertThat(e).isNotNull();
|
||||
assertThat(e.getCode()).isEqualTo(401);
|
||||
// 客户端只按 401/409 分支;换新文案会让它走未知路径
|
||||
assertThat(e.getMessage()).isEqualTo("登录已过期,请重新登录");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankMachineIsNotRevocation() {
|
||||
assertThat(DeviceSessionPolicy.isRevoked("")).isFalse();
|
||||
assertThat(DeviceSessionPolicy.isRevoked(null)).isFalse();
|
||||
assertThatCode(() -> DeviceSessionPolicy.assertNotRevoked("", 1L, "u")).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAccountSignaturesAreNotRevokedMarkers() {
|
||||
// users.machine 同时被当测试号签名使用(claude-*/e2e-*,10-06 清脏数据靠它认账号):
|
||||
// 吊销标记的前缀必须与这两族写法不重叠,否则清账脚本会把已登出设备读成测试号(或反之)。
|
||||
assertThat(DeviceSessionPolicy.isRevoked("claude-abc")).isFalse();
|
||||
assertThat(DeviceSessionPolicy.isRevoked("e2e-device-1")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void realDeviceIdIsNotRevoked() {
|
||||
assertThat(DeviceSessionPolicy.isRevoked(
|
||||
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user