From 638fbb9613b390bd66c64e0e069b2cb16876a5ed Mon Sep 17 00:00:00 2001 From: lijiahang Date: Thu, 28 Dec 2023 11:44:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=20websocket=20?= =?UTF-8?q?=E9=85=8D=E7=BD=AE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrionSecurityAutoConfiguration.java | 6 ++- .../WebsocketAuthorizeRequestsCustomizer.java | 8 +++- ...a => OrionWebSocketAutoConfiguration.java} | 6 +-- ...socketConfig.java => WebSocketConfig.java} | 10 ++++- .../spring-configuration-metadata.json | 34 +++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 2 +- .../src/main/resources/application.yaml | 18 +++++---- .../interceptor/TerminalInterceptor1.java | 37 ------------------- 8 files changed, 67 insertions(+), 54 deletions(-) rename orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/{OrionWebsocketAutoConfiguration.java => OrionWebSocketAutoConfiguration.java} (91%) rename orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/{WebsocketConfig.java => WebSocketConfig.java} (68%) create mode 100644 orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring-configuration-metadata.json delete mode 100644 orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/interceptor/TerminalInterceptor1.java diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/config/OrionSecurityAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/config/OrionSecurityAutoConfiguration.java index 8223bb7b..92ef84c9 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/config/OrionSecurityAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/config/OrionSecurityAutoConfiguration.java @@ -14,6 +14,7 @@ import org.springframework.beans.factory.config.MethodInvokingFactoryBean; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -162,8 +163,9 @@ public class OrionSecurityAutoConfiguration { * @return websocket 安全策略 */ @Bean - public WebsocketAuthorizeRequestsCustomizer websocketAuthorizeRequestsCustomizer() { - return new WebsocketAuthorizeRequestsCustomizer(); + @ConditionalOnProperty(value = "orion.websocket.prefix") + public WebsocketAuthorizeRequestsCustomizer websocketAuthorizeRequestsCustomizer(@Value("${orion.websocket.prefix}") String prefix) { + return new WebsocketAuthorizeRequestsCustomizer(prefix); } /** diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/strategy/WebsocketAuthorizeRequestsCustomizer.java b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/strategy/WebsocketAuthorizeRequestsCustomizer.java index 7ace0747..e68eac6a 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/strategy/WebsocketAuthorizeRequestsCustomizer.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-security/src/main/java/com/orion/ops/framework/security/core/strategy/WebsocketAuthorizeRequestsCustomizer.java @@ -12,10 +12,16 @@ import org.springframework.security.config.annotation.web.configurers.Expression */ public class WebsocketAuthorizeRequestsCustomizer extends AuthorizeRequestsCustomizer { + private final String prefix; + + public WebsocketAuthorizeRequestsCustomizer(String prefix) { + this.prefix = prefix; + } + @Override public void customize(ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry registry) { // websocket 允许匿名访问 - registry.antMatchers("/orion/keep-alive/**").permitAll(); + registry.antMatchers(prefix + "/**").permitAll(); } } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/OrionWebsocketAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/OrionWebSocketAutoConfiguration.java similarity index 91% rename from orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/OrionWebsocketAutoConfiguration.java rename to orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/OrionWebSocketAutoConfiguration.java index c547a78a..b9a709cb 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/OrionWebsocketAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/OrionWebSocketAutoConfiguration.java @@ -20,14 +20,14 @@ import org.springframework.web.socket.server.standard.ServletServerContainerFact @EnableWebSocket @AutoConfiguration @AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_WEBSOCKET) -@EnableConfigurationProperties(WebsocketConfig.class) -public class OrionWebsocketAutoConfiguration { +@EnableConfigurationProperties(WebSocketConfig.class) +public class OrionWebSocketAutoConfiguration { /** * @return websocket 缓冲区大小配置 */ @Bean - public ServletServerContainerFactoryBean servletServerContainerFactoryBean(WebsocketConfig config) { + public ServletServerContainerFactoryBean servletServerContainerFactoryBean(WebSocketConfig config) { ServletServerContainerFactoryBean factory = new ServletServerContainerFactoryBean(); factory.setMaxBinaryMessageBufferSize(config.getBinaryBufferSize()); factory.setMaxTextMessageBufferSize(config.getBinaryBufferSize()); diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/WebsocketConfig.java b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/WebSocketConfig.java similarity index 68% rename from orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/WebsocketConfig.java rename to orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/WebSocketConfig.java index 36a7e556..a34dfedc 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/WebsocketConfig.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/java/com/orion/ops/framework/websocket/config/WebSocketConfig.java @@ -11,8 +11,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @since 2023/6/25 19:57 */ @Data -@ConfigurationProperties("spring.websocket") -public class WebsocketConfig { +@ConfigurationProperties("orion.websocket") +public class WebSocketConfig { /** * 二进制消息缓冲区大小 byte @@ -29,4 +29,10 @@ public class WebsocketConfig { */ private Long sessionIdleTimeout; + public WebSocketConfig() { + this.binaryBufferSize = 1024 * 1024; + this.textBufferSize = 1024 * 1024; + this.sessionIdleTimeout = 30 * 60 * 1000L; + } + } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring-configuration-metadata.json b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring-configuration-metadata.json new file mode 100644 index 00000000..8a2df355 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring-configuration-metadata.json @@ -0,0 +1,34 @@ +{ + "groups": [ + { + "name": "orion.websocket", + "type": "com.orion.ops.framework.websocket.config.WebSocketConfig", + "sourceType": "com.orion.ops.framework.websocket.config.WebSocketConfig" + } + ], + "properties": [ + { + "name": "orion.websocket.prefix", + "type": "java.lang.String", + "description": "公共 websocket 前缀" + }, + { + "name": "orion.websocket.binary-buffer-size", + "type": "java.lang.Integer", + "description": "二进制消息缓冲区大小 byte.", + "defaultValue": "1048576" + }, + { + "name": "orion.websocket.text-buffer-size", + "type": "java.lang.Integer", + "description": "文本消息缓冲区大小 byte.", + "defaultValue": "1048576" + }, + { + "name": "orion.websocket.session-idle-timeout", + "type": "java.lang.Long", + "description": "session 最大超时时间 ms.", + "defaultValue": "1800000" + } + ] +} \ No newline at end of file diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 23f104b6..a8e13663 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/orion-ops-framework/orion-ops-spring-boot-starter-websocket/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1 @@ -com.orion.ops.framework.websocket.config.OrionWebsocketAutoConfiguration \ No newline at end of file +com.orion.ops.framework.websocket.config.OrionWebSocketAutoConfiguration \ No newline at end of file diff --git a/orion-ops-launch/src/main/resources/application.yaml b/orion-ops-launch/src/main/resources/application.yaml index fc088a57..55ae14f3 100644 --- a/orion-ops-launch/src/main/resources/application.yaml +++ b/orion-ops-launch/src/main/resources/application.yaml @@ -19,13 +19,6 @@ spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER - websocket: - # 1MB - binary-buffer-size: 1048576 - # 1MB - text-buffer-size: 1048576 - # 30MIN - session-idle-timeout: 1800000 datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver @@ -129,10 +122,19 @@ orion: # 版本 version: @revision@ api: - # 公共api前缀 + # 公共 api 前缀 prefix: /orion-api # 是否开启跨域 cors: true + websocket: + # 公共 websocket 前缀 + prefix: /orion/keep-alive + # 1MB + binary-buffer-size: 1048576 + # 1MB + text-buffer-size: 1048576 + # 30MIN + session-idle-timeout: 1800000 swagger: # swagger 配置 title: orion-ops-pro 运维平台 diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/interceptor/TerminalInterceptor1.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/interceptor/TerminalInterceptor1.java deleted file mode 100644 index 37d4a545..00000000 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/interceptor/TerminalInterceptor1.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.orion.ops.module.asset.interceptor; - -import com.orion.ops.framework.websocket.core.interceptor.UserHandshakeInterceptor; -import org.springframework.context.annotation.Configuration; -import org.springframework.stereotype.Component; -import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.config.annotation.WebSocketConfigurer; -import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; -import org.springframework.web.socket.handler.TextWebSocketHandler; - -import javax.annotation.Resource; - -/** - * @author Jiahang Li - * @version 1.0.0 - * @since 2023/12/27 23:53 - */ -// @Configuration -public class TerminalInterceptor1 implements WebSocketConfigurer { - - @Resource - private UserHandshakeInterceptor userHandshakeInterceptor; - - @Override - public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - registry.addHandler(new TextWebSocketHandler() { - @Override - protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { - System.out.println(message); - } - }, "/orion/keep-alive/host/terminal1") - .addInterceptors(userHandshakeInterceptor) - .setAllowedOrigins("*"); - System.out.println("1231"); - } -}