refactor: 修改 websocket 配置.

This commit is contained in:
lijiahang
2023-12-28 11:44:04 +08:00
parent 47657da24d
commit 638fbb9613
8 changed files with 67 additions and 54 deletions

View File

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

View File

@@ -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<HttpSecurity>.ExpressionInterceptUrlRegistry registry) {
// websocket 允许匿名访问
registry.antMatchers("/orion/keep-alive/**").permitAll();
registry.antMatchers(prefix + "/**").permitAll();
}
}

View File

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

View File

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

View File

@@ -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"
}
]
}

View File

@@ -1 +1 @@
com.orion.ops.framework.websocket.config.OrionWebsocketAutoConfiguration
com.orion.ops.framework.websocket.config.OrionWebSocketAutoConfiguration

View File

@@ -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 运维平台

View File

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