From 06052c41fca86d9e0d58569a106a1ba2d0399926 Mon Sep 17 00:00:00 2001 From: daxpay Date: Fri, 9 May 2025 15:27:52 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BC=98=E5=8C=96=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=9D=83=E9=99=90=E7=94=9F=E6=88=90=E8=A7=84=E5=88=99,=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=92=8C=E5=88=86=E7=BB=84=E4=B8=8D=E4=BC=9A?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E8=BF=9B=E8=A1=8C=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../permission/PermPathSyncService.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermPathSyncService.java b/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermPathSyncService.java index a7741b90..c96d552b 100644 --- a/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermPathSyncService.java +++ b/bootx-platform/bootx-platform-service/service-iam/src/main/java/cn/bootx/platform/iam/service/permission/PermPathSyncService.java @@ -12,6 +12,7 @@ import cn.bootx.platform.iam.entity.permission.PermPath; import cn.bootx.platform.iam.service.client.ClientCodeService; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.SecureUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.annotation.CacheEvict; @@ -23,6 +24,7 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -106,15 +108,18 @@ public class PermPathSyncService { // 重建树结构 删除指定终端的非子节点 permPathManager.deleteNotChild(clientCode); // 生成模块信息 - var moduleMap = this.builderModule(requestPathBos); + var moduleList = this.builderModule(requestPathBos); // 生成分组信息 - var groupMap = this.builderGroup(requestPathBos); + var groupList = this.builderGroup(requestPathBos); // 合并进行保存 ArrayList list = new ArrayList<>(); - list.addAll(moduleMap); - list.addAll(groupMap); + list.addAll(moduleList); + list.addAll(groupList); // 设置终端编码 list.forEach(o -> o.setClientCode(clientCode)); + // 根据编码生成ID, 保证每次同步时的模块和分组ID不变 + list.forEach(o -> o.setId(this.genPathId(o.getParentCode()+o.getClientCode()+o.getCode()))); + // 保存 permPathManager.saveAll(list); } @@ -222,7 +227,7 @@ public class PermPathSyncService { .stream() .filter(pathKey -> { HandlerMethod handlerMethod = map.get(pathKey); - return Objects.nonNull(handlerMethod.getMethodAnnotation(RequestPath.class)) + return Objects.nonNull(handlerMethod.getMethodAnnotation(cn.bootx.platform.core.annotation.RequestPath.class)) &&Objects.nonNull(handlerMethod.getBeanType().getAnnotation(RequestGroup.class)); }).toList(); @@ -297,4 +302,19 @@ public class PermPathSyncService { .collect(toList()); } + /** + * 给分组和模块生成ID, 防止每次更新ID都会发生变化 + */ + private long genPathId(String str) { + String s = SecureUtil.sha256(str); + byte[] hashBytes = s.getBytes(StandardCharsets.UTF_8); + // 将前8个字节转换为 long + long result = 0; + for (int i = 0; i < 8; i++) { + result = (result << 8) | (hashBytes[i] & 0xFF); + } + // 取绝对值,避免负数 + return Math.abs(result); + } + }