From 34913542c0773455f7e50de861b5a854db9037ee Mon Sep 17 00:00:00 2001
From: bunny <1319900154@qq.com>
Date: Sun, 1 Jun 2025 19:07:44 +0800
Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=88=B6=E8=8F=9C=E5=8D=95?=
=?UTF-8?q?=E8=A7=92=E8=89=B2=E7=BB=A7=E6=89=BF=E5=AD=90=E8=8F=9C=E5=8D=95?=
=?UTF-8?q?=E8=A7=92=E8=89=B2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../services/aop/aspect/JobExecuteAspect.java | 2 +-
.../listener/user/UserinfoUpdateListener.java | 8 +-
.../core/utils/RouterServiceHelper.java | 89 +++++++++++++++++++
.../SecurityAuthenticationFailureHandler.java | 26 ------
.../SecurityAuthenticationSuccessHandler.java | 26 ------
.../UserLoginVoBuilderCacheService.java | 6 +-
.../impl/RolePermissionServiceImpl.java | 5 +-
.../system/impl/RouterServiceImpl.java | 5 +-
.../system/impl/UserLoginServiceImpl.java | 22 ++---
.../java/impl/BuildPermissionApiTest.java | 79 ----------------
.../test/java/impl/ControllerScannerTest.java | 29 ------
11 files changed, 113 insertions(+), 184 deletions(-)
delete mode 100644 auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationFailureHandler.java
delete mode 100644 auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationSuccessHandler.java
rename auth-system/src/main/java/cn/bunny/services/{core/cache => security/service}/UserLoginVoBuilderCacheService.java (98%)
delete mode 100644 auth-system/src/test/java/impl/BuildPermissionApiTest.java
delete mode 100644 auth-system/src/test/java/impl/ControllerScannerTest.java
diff --git a/auth-system/src/main/java/cn/bunny/services/aop/aspect/JobExecuteAspect.java b/auth-system/src/main/java/cn/bunny/services/aop/aspect/JobExecuteAspect.java
index 1e3958d..c622f52 100644
--- a/auth-system/src/main/java/cn/bunny/services/aop/aspect/JobExecuteAspect.java
+++ b/auth-system/src/main/java/cn/bunny/services/aop/aspect/JobExecuteAspect.java
@@ -101,7 +101,7 @@ public class JobExecuteAspect {
scheduleExecuteLogMapper.insert(executeLog);
}
- @Pointcut("execution(* cn.bunny.core.quartz.*.execute(..))")
+ @Pointcut("execution(* cn.bunny.services.quartz.*.execute(..))")
public void pointCut() {
}
}
diff --git a/auth-system/src/main/java/cn/bunny/services/core/event/listener/user/UserinfoUpdateListener.java b/auth-system/src/main/java/cn/bunny/services/core/event/listener/user/UserinfoUpdateListener.java
index 8647e73..3f071e2 100644
--- a/auth-system/src/main/java/cn/bunny/services/core/event/listener/user/UserinfoUpdateListener.java
+++ b/auth-system/src/main/java/cn/bunny/services/core/event/listener/user/UserinfoUpdateListener.java
@@ -1,15 +1,15 @@
package cn.bunny.services.core.event.listener.user;
-import cn.bunny.services.core.cache.UserLoginVoBuilderCacheService;
+import cn.bunny.domain.common.constant.RedisUserConstant;
+import cn.bunny.domain.model.system.entity.RolePermission;
+import cn.bunny.domain.model.system.entity.UserRole;
import cn.bunny.services.core.event.event.ClearAllUserCacheEvent;
import cn.bunny.services.core.event.event.UpdateUserinfoByPermissionIdsEvent;
import cn.bunny.services.core.event.event.UpdateUserinfoByRoleIdsEvent;
import cn.bunny.services.core.event.event.UpdateUserinfoByUserIdsEvent;
-import cn.bunny.domain.common.constant.RedisUserConstant;
-import cn.bunny.domain.model.system.entity.RolePermission;
-import cn.bunny.domain.model.system.entity.UserRole;
import cn.bunny.services.mapper.system.RolePermissionMapper;
import cn.bunny.services.mapper.system.UserRoleMapper;
+import cn.bunny.services.security.service.UserLoginVoBuilderCacheService;
import jakarta.annotation.Resource;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
diff --git a/auth-system/src/main/java/cn/bunny/services/core/utils/RouterServiceHelper.java b/auth-system/src/main/java/cn/bunny/services/core/utils/RouterServiceHelper.java
index 3f86f53..0b82270 100644
--- a/auth-system/src/main/java/cn/bunny/services/core/utils/RouterServiceHelper.java
+++ b/auth-system/src/main/java/cn/bunny/services/core/utils/RouterServiceHelper.java
@@ -162,4 +162,93 @@ public class RouterServiceHelper {
.sorted(Comparator.comparing(routerVo -> routerVo.getMeta().getRank()))
.toList();
}
+
+ /**
+ * 首先构建一个从节点ID到节点对象的映射,方便快速查找父节点。
+ *
+ * 然后遍历所有节点,确保每个父节点包含其直接子节点的所有角色。
+ *
+ * 最后进行多次从下往上的传播,确保角色信息从叶子节点一直传播到根节点。这个过程会重复直到没有新的角色需要添加为止。
+ */
+ public void propagateRolesToParents(List webUserRouterVoList) {
+ if (webUserRouterVoList == null || webUserRouterVoList.isEmpty()) return;
+
+ // 首先构建一个id到节点的映射,方便查找父节点
+ Map idToNodeMap = new HashMap<>();
+ buildIdMap(webUserRouterVoList, idToNodeMap);
+
+ // 遍历所有节点,将子节点的角色传播到父节点
+ for (WebUserRouterVo node : idToNodeMap.values()) {
+ if (node.getChildren() != null && !node.getChildren().isEmpty()) {
+ // 获取当前节点的所有子节点
+ List children = node.getChildren();
+
+ // 收集所有子节点的角色
+ RouterMeta parentMeta = node.getMeta();
+ if (parentMeta == null) {
+ parentMeta = new RouterMeta();
+ node.setMeta(parentMeta);
+ }
+
+ Set allRoles = new HashSet<>();
+ if (parentMeta.getRoles() != null) {
+ allRoles.addAll(parentMeta.getRoles());
+ }
+
+ for (WebUserRouterVo child : children) {
+ if (child.getMeta() != null && child.getMeta().getRoles() != null) {
+ allRoles.addAll(child.getMeta().getRoles());
+ }
+ }
+
+ // 更新父节点的角色
+ if (!allRoles.isEmpty()) {
+ parentMeta.setRoles(new ArrayList<>(allRoles));
+ }
+ }
+ }
+
+ // 需要从下往上传播角色,所以需要多次遍历直到没有变化
+ boolean changed;
+ do {
+ changed = false;
+ for (WebUserRouterVo node : idToNodeMap.values()) {
+ if (node.getParentId() != null && node.getParentId() != 0) {
+ WebUserRouterVo parent = idToNodeMap.get(node.getParentId());
+ if (parent != null) {
+ RouterMeta parentMeta = parent.getMeta();
+ RouterMeta childMeta = node.getMeta();
+
+ if (childMeta != null && childMeta.getRoles() != null) {
+ if (parentMeta == null) {
+ parentMeta = new RouterMeta();
+ parent.setMeta(parentMeta);
+ }
+
+ Set parentRoles = parentMeta.getRoles() != null
+ ? new HashSet<>(parentMeta.getRoles())
+ : new HashSet<>();
+
+ int originalSize = parentRoles.size();
+ parentRoles.addAll(childMeta.getRoles());
+
+ if (parentRoles.size() > originalSize) {
+ parentMeta.setRoles(new ArrayList<>(parentRoles));
+ changed = true;
+ }
+ }
+ }
+ }
+ }
+ } while (changed);
+ }
+
+ private void buildIdMap(List nodes, Map idToNodeMap) {
+ for (WebUserRouterVo node : nodes) {
+ idToNodeMap.put(node.getId(), node);
+ if (node.getChildren() != null && !node.getChildren().isEmpty()) {
+ buildIdMap(node.getChildren(), idToNodeMap);
+ }
+ }
+ }
}
diff --git a/auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationFailureHandler.java b/auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationFailureHandler.java
deleted file mode 100644
index c36d811..0000000
--- a/auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationFailureHandler.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package cn.bunny.services.security.handelr;
-
-import cn.bunny.domain.common.model.vo.result.Result;
-import com.alibaba.fastjson2.JSON;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-import org.springframework.security.core.AuthenticationException;
-import org.springframework.security.web.authentication.AuthenticationFailureHandler;
-
-import java.io.IOException;
-
-public class SecurityAuthenticationFailureHandler implements AuthenticationFailureHandler {
- @Override
- public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
- // 错误消息
- String localizedMessage = exception.getLocalizedMessage();
- Result result = Result.error(localizedMessage);
-
- // 转成JSON
- Object json = JSON.toJSON(result);
-
- // 返回响应
- response.setContentType("application/json;charset=UTF-8");
- response.getWriter().println(json);
- }
-}
diff --git a/auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationSuccessHandler.java b/auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationSuccessHandler.java
deleted file mode 100644
index 79b68c8..0000000
--- a/auth-system/src/main/java/cn/bunny/services/security/handelr/SecurityAuthenticationSuccessHandler.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package cn.bunny.services.security.handelr;
-
-import cn.bunny.domain.common.model.vo.result.Result;
-import com.alibaba.fastjson2.JSON;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
-
-import java.io.IOException;
-
-/**
- * 登录成功
- */
-public class SecurityAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
- @Override
- public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
- // 获取用户身份信息
- Object principal = authentication.getPrincipal();
- Result