我目前正在开发一个用于内部和外部请求的 Web Socket。但是,在尝试通过 Chrome 命令测试双向连接时,我收到以下错误:
"Origin 'null' is therefore not allowed access."
Web 套接字配置:
@Configuration
@EnableWebSocketMessageBroker
public class SocketConfig implements WebSocketMessageBrokerConfigurer{
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/bidirectional");
}
public void registerStompEndpoints(StompEndpointRegistry registry) {
//register a new endpoint
registry.addEndpoint("/blueframe").setAllowedOrigins("/*");
registry.addEndpoint("/blueframe").setAllowedOrigins("/*").withSockJS();
}
}
JavaScript 连接:
var socket = new SockJS('http://192.168.137.23:8080/blueframe');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
});
我已经尝试查看其他帖子,但没有结果。
最佳答案
首先我放弃使用 Webservices,而是开始使用 RabbitMQ 进行双向通信。
关于 Web Socket 配置,我不是唯一遇到此问题的人,因此我找到了一个部分解决方案:降级 Spring 版本或使用 Socket 处理程序而不是 StompEndpointRegistry 接口(interface)。
关于java - Spring - WebSocket - Origin 'null' 因此不允许访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49861120/