55 lines
1.9 KiB
Java
55 lines
1.9 KiB
Java
package com.emotion.config;
|
|
|
|
import com.emotion.interceptor.WebSocketAuthInterceptor;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.messaging.simp.config.ChannelRegistration;
|
|
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
|
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
|
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
|
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
|
|
|
/**
|
|
* WebSocket配置
|
|
*
|
|
* @author huazhongmin
|
|
* @date 2025-07-22
|
|
*/
|
|
@Configuration
|
|
@EnableWebSocketMessageBroker
|
|
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
|
|
|
@Autowired
|
|
private WebSocketAuthInterceptor webSocketAuthInterceptor;
|
|
|
|
@Override
|
|
public void configureMessageBroker(MessageBrokerRegistry config) {
|
|
// 启用简单消息代理,并设置消息代理的前缀
|
|
config.enableSimpleBroker("/topic", "/queue", "/user");
|
|
|
|
// 设置应用程序的目标前缀
|
|
config.setApplicationDestinationPrefixes("/app");
|
|
|
|
// 设置用户目标前缀
|
|
config.setUserDestinationPrefix("/user");
|
|
}
|
|
|
|
@Override
|
|
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
|
// 注册STOMP端点
|
|
registry.addEndpoint("/ws/chat")
|
|
.setAllowedOriginPatterns("*")
|
|
.withSockJS();
|
|
|
|
// 注册普通WebSocket端点
|
|
registry.addEndpoint("/ws/chat")
|
|
.setAllowedOriginPatterns("*");
|
|
}
|
|
|
|
@Override
|
|
public void configureClientInboundChannel(ChannelRegistration registration) {
|
|
// 添加WebSocket认证拦截器
|
|
registration.interceptors(webSocketAuthInterceptor);
|
|
}
|
|
}
|