feat(coalWashingDailyAnalysis): 实现基于 MD5(sessionId) 的消息广播

- 在 CoalWashingDailyAnalysisService 中添加 sessionId 的 MD5 值到广播消息
- 在 Ctx 类中新增 getSessionIdMd5 方法,用于获取 sessionId 的 MD5 值- 修改 SseService 中的 broadcast 方法,增加日志记录功能
This commit is contained in:
2025-03-12 22:02:26 +08:00
parent 8bd5423bbd
commit ab873e1fdc
3 changed files with 21 additions and 12 deletions

View File

@@ -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());
}

View File

@@ -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) {

View File

@@ -58,7 +58,9 @@ public class SseService {
@SneakyThrows
public void broadcast(String s, Object event) {
RTopic topic = redissonClient.getTopic(s);