WebSocket跨域问题解决

package com.oms.ppx.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration  
@EnableWebMvc  
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer { 
	private static final long heartbeatTime =  60000L; // 1 minute
	
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    	registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("*").addInterceptors(new MyHandshakeInterceptor());
    	   
    	registry.addHandler(myHandler(), "/sockjs/myHandler").setAllowedOrigins("*").addInterceptors(new MyHandshakeInterceptor())
    		.withSockJS().setHeartbeatTime(heartbeatTime);
    }
    

}

setAllowedOrigins("*") 就是解决WebSocket跨域问题的核心代码

你可能感兴趣的:(java)