refactor(boundary): 跨模块循环依赖清零(8 对 → 0),task→业务 依赖归零
共享内核下沉(跨模块共享的"身份/组织/安全"类型进 common) - AdminUserEntity/AdminUserMapper、ShopManageGroupEntity/ShopManageGroupMapper → common - AdminAuthSupport(33 文件 17 模块引用)、JwtService、AuthProperties、DeviceSessionPolicy → common/security(原先放在 admin/auth 里,任何模块用一次就多一条跨模块边) 端口化(消费方声明接口、数据方实现) - task/spi:ResultFileJobHandler 新增 resolveResultFileUrl 钩子(BRAND 特例从 Worker 收回); Worker 改为调用 handler.onSuccess(该钩子历史上从未被调用,withdraw 的收尾靠 Worker 里的 WITHDRAW 特判硬编码——现两者都归位,task 侧不再 import withdraw/brand) - admin/spi/UserSecretCleanupPort(删除用户级联清理密钥)、notification/spi/ProxyBalancePort (代理余额探测)、shopdatacrawl/spi/ManagedShopNamesPort(可管店铺名) - shopkey/spi/GroupDeletionGuardPort:分组删除守卫改由各业务模块实现(invalidasin/dedupe 两个实现), shopkey 不再直连它们的 Mapper 棘轮与量化(2026-09-14 实测) - ArchitectureBoundaryTest:task→业务 119 → **0**(基线钉死为 0,新增跨模块动作必须走 task/spi) - 跨模块 import 行数 650 → 605;双向依赖对 8 → **0**(usersecret/permission/shopkey 三向环、 task↔withdraw、task↔brand、admin↔permission、admin↔usersecret、dedup↔shopkey、 invalidasin↔shopkey、notification↔usersecret、shopdatacrawl↔shopduplicatecheck 全部拆解) 测试同步:接口契约 8→9、品牌 Handler 钩子契约、worker 的 10 个测试构造实参、通知/管理测试端口化。 mvn test 2881 全绿。
This commit is contained in:
+128
@@ -0,0 +1,128 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.security.AuthProperties;
|
||||
import com.nanri.aiimage.common.security.JwtService;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
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.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AdminAuthSupportTest {
|
||||
|
||||
@Test
|
||||
void explicitLowestIdAdminIsNotPromotedToSuperAdmin() {
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
AdminAuthSupport support = new AdminAuthSupport(
|
||||
mock(JwtService.class), userMapper, mock(AuthProperties.class));
|
||||
AdminUserEntity user = user(1L, "admin", 1, null);
|
||||
|
||||
assertThat(support.currentRole(user)).isEqualTo("admin");
|
||||
verify(userMapper, never()).selectOne(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyBlankRoleRootRemainsSuperAdmin() {
|
||||
AdminAuthSupport support = new AdminAuthSupport(
|
||||
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class));
|
||||
|
||||
assertThat(support.currentRole(user(1L, null, 1, null))).isEqualTo("super_admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitNormalRoleIsNotPromotedByLegacyAdminFields() {
|
||||
AdminAuthSupport support = new AdminAuthSupport(
|
||||
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class));
|
||||
|
||||
assertThat(support.currentRole(user(1L, "normal", 1, null))).isNull();
|
||||
}
|
||||
|
||||
// ---------- 单设备登录:requireUser 统一拦截 ----------
|
||||
|
||||
@Test
|
||||
void requireUserRejectsTokenFromSupersededDevice() {
|
||||
// 账号已绑定 devA,token 内签名的设备是 devB(被新设备顶下线)→ 4011
|
||||
AdminAuthSupport support = support(user(7L, "normal", 0, null, "devA"), "devB", true);
|
||||
|
||||
BusinessException ex = catchThrowableOfType(
|
||||
() -> support.requireUser(requestWithBearer("t")), BusinessException.class);
|
||||
|
||||
assertThat(ex.getCode()).isEqualTo(4011);
|
||||
assertThat(ex.getMessage()).contains("已在其他设备登录");
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireUserPassesWhenDeviceMatches() {
|
||||
AdminAuthSupport support = support(user(7L, "normal", 0, null, "devA"), "devA", true);
|
||||
|
||||
assertThatCode(() -> support.requireUser(requestWithBearer("t"))).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireUserExemptsSuperAdmin() {
|
||||
AdminAuthSupport support = support(user(7L, "super_admin", 1, null, "devA"), "devB", true);
|
||||
|
||||
assertThatCode(() -> support.requireUser(requestWithBearer("t"))).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireUserPassesWhenSingleDeviceDisabled() {
|
||||
AdminAuthSupport support = support(user(7L, "normal", 0, null, "devA"), "devB", false);
|
||||
|
||||
assertThatCode(() -> support.requireUser(requestWithBearer("t"))).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void requireUserPassesWhenUnbound() {
|
||||
AdminAuthSupport support = support(user(7L, "normal", 0, null, null), "devB", true);
|
||||
|
||||
assertThatCode(() -> support.requireUser(requestWithBearer("t"))).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
private AdminAuthSupport support(AdminUserEntity user, String claimDeviceId, boolean singleDeviceEnabled) {
|
||||
JwtService jwtService = mock(JwtService.class);
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
AuthProperties props = mock(AuthProperties.class);
|
||||
|
||||
Claims claims = mock(Claims.class);
|
||||
when(claims.getSubject()).thenReturn(String.valueOf(user.getId()));
|
||||
when(claims.get("deviceId")).thenReturn(claimDeviceId);
|
||||
when(jwtService.parse("t")).thenReturn(claims);
|
||||
when(userMapper.selectById(user.getId())).thenReturn(user);
|
||||
when(props.isSingleDeviceEnabled()).thenReturn(singleDeviceEnabled);
|
||||
|
||||
return new AdminAuthSupport(jwtService, userMapper, props);
|
||||
}
|
||||
|
||||
private HttpServletRequest requestWithBearer(String token) {
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn("Bearer " + token);
|
||||
return request;
|
||||
}
|
||||
|
||||
private AdminUserEntity user(Long id, String role, int isAdmin, Long createdById) {
|
||||
return user(id, role, isAdmin, createdById, null);
|
||||
}
|
||||
|
||||
private AdminUserEntity user(Long id, String role, int isAdmin, Long createdById, String machine) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
user.setUsername("u" + id);
|
||||
user.setRole(role);
|
||||
user.setIsAdmin(isAdmin);
|
||||
user.setCreatedById(createdById);
|
||||
user.setMachine(machine);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import io.jsonwebtoken.Claims;
|
||||
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;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class DeviceSessionPolicyTest {
|
||||
|
||||
// ---------- 角色规则(从 AdminAuthSupport.currentRole 迁移,行为必须保持一致) ----------
|
||||
|
||||
@Test
|
||||
void explicitSuperAdminResolved() {
|
||||
assertThat(DeviceSessionPolicy.resolveRole("super_admin", 1, 9L)).isEqualTo("super_admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitAdminNotPromotedToSuperAdmin() {
|
||||
assertThat(DeviceSessionPolicy.resolveRole("admin", 1, null)).isEqualTo("admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyBlankRoleRootRemainsSuperAdmin() {
|
||||
assertThat(DeviceSessionPolicy.resolveRole(null, 1, null)).isEqualTo("super_admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyBlankRoleCreatedByOtherIsAdmin() {
|
||||
assertThat(DeviceSessionPolicy.resolveRole("", 1, 3L)).isEqualTo("admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalRoleIsNotPromotedByLegacyAdminFields() {
|
||||
assertThat(DeviceSessionPolicy.resolveRole("normal", 1, null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isSuperAdminOnlyForSuperAdmin() {
|
||||
assertThat(DeviceSessionPolicy.isSuperAdmin("super_admin", 1, null)).isTrue();
|
||||
assertThat(DeviceSessionPolicy.isSuperAdmin("admin", 1, null)).isFalse();
|
||||
assertThat(DeviceSessionPolicy.isSuperAdmin("normal", 0, null)).isFalse();
|
||||
}
|
||||
|
||||
// ---------- 设备一致性校验 ----------
|
||||
|
||||
@Test
|
||||
void exemptUserAlwaysPasses() {
|
||||
// 超管豁免:设备不一致也放行
|
||||
assertThatCode(() -> DeviceSessionPolicy.assertSameDevice("devA", "devB", true, 7L, "root"))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void unboundMachinePasses() {
|
||||
// 尚未绑定(machine 为空)放行,下次登录写入后开始生效
|
||||
assertThatCode(() -> DeviceSessionPolicy.assertSameDevice(null, "devB", false, 7L, "u1"))
|
||||
.doesNotThrowAnyException();
|
||||
assertThatCode(() -> DeviceSessionPolicy.assertSameDevice(" ", "devB", false, 7L, "u1"))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameDevicePasses() {
|
||||
assertThatCode(() -> DeviceSessionPolicy.assertSameDevice("devA", "devA", false, 7L, "u1"))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void differentDeviceThrowsKicked() {
|
||||
BusinessException ex = catchThrowableOfType(
|
||||
() -> DeviceSessionPolicy.assertSameDevice("devA", "devB", false, 7L, "u1"),
|
||||
BusinessException.class);
|
||||
|
||||
assertThat(ex.getCode()).isEqualTo(DeviceSessionPolicy.CODE_KICKED);
|
||||
assertThat(ex.getMessage()).contains("已在其他设备登录");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankClaimDeviceThrowsUnauthorized() {
|
||||
// 正常 token 必带 deviceId claim;缺失按登录态无效处理(普通 401,不误报"被踢")
|
||||
BusinessException ex = catchThrowableOfType(
|
||||
() -> DeviceSessionPolicy.assertSameDevice("devA", null, false, 7L, "u1"),
|
||||
BusinessException.class);
|
||||
|
||||
assertThat(ex.getCode()).isEqualTo(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
void claimDeviceIdExtractsAndTrims() {
|
||||
assertThat(DeviceSessionPolicy.claimDeviceId(null)).isEmpty();
|
||||
|
||||
Claims claims = mock(Claims.class);
|
||||
when(claims.get("deviceId")).thenReturn(" devA ");
|
||||
assertThat(DeviceSessionPolicy.claimDeviceId(claims)).isEqualTo("devA");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user