diff --git a/README.md b/README.md index 62cba4b1e..3841fda95 100644 --- a/README.md +++ b/README.md @@ -188,17 +188,17 @@ ps:核心功能已经实现,正在对接微信小程序中... | 框架 | 说明 | 版本 | 学习指南 | |---------------------------------------------------------------------------------------------|------------------|-------------|----------------------------------------------------------------| -| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.7.6 | [文档](https://github.com/YunaiV/SpringBoot-Labs) | +| [Spring Boot](https://spring.io/projects/spring-boot) | 应用开发框架 | 2.7.7 | [文档](https://github.com/YunaiV/SpringBoot-Labs) | | [MySQL](https://www.mysql.com/cn/) | 数据库服务器 | 5.7 / 8.0+ | | | [Druid](https://github.com/alibaba/druid) | JDBC 连接池、监控组件 | 1.2.15 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) | -| [MyBatis Plus](https://mp.baomidou.com/) | MyBatis 增强工具包 | 3.5.2 | [文档](http://www.iocoder.cn/Spring-Boot/MyBatis/?yudao) | -| [Dynamic Datasource](https://dynamic-datasource.com/) | 动态数据源 | 3.6.0 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) | +| [MyBatis Plus](https://mp.baomidou.com/) | MyBatis 增强工具包 | 3.5.3 | [文档](http://www.iocoder.cn/Spring-Boot/MyBatis/?yudao) | +| [Dynamic Datasource](https://dynamic-datasource.com/) | 动态数据源 | 3.6.1 | [文档](http://www.iocoder.cn/Spring-Boot/datasource-pool/?yudao) | | [Redis](https://redis.io/) | key-value 数据库 | 5.0 / 6.0 | | | [Redisson](https://github.com/redisson/redisson) | Redis 客户端 | 3.18.0 | [文档](http://www.iocoder.cn/Spring-Boot/Redis/?yudao) | | [Spring MVC](https://github.com/spring-projects/spring-framework/tree/master/spring-webmvc) | MVC 框架 | 5.3.24 | [文档](http://www.iocoder.cn/SpringMVC/MVC/?yudao) | | [Spring Security](https://github.com/spring-projects/spring-security) | Spring 安全框架 | 5.7.5 | [文档](http://www.iocoder.cn/Spring-Boot/Spring-Security/?yudao) | | [Hibernate Validator](https://github.com/hibernate/hibernate-validator) | 参数校验组件 | 6.2.5 | [文档](http://www.iocoder.cn/Spring-Boot/Validation/?yudao) | -| [Flowable](https://github.com/flowable/flowable-engine) | 工作流引擎 | 6.7.2 | [文档](https://doc.iocoder.cn/bpm/) | +| [Flowable](https://github.com/flowable/flowable-engine) | 工作流引擎 | 6.8.0 | [文档](https://doc.iocoder.cn/bpm/) | | [Quartz](https://github.com/quartz-scheduler) | 任务调度组件 | 2.3.2 | [文档](http://www.iocoder.cn/Spring-Boot/Job/?yudao) | | [Knife4j](https://gitee.com/xiaoym/knife4j) | Swagger 增强 UI 实现 | 3.0.3 | [文档](http://www.iocoder.cn/Spring-Boot/Swagger/?yudao) | | [Resilience4j](https://github.com/resilience4j/resilience4j) | 服务保障组件 | 1.7.1 | [文档](http://www.iocoder.cn/Spring-Boot/Resilience4j/?yudao) | @@ -222,8 +222,8 @@ ps:核心功能已经实现,正在对接微信小程序中... | 框架 | 说明 | 版本 | |----------------------------------------------------------------------|:------------:|:------:| | [Vue](https://staging-cn.vuejs.org/) | Vue 框架 | 3.2.45 | -| [Vite](https://cn.vitejs.dev//) | 开发与构建工具 | 4.0.3 | -| [Element Plus](https://element-plus.org/zh-CN/) | Element Plus | 2.2.27 | +| [Vite](https://cn.vitejs.dev//) | 开发与构建工具 | 4.0.4 | +| [Element Plus](https://element-plus.org/zh-CN/) | Element Plus | 2.2.28 | | [TypeScript](https://www.typescriptlang.org/docs/) | TypeScript | 4.9.4 | | [pinia](https://pinia.vuejs.org/) | vuex5 | 2.0.28 | | [vue-i18n](https://kazupon.github.io/vue-i18n/zh/introduction.html/) | 国际化 | 9.2.2 | diff --git a/pom.xml b/pom.xml index ede33e741..e267a1536 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ https://github.com/YunaiV/ruoyi-vue-pro - 1.6.5-snapshot + 1.6.6-snapshot 1.8 ${java.version} diff --git a/sql/mysql/optional/vue3-menu.sql b/sql/mysql/optional/vue3-menu.sql index 3ba7242ff..dfe3a6123 100644 --- a/sql/mysql/optional/vue3-menu.sql +++ b/sql/mysql/optional/vue3-menu.sql @@ -262,5 +262,6 @@ INSERT INTO `system_menu` VALUES (1266, '客户端更新', 'system:oauth2-client INSERT INTO `system_menu` VALUES (1267, '客户端删除', 'system:oauth2-client:delete', 3, 4, 1263, '', '', '', 0, b'1', b'1', '', '2022-05-10 16:26:33', '1', '2022-05-11 00:31:33', b'0'); INSERT INTO `system_menu` VALUES (1281, '可视化报表', '', 1, 12, 0, '/visualization', 'ep:histogram', NULL, 0, b'1', b'1', '1', '2022-07-10 20:22:15', '1', '2022-07-10 20:33:30', b'0'); INSERT INTO `system_menu` VALUES (1282, '积木报表', '', 2, 1, 1281, 'jimu-report', 'ep:histogram', 'visualization/jmreport/index', 0, b'1', b'1', '1', '2022-07-10 20:26:36', '1', '2022-07-28 21:17:34', b'0'); +INSERT INTO `system_menu` VALUES (1283, 'webSocket连接', '', 2, 14, 2, 'webSocket', 'ep:turn-off', 'infra/webSocket/index', 0, b'1', b'1', '1', '2023-01-01 11:43:04', '1', '2023-01-01 11:43:04', b'0'); SET FOREIGN_KEY_CHECKS = 1; diff --git a/sql/mysql/ruoyi-vue-pro.sql b/sql/mysql/ruoyi-vue-pro.sql index 39cb52afe..7b08e86fe 100644 --- a/sql/mysql/ruoyi-vue-pro.sql +++ b/sql/mysql/ruoyi-vue-pro.sql @@ -1710,6 +1710,8 @@ INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_i INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `status`, `visible`, `keep_alive`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1267, '客户端删除', 'system:oauth2-client:delete', 3, 4, 1263, '', '', '', 0, b'1', b'1', '', '2022-05-10 16:26:33', '1', '2022-05-11 00:31:33', b'0'); INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `status`, `visible`, `keep_alive`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1281, '可视化报表', '', 1, 12, 0, '/visualization', 'chart', NULL, 0, b'1', b'1', '1', '2022-07-10 20:22:15', '1', '2022-07-10 20:33:30', b'0'); INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `status`, `visible`, `keep_alive`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1282, '积木报表', '', 2, 1, 1281, 'jimu-report', 'example', 'visualization/jmreport/index', 0, b'1', b'1', '1', '2022-07-10 20:26:36', '1', '2022-07-28 21:17:34', b'0'); +INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `status`, `visible`, `keep_alive`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1283, 'webSocket连接', '', 2, 14, 2, 'webSocket', 'message', 'infra/webSocket/index', 0, b'1', b'1', '1', '2023-01-01 11:43:04', '1', '2023-01-01 11:43:04', b'0'); + COMMIT; -- ---------------------------- diff --git a/sql/optional/visualization/jimureport.mysql5.7.create.sql b/sql/optional/visualization/jimureport.mysql5.7.create.sql index e32134005..c90ea27d8 100644 --- a/sql/optional/visualization/jimureport.mysql5.7.create.sql +++ b/sql/optional/visualization/jimureport.mysql5.7.create.sql @@ -1344,6 +1344,7 @@ CREATE TABLE `jimu_report_share` ( `last_update_time` datetime NULL DEFAULT NULL COMMENT '最后更新时间', `term_of_validity` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '有效期(0:永久有效,1:1天,2:7天)', `status` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '是否过期(0未过期,1已过期)', + `preview_lock_status` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '密码锁状态', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '积木报表预览权限表' ROW_FORMAT = Dynamic; diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 809e7d16d..f4aa956fb 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -14,18 +14,18 @@ https://github.com/YunaiV/ruoyi-vue-pro - 1.6.5-snapshot + 1.6.6-snapshot - 2.7.6 + 2.7.7 3.0.3 1.6.8 2.5 1.2.15 - 3.5.2 - 3.5.2 - 3.6.0 + 3.5.3.1 + 3.5.3.1 + 3.6.1 3.18.0 2.2.3 @@ -37,14 +37,14 @@ 7.2.11.RELEASE 1.0.5 - 4.8.0 + 4.11.0 - 6.7.2 + 6.8.0 1.18.24 1.5.3.Final - 5.8.10 - 3.1.3 + 5.8.11 + 3.1.4 2.3 1.0.5 1.2.83 @@ -55,7 +55,7 @@ 0.1.55 2.6.0 1.3.0 - 4.1.85.Final + 4.1.86.Final 2.6.6 3.0.0 @@ -63,7 +63,7 @@ 8.4.6 4.6.3 2.2.1 - 3.1.637 + 3.1.660 1.4.0 1.5.6 2.12.2 @@ -602,6 +602,12 @@ xercesImpl ${xercesImpl.version} + + + org.springframework.boot + spring-boot-starter-websocket + ${spring.boot.version} + diff --git a/yudao-example/yudao-sso-demo-by-code/pom.xml b/yudao-example/yudao-sso-demo-by-code/pom.xml index f6a168f76..ef83df4ac 100644 --- a/yudao-example/yudao-sso-demo-by-code/pom.xml +++ b/yudao-example/yudao-sso-demo-by-code/pom.xml @@ -21,7 +21,7 @@ 8 UTF-8 - 2.7.6 + 2.7.7 @@ -52,7 +52,7 @@ cn.hutool hutool-all - 5.8.10 + 5.8.11 diff --git a/yudao-example/yudao-sso-demo-by-password/pom.xml b/yudao-example/yudao-sso-demo-by-password/pom.xml index c00032169..391358ee2 100644 --- a/yudao-example/yudao-sso-demo-by-password/pom.xml +++ b/yudao-example/yudao-sso-demo-by-password/pom.xml @@ -21,7 +21,7 @@ 8 UTF-8 - 2.7.6 + 2.7.7 @@ -52,7 +52,7 @@ cn.hutool hutool-all - 5.8.10 + 5.8.11 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-biz-pay/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-pay/pom.xml index 03f0013aa..06b127a0a 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/pom.xml @@ -52,7 +52,7 @@ com.alipay.sdk alipay-sdk-java - 4.35.0.ALL + 4.35.9.ALL org.bouncycastle diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java index a2b11b1dc..f554e0b52 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClient.java @@ -9,10 +9,11 @@ import io.minio.*; import java.io.ByteArrayInputStream; import static cn.iocoder.yudao.framework.file.core.client.s3.S3FileClientConfig.ENDPOINT_ALIYUN; +import static cn.iocoder.yudao.framework.file.core.client.s3.S3FileClientConfig.ENDPOINT_TENCENT; /** * 基于 S3 协议的文件客户端,实现 MinIO、阿里云、腾讯云、七牛云、华为云等云服务 - * + *

* S3 协议的客户端,采用亚马逊提供的 software.amazon.awssdk.s3 库 * * @author 芋道源码 @@ -78,6 +79,11 @@ public class S3FileClient extends AbstractFileClient { .replaceAll("-internal", "")// 去除内网 Endpoint 的后缀 .replaceAll("https://", ""); } + // 腾讯云必须有 region,否则会报错 + if (config.getEndpoint().contains(ENDPOINT_TENCENT)) { + return StrUtil.subAfter(config.getEndpoint(), ".cos.", false) + .replaceAll("." + ENDPOINT_TENCENT, ""); // 去除 Endpoint + } return null; } diff --git a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientConfig.java b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientConfig.java index 151159f5e..0c46e8aa6 100644 --- a/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientConfig.java +++ b/yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/s3/S3FileClientConfig.java @@ -19,6 +19,7 @@ public class S3FileClientConfig implements FileClientConfig { public static final String ENDPOINT_QINIU = "qiniucs.com"; public static final String ENDPOINT_ALIYUN = "aliyuncs.com"; + public static final String ENDPOINT_TENCENT = "myqcloud.com"; /** * 节点地址 diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/config/YudaoWebSecurityConfigurerAdapter.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/config/YudaoWebSecurityConfigurerAdapter.java index c3715c185..082d84756 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/config/YudaoWebSecurityConfigurerAdapter.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/config/YudaoWebSecurityConfigurerAdapter.java @@ -129,6 +129,8 @@ public class YudaoWebSecurityConfigurerAdapter { .antMatchers(buildAppApi("/**")).permitAll() // 1.5 验证码captcha 允许匿名访问 .antMatchers("/captcha/get", "/captcha/check").permitAll() + // 1.6 webSocket 允许匿名访问 + .antMatchers("/websocket/message").permitAll() // ②:每个项目的自定义规则 .and().authorizeRequests(registry -> // 下面,循环设置自定义规则 authorizeRequestsCustomizers.forEach(customizer -> customizer.customize(registry))) 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-module-infra/yudao-module-infra-biz/pom.xml b/yudao-module-infra/yudao-module-infra-biz/pom.xml index db23e697d..6d7ac0cff 100644 --- a/yudao-module-infra/yudao-module-infra-biz/pom.xml +++ b/yudao-module-infra/yudao-module-infra-biz/pom.xml @@ -111,6 +111,12 @@ cn.iocoder.boot yudao-spring-boot-starter-file + + + + org.springframework.boot + spring-boot-starter-websocket + diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/config/ConfigController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/config/ConfigController.java index 726a10498..493b5faa6 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/config/ConfigController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/config/ConfigController.java @@ -75,7 +75,7 @@ public class ConfigController { if (config == null) { return null; } - if (config.getVisible()) { + if (!config.getVisible()) { throw ServiceExceptionUtil.exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE); } return success(config.getValue()); diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileContentDAOImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileContentDAOImpl.java index c4dcfe8a0..2492c803d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileContentDAOImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileContentDAOImpl.java @@ -1,11 +1,14 @@ package cn.iocoder.yudao.module.infra.dal.mysql.file; +import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.file.core.client.db.DBFileContentFrameworkDAO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileContentDO; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.stereotype.Repository; import javax.annotation.Resource; +import java.util.List; +import java.util.Optional; @Repository public class FileContentDAOImpl implements DBFileContentFrameworkDAO { @@ -27,9 +30,11 @@ public class FileContentDAOImpl implements DBFileContentFrameworkDAO { @Override public byte[] selectContent(Long configId, String path) { - FileContentDO fileContentDO = fileContentMapper.selectOne( - buildQuery(configId, path).select(FileContentDO::getContent)); - return fileContentDO != null ? fileContentDO.getContent() : null; + List list = fileContentMapper.selectList( + buildQuery(configId, path).select(FileContentDO::getContent).orderByDesc(FileContentDO::getId)); + return Optional.ofNullable(CollUtil.getFirst(list)) + .map(FileContentDO::getContent) + .orElse(null); } private LambdaQueryWrapper buildQuery(Long configId, String path) { diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/SemaphoreUtils.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/SemaphoreUtils.java new file mode 100644 index 000000000..67a87f169 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/SemaphoreUtils.java @@ -0,0 +1,45 @@ +package cn.iocoder.yudao.module.infra.websocket; + +import lombok.extern.slf4j.Slf4j; + +import java.util.concurrent.Semaphore; + +/** + * 信号量相关处理 + * + */ +@Slf4j +public class SemaphoreUtils { + + /** + * 获取信号量 + * + * @param semaphore + * @return + */ + public static boolean tryAcquire(Semaphore semaphore) { + boolean flag = false; + + try { + flag = semaphore.tryAcquire(); + } catch (Exception e) { + log.error("获取信号量异常", e); + } + + return flag; + } + + /** + * 释放信号量 + * + * @param semaphore + */ + public static void release(Semaphore semaphore) { + + try { + semaphore.release(); + } catch (Exception e) { + log.error("释放信号量异常", e); + } + } +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketConfig.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketConfig.java new file mode 100644 index 000000000..380bc9317 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketConfig.java @@ -0,0 +1,16 @@ +package cn.iocoder.yudao.module.infra.websocket; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +/** + * websocket 配置 + */ +@Configuration +public class WebSocketConfig { + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketServer.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketServer.java new file mode 100644 index 000000000..f0cfdd9dc --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketServer.java @@ -0,0 +1,86 @@ +package cn.iocoder.yudao.module.infra.websocket; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; +import java.util.concurrent.Semaphore; + +/** + * websocket 消息处理 + */ +@Component +@ServerEndpoint("/websocket/message") +@Slf4j +public class WebSocketServer { + + /** + * 默认最多允许同时在线用户数100 + */ + public static int socketMaxOnlineCount = 100; + + private static final Semaphore SOCKET_SEMAPHORE = new Semaphore(socketMaxOnlineCount); + + /** + * 连接建立成功调用的方法 + */ + @OnOpen + public void onOpen(Session session) throws Exception { + // 尝试获取信号量 + boolean semaphoreFlag = SemaphoreUtils.tryAcquire(SOCKET_SEMAPHORE); + if (!semaphoreFlag) { + // 未获取到信号量 + log.error("当前在线人数超过限制数:{}", socketMaxOnlineCount); + WebSocketUsers.sendMessage(session, "当前在线人数超过限制数:" + socketMaxOnlineCount); + session.close(); + } else { + String userId = WebSocketUsers.getParam("userId", session); + if (userId != null) { + // 添加用户 + WebSocketUsers.addSession(userId, session); + log.info("用户【userId={}】建立连接,当前连接用户总数:{}", userId, WebSocketUsers.getUsers().size()); + WebSocketUsers.sendMessage(session, "接收内容:连接成功"); + } else { + WebSocketUsers.sendMessage(session, "接收内容:连接失败"); + } + } + } + + /** + * 连接关闭时处理 + */ + @OnClose + public void onClose(Session session) { + log.info("用户【sessionId={}】关闭连接!", session.getId()); + // 移除用户 + WebSocketUsers.removeSession(session); + // 获取到信号量则需释放 + SemaphoreUtils.release(SOCKET_SEMAPHORE); + } + + /** + * 抛出异常时处理 + */ + @OnError + public void onError(Session session, Throwable exception) throws Exception { + if (session.isOpen()) { + // 关闭连接 + session.close(); + } + String sessionId = session.getId(); + log.info("用户【sessionId={}】连接异常!异常信息:{}", sessionId, exception); + // 移出用户 + WebSocketUsers.removeSession(session); + // 获取到信号量则需释放 + SemaphoreUtils.release(SOCKET_SEMAPHORE); + } + + /** + * 收到客户端消息时调用的方法 + */ + @OnMessage + public void onMessage(Session session, String message) { + WebSocketUsers.sendMessage(session, "接收内容:" + message); + } +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketUsers.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketUsers.java new file mode 100644 index 000000000..281a97c7d --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/websocket/WebSocketUsers.java @@ -0,0 +1,178 @@ +package cn.iocoder.yudao.module.infra.websocket; + +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; +import org.bouncycastle.util.Strings; + +import javax.validation.constraints.NotNull; +import javax.websocket.Session; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * websocket 客户端用户 + */ +@Slf4j +public class WebSocketUsers { + + /** + * 用户集 + * TODO 需要登录用户的session? + */ + private static final Map SESSION_MAP = new ConcurrentHashMap<>(); + + /** + * 存储用户 + * + * @param userId 唯一键 + * @param session 用户信息 + */ + public static void addSession(String userId, Session session) { + SESSION_MAP.put(userId, session); + } + + /** + * 移除用户 + * + * @param session 用户信息 + * @return 移除结果 + */ + public static boolean removeSession(Session session) { + String key = null; + boolean flag = SESSION_MAP.containsValue(session); + if (flag) { + Set> entries = SESSION_MAP.entrySet(); + for (Map.Entry entry : entries) { + Session value = entry.getValue(); + if (value.equals(session)) { + key = entry.getKey(); + break; + } + } + } else { + return true; + } + return removeSession(key); + } + + /** + * 移出用户 + * + * @param userId 用户id + */ + public static boolean removeSession(String userId) { + log.info("用户【userId={}】退出", userId); + Session remove = SESSION_MAP.remove(userId); + if (remove != null) { + boolean containsValue = SESSION_MAP.containsValue(remove); + log.info("用户【userId={}】退出{},当前连接用户总数:{}", userId, containsValue ? "失败" : "成功", SESSION_MAP.size()); + return containsValue; + } else { + return true; + } + } + + /** + * 获取在线用户列表 + * + * @return 返回用户集合 + */ + public static Map getUsers() { + return SESSION_MAP; + } + + /** + * 向所有在线人发送消息 + * + * @param message 消息内容 + */ + public static void sendMessageToAll(String message) { + SESSION_MAP.forEach((userId, session) -> { + if (session.isOpen()) { + sendMessage(session, message); + } + }); + } + + /** + * 异步发送文本消息 + * + * @param session 用户session + * @param message 消息内容 + */ + public static void sendMessageAsync(Session session, String message) { + if (session.isOpen()) { + // TODO 需要加synchronized锁(synchronized(session))?单个session创建线程? + session.getAsyncRemote().sendText(message); + } else { + log.warn("用户【session={}】不在线", session.getId()); + } + } + + /** + * 同步发送文本消息 + * + * @param session 用户session + * @param message 消息内容 + */ + public static void sendMessage(Session session, String message) { + try { + if (session.isOpen()) { + // TODO 需要加synchronized锁(synchronized(session))?单个session创建线程? + session.getBasicRemote().sendText(message); + } else { + log.warn("用户【session={}】不在线", session.getId()); + } + } catch (IOException e) { + log.error("发送消息异常", e); + } + + } + + /** + * 根据用户id发送消息 + * + * @param userId 用户id + * @param message 消息内容 + */ + public static void sendMessage(String userId, String message) { + Session session = SESSION_MAP.get(userId); + //判断是否存在该用户的session,并且是否在线 + if (session == null || !session.isOpen()) { + return; + } + sendMessage(session, message); + } + + + /** + * 获取session中的指定参数值 + * + * @param key 参数key + * @param session 用户session + */ + public static String getParam(@NotNull String key, Session session) { + //TODO 目前只针对获取一个key的值,后期根据情况拓展多个 或者直接在onClose onOpen上获取参数? + String value = null; + Map> parameters = session.getRequestParameterMap(); + if (MapUtil.isNotEmpty(parameters)) { + value = parameters.get(key).get(0); + } else { + String queryString = session.getQueryString(); + if (!StrUtil.isEmpty(queryString)) { + String[] params = Strings.split(queryString, '&'); + for (String paramPair : params) { + String[] nameValues = Strings.split(paramPair, '='); + if (key.equals(nameValues[0])) { + value = nameValues[1]; + } + } + } + } + return value; + } +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/views/index.vue.vm b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/views/index.vue.vm index 06fe3002a..6b520c2c3 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/views/index.vue.vm +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/codegen/vue3/views/index.vue.vm @@ -1,7 +1,7 @@