From ab873e1fdc8594e479866ee14e7ad55e72de213b Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Wed, 12 Mar 2025 22:02:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(coalWashingDailyAnalysis):=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=9F=BA=E4=BA=8E=20MD5(sessionId)=20=E7=9A=84?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=B9=BF=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CoalWashingDailyAnalysisService 中添加 sessionId 的 MD5 值到广播消息 - 在 Ctx 类中新增 getSessionIdMd5 方法,用于获取 sessionId 的 MD5 值- 修改 SseService 中的 broadcast 方法,增加日志记录功能 --- .../CoalWashingDailyAnalysisService.java | 6 ++--- .../java/cn/lihongjie/coal/common/Ctx.java | 23 ++++++++++++------- .../coal/sse/service/SseService.java | 4 +++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java index f07ffa2e..694554d7 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/service/CoalWashingDailyAnalysisService.java @@ -127,7 +127,7 @@ public class CoalWashingDailyAnalysisService } this.repository.save(entity); - sseService.broadcast("coalWashingDailyAnalysis." + entity.getId(), ImmutableMap.of("id", entity.getId(), "event", "update")); + sseService.broadcast("coalWashingDailyAnalysis." + entity.getId(), ImmutableMap.of("id", entity.getId(), "event", "update", "sessionId", Ctx.getSessionIdMd5())); return getById(entity.getId()); } @@ -154,7 +154,7 @@ public class CoalWashingDailyAnalysisService entity.setName(productService.get(entity.getProduct().getId()).getName()); } this.repository.save(entity); - sseService.broadcast("coalWashingDailyAnalysis." + entity.getId(), ImmutableMap.of("id", entity.getId(), "event", "updateKfItems")); + sseService.broadcast("coalWashingDailyAnalysis." + entity.getId(), ImmutableMap.of("id", entity.getId(), "event", "updateKfItems", "sessionId", Ctx.getSessionIdMd5())); return getById(entity.getId()); } @@ -183,7 +183,7 @@ public class CoalWashingDailyAnalysisService entity.setName(productService.get(entity.getProduct().getId()).getName()); } this.repository.save(entity); - sseService.broadcast("coalWashingDailyAnalysis." + entity.getId(), ImmutableMap.of("id", entity.getId(), "event", "updateMain")); + sseService.broadcast("coalWashingDailyAnalysis." + entity.getId(), ImmutableMap.of("id", entity.getId(), "event", "updateMain", "sessionId", Ctx.getSessionIdMd5())); return getById(entity.getId()); } diff --git a/src/main/java/cn/lihongjie/coal/common/Ctx.java b/src/main/java/cn/lihongjie/coal/common/Ctx.java index 15694ea6..ec413acd 100644 --- a/src/main/java/cn/lihongjie/coal/common/Ctx.java +++ b/src/main/java/cn/lihongjie/coal/common/Ctx.java @@ -6,6 +6,7 @@ import cn.lihongjie.coal.user.dto.UserDto; import lombok.Getter; import lombok.experimental.UtilityClass; +import org.apache.commons.codec.digest.DigestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -13,8 +14,7 @@ import org.springframework.security.core.context.SecurityContextHolder; @UtilityClass public class Ctx { - @Getter - private static ConfigurableApplicationContext context; + @Getter private static ConfigurableApplicationContext context; public static String getUserId() { if (!isLoggedIn()) { @@ -35,14 +35,21 @@ public class Ctx { return getAuthentication().getSessionId(); } + public static String getSessionIdMd5() { + if (!isLoggedIn()) { + return ""; + } + String sessionId = getAuthentication().getSessionId(); + + if (sessionId == null) return ""; + return DigestUtils.md5Hex(sessionId); + } + private static SessionService.MyAuthentication getAuthentication() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if( ! (authentication instanceof SessionService.MyAuthentication)) - return null; + if (!(authentication instanceof SessionService.MyAuthentication)) return null; - - return (SessionService.MyAuthentication) - authentication; + return (SessionService.MyAuthentication) authentication; } public static boolean isOrgAdmin() { @@ -59,7 +66,7 @@ public class Ctx { if (!isLoggedIn()) { return null; } - return getAuthentication().getUser(); + return getAuthentication().getUser(); } public static void setContext(ConfigurableApplicationContext context) { diff --git a/src/main/java/cn/lihongjie/coal/sse/service/SseService.java b/src/main/java/cn/lihongjie/coal/sse/service/SseService.java index 2b0d95f4..8a6b8d74 100644 --- a/src/main/java/cn/lihongjie/coal/sse/service/SseService.java +++ b/src/main/java/cn/lihongjie/coal/sse/service/SseService.java @@ -58,7 +58,9 @@ public class SseService { @SneakyThrows public void broadcast(String s, Object event) { - + + + RTopic topic = redissonClient.getTopic(s);