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