zjh
16 小时以前 99d28bddd6ad5bd7c87babf7dcab670215993cb6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.ltkj.web.websocket;
 
import com.ltkj.web.websocket.interceptor.TokenHandshakeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2025/6/27 15:47
 */
@Configuration
@EnableWebSocket
public class MyWebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private WebSockerManager webSockerManager;
    @Autowired
    private TokenHandshakeInterceptor tokenHandshakeInterceptor;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSockerManager, "/ws")
                .addInterceptors(tokenHandshakeInterceptor)
                .setAllowedOrigins("*"); // 允许跨域
    }
}