Spring Websocket 跨域访问

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tian330726/article/details/80695141

官方文档springwebsocket 4.1.5版本前默认支持跨域访问,之后的版本默认不支持跨域,需要设置:

.setAllowedOrigins("*")  
<websocket:handlers allowed-orgins="*">
    ...
</websocket:handlers>

具体实现代码:

方式一:

@Configuration  
@EnableWebMvc  
@EnableWebSocket  
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer{  

    @Override  
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {  
        registry.addHandler(systemWebSocketHandler(),"/webSocketServer");  
        registry.addHandler(systemWebSocketHandler(),"/webSocketServer/sockjs").setAllowedOrigins("*").withSockJS();    
    }  

    @Bean  
    public WebSocketHandler systemWebSocketHandler(){  
        return new SystemWebSocketHandler();  
    }

方式二:

<websocket:handlers allowed-origins="*">
     <websocket:mapping path="/webSocketHandler" handler="webSocketHandler"/>
     <!--<websocket:handshake-interceptors>
           <bean class="com.tyron.web.interceptor.WebSocketSessionHandshakeInterceptor"/>
     </websocket:handshake-interceptors>-->
</websocket:handlers>
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页