diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index cd599ffdf..7b056c46b 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -23,8 +23,8 @@ 2.5 1.2.15 - 3.5.3 - 3.5.2 + 3.5.3.1 + 3.5.3.1 3.6.1 3.18.0 diff --git a/yudao-framework/pom.xml b/yudao-framework/pom.xml index 1928aa88c..3bfc020ba 100644 --- a/yudao-framework/pom.xml +++ b/yudao-framework/pom.xml @@ -40,6 +40,7 @@ yudao-spring-boot-starter-flowable yudao-spring-boot-starter-captcha + yudao-spring-boot-starter-websocket yudao-framework diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/pom.xml b/yudao-framework/yudao-spring-boot-starter-websocket/pom.xml new file mode 100644 index 000000000..320e52c48 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/pom.xml @@ -0,0 +1,37 @@ + + + + cn.iocoder.boot + yudao-framework + ${revision} + + 4.0.0 + yudao-spring-boot-starter-websocket + jar + + ${project.artifactId} + WebSocket + https://github.com/YunaiV/ruoyi-vue-pro + + + + + + cn.iocoder.boot + yudao-common + + + + cn.iocoder.boot + yudao-spring-boot-starter-security + + + + org.springframework.boot + spring-boot-starter-websocket + + + + \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/WebSocketHandlerConfig.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/WebSocketHandlerConfig.java new file mode 100644 index 000000000..02c3415d5 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/WebSocketHandlerConfig.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.framework.websocket.config; + +import cn.iocoder.yudao.framework.websocket.core.UserHandshakeInterceptor; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.web.socket.server.HandshakeInterceptor; + +@EnableConfigurationProperties(WebSocketProperties.class) +public class WebSocketHandlerConfig { + @Bean + public HandshakeInterceptor handshakeInterceptor() { + return new UserHandshakeInterceptor(); + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/WebSocketProperties.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/WebSocketProperties.java new file mode 100644 index 000000000..0ab1b498f --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/WebSocketProperties.java @@ -0,0 +1,29 @@ +package cn.iocoder.yudao.framework.websocket.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.validation.annotation.Validated; + +/** + * WebSocket 配置项 + * + * @author xingyu4j + */ +@ConfigurationProperties("yudao.websocket") +@Data +@Validated +public class WebSocketProperties { + + /** + * 路径 + */ + private String path = ""; + /** + * 默认最多允许同时在线用户数 + */ + private int maxOnlineCount = 0; + /** + * 是否保存session + */ + private boolean sessionMap = true; +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/YudaoWebSocketAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/YudaoWebSocketAutoConfiguration.java new file mode 100644 index 000000000..f8c50ae6a --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/config/YudaoWebSocketAutoConfiguration.java @@ -0,0 +1,34 @@ +package cn.iocoder.yudao.framework.websocket.config; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.server.HandshakeInterceptor; + +import java.util.List; + +/** + * WebSocket 自动配置 + * + * @author xingyu4j + */ +@AutoConfiguration +// 允许使用 yudao.websocket.enable=false 禁用websocket +@ConditionalOnProperty(prefix = "yudao.websocket", value = "enable", matchIfMissing = true) +@EnableConfigurationProperties(WebSocketProperties.class) +public class YudaoWebSocketAutoConfiguration { + @Bean + @ConditionalOnMissingBean + public WebSocketConfigurer webSocketConfigurer(List handshakeInterceptor, + WebSocketHandler webSocketHandler, + WebSocketProperties webSocketProperties) { + + return registry -> registry + .addHandler(webSocketHandler, webSocketProperties.getPath()) + .addInterceptors(handshakeInterceptor.toArray(new HandshakeInterceptor[0])); + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/UserHandshakeInterceptor.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/UserHandshakeInterceptor.java new file mode 100644 index 000000000..3f2fa4ec3 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/UserHandshakeInterceptor.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.framework.websocket.core; + +import cn.iocoder.yudao.framework.security.core.LoginUser; +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.HandshakeInterceptor; + +import java.util.Map; + +public class UserHandshakeInterceptor implements HandshakeInterceptor { + @Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); + attributes.put(WebSocketKeyDefine.LOGIN_USER, loginUser); + return true; + } + + @Override + public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { + + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketKeyDefine.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketKeyDefine.java new file mode 100644 index 000000000..f75ebc41c --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketKeyDefine.java @@ -0,0 +1,9 @@ +package cn.iocoder.yudao.framework.websocket.core; + + +import lombok.Data; + +@Data +public class WebSocketKeyDefine { + public static final String LOGIN_USER ="LOGIN_USER"; +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketMessageDO.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketMessageDO.java new file mode 100644 index 000000000..7bb348e99 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketMessageDO.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.framework.websocket.core; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.util.List; + +@Data +@Accessors(chain = true) +public class WebSocketMessageDO { + /** + * 接收消息的seesion + */ + private List seesionKeyList; + /** + * 发送消息 + */ + private String msgText; + + public static WebSocketMessageDO build(List seesionKeyList, String msgText) { + return new WebSocketMessageDO().setMsgText(msgText).setSeesionKeyList(seesionKeyList); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketSessionHandler.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketSessionHandler.java new file mode 100644 index 000000000..2747f8192 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketSessionHandler.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.framework.websocket.core; + +import org.springframework.web.socket.WebSocketSession; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +public final class WebSocketSessionHandler { + private WebSocketSessionHandler() { + } + + private static final Map SESSION_MAP = new ConcurrentHashMap<>(); + + public static void addSession(Object sessionKey, WebSocketSession session) { + SESSION_MAP.put(sessionKey.toString(), session); + } + + public static void removeSession(Object sessionKey) { + SESSION_MAP.remove(sessionKey.toString()); + } + + public static WebSocketSession getSession(Object sessionKey) { + return SESSION_MAP.get(sessionKey.toString()); + } + + public static Collection getSessions() { + return SESSION_MAP.values(); + } + + public static Set getSessionKeys() { + return SESSION_MAP.keySet(); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketUtils.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketUtils.java new file mode 100644 index 000000000..816e664cc --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/WebSocketUtils.java @@ -0,0 +1,31 @@ +package cn.iocoder.yudao.framework.websocket.core; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; + +import java.io.IOException; + +@Slf4j +public class WebSocketUtils { + public static boolean sendMessage(WebSocketSession seesion, String message) { + if (seesion == null) { + log.error("seesion 不存在"); + return false; + } + if (seesion.isOpen()) { + try { + seesion.sendMessage(new TextMessage(message)); + } catch (IOException e) { + log.error("WebSocket 消息发送异常 Session={} | msg= {} | exception={}", seesion, message, e); + return false; + } + } + return true; + } + + public static boolean sendMessage(Object sessionKey, String message) { + WebSocketSession session = WebSocketSessionHandler.getSession(sessionKey); + return sendMessage(session, message); + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/YudaoWebSocketHandlerDecorator.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/YudaoWebSocketHandlerDecorator.java new file mode 100644 index 000000000..dd8dc602e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/core/YudaoWebSocketHandlerDecorator.java @@ -0,0 +1,49 @@ +package cn.iocoder.yudao.framework.websocket.core; + +import cn.iocoder.yudao.framework.security.core.LoginUser; +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.WebSocketHandlerDecorator; + +public class YudaoWebSocketHandlerDecorator extends WebSocketHandlerDecorator { + public YudaoWebSocketHandlerDecorator(WebSocketHandler delegate) { + super(delegate); + } + + /** + * websocket 连接时执行的动作 + * @param session websocket session 对象 + * @throws Exception 异常对象 + */ + @Override + public void afterConnectionEstablished(final WebSocketSession session) throws Exception { + Object sessionKey = sessionKeyGen(session); + WebSocketSessionHandler.addSession(sessionKey, session); + } + + /** + * websocket 关闭连接时执行的动作 + * @param session websocket session 对象 + * @param closeStatus 关闭状态对象 + * @throws Exception 异常对象 + */ + @Override + public void afterConnectionClosed(final WebSocketSession session, CloseStatus closeStatus) throws Exception { + Object sessionKey = sessionKeyGen(session); + WebSocketSessionHandler.removeSession(sessionKey); + } + + public Object sessionKeyGen(WebSocketSession webSocketSession) { + + Object obj = webSocketSession.getAttributes().get(WebSocketKeyDefine.LOGIN_USER); + + if (obj instanceof LoginUser) { + LoginUser loginUser = (LoginUser) obj; + // userId 作为唯一区分 + return String.valueOf(loginUser.getId()); + } + + return null; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/package-info.java b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/package-info.java new file mode 100644 index 000000000..c771dfaac --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/java/cn/iocoder/yudao/framework/websocket/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.framework.websocket; diff --git a/yudao-framework/yudao-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 000000000..6260e407e --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +cn.iocoder.yudao.framework.websocket.config.YudaoWebSocketAutoConfiguration \ No newline at end of file diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index d6b0ea3e8..deabcba49 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -92,6 +92,11 @@ yudao: security: permit-all_urls: - /admin-ui/** # /resources/admin-ui 目录下的静态资源 + websocket: + enable: true # websocket的开关 + path: /websocket/message # 路径 + maxOnlineCount: 0 # 最大连接人数 + sessionMap: true # 保存sessionMap swagger: title: 管理后台 description: 提供管理员管理的所有功能