io.github.dengchen2020.websocket.config.SpringWebSocketAutoConfiguration Maven / Gradle / Ivy
package io.github.dengchen2020.websocket.config;
import io.github.dengchen2020.websocket.BaseSpringWebSocketHandler;
import io.github.dengchen2020.websocket.properties.WebSocketProperties;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.List;
/**
* spring封装的websocket自动配置
* @author dengchen
* @since 2024/6/27
*/
@ConditionalOnProperty(value = "dc.websocket.enabled", matchIfMissing = true, havingValue = "true")
@EnableConfigurationProperties(WebSocketProperties.class)
@EnableWebSocket
@Configuration(proxyBeanMethods = false)
public class SpringWebSocketAutoConfiguration implements WebSocketConfigurer {
private final List springWebSocketHandles;
private final HandshakeHandler handshakeHandler;
private final HandshakeInterceptor[] handshakeInterceptors;
private final WebSocketProperties webSocketProperties;
public SpringWebSocketAutoConfiguration(@Nullable List springWebSocketHandles, @Nullable HandshakeHandler handshakeHandler, @Nullable HandshakeInterceptor[] handshakeInterceptors, WebSocketProperties webSocketProperties) {
this.springWebSocketHandles = springWebSocketHandles;
this.handshakeHandler = handshakeHandler;
this.handshakeInterceptors = handshakeInterceptors;
this.webSocketProperties = webSocketProperties;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
if (springWebSocketHandles == null) return;
for (BaseSpringWebSocketHandler handle : springWebSocketHandles) {
if (handle.getClass().getAnnotation(ServerEndpoint.class) != null) throw new RuntimeException("无需添加@ServerEndpoint注解," + handle.getClass().getName());
if (handle.getPaths() == null || handle.getPaths().length == 0) throw new RuntimeException(handle.getClass().getName() + " 未配置WebSocket端口映射路径," + "请实现getPaths()方法");
WebSocketHandlerRegistration registration = registry.addHandler(handle, handle.getPaths());
if(webSocketProperties.getAllowedOrigins() != null) registration.setAllowedOrigins(webSocketProperties.getAllowedOrigins());
if(webSocketProperties.getAllowedOriginPatterns() != null) registration.setAllowedOriginPatterns(webSocketProperties.getAllowedOriginPatterns());
if(webSocketProperties.getAllowedOrigins() == null && webSocketProperties.getAllowedOriginPatterns() == null) registration.setAllowedOriginPatterns("*");
if(webSocketProperties.isWithSockJS()) registration.withSockJS();
if(handshakeHandler != null) registration.setHandshakeHandler(handshakeHandler);
if(handshakeInterceptors != null) registration.addInterceptors(handshakeInterceptors);
}
}
}