我在Spring中使用websockets &试图为我的STOMP端点配置CORS。我的application-context.xml中包含以下内容:
<mvc:cors>
<mvc:mapping path="/notifier/**" allowed-origins="http://mydomain1.com,http://mydomain2.com" allowed-methods="GET, PUT" />
</mvc:cors>
<websocket:message-broker >
<websocket:stomp-endpoint path="/notifier" allowed-origins="http://mydomain1.com,http://mydomain2.com" >
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>但是,由于允许的来源是*.*,导致订阅请求在客户端失败,以下CURL请求确认了这一点:
curl http://localhost:8080/notifier/info
{"entropy":2116774357,"origins":["*:*"],"cookie_needed":true,"websocket":true}XML是唯一的配置方法(代码中没有注释或配置)。
有没有人知道为什么设置允许的原点不起作用,或者我是否遗漏了什么?
发布于 2019-03-14 12:20:42
我尝试使用xml进行配置,但失败了,我使用了注释。
在函数registerStompEndpoints.中添加.setAllowedOrigins("*")
这是我的代码。
package config;
import org.springframework.context.annotation.Configuration;
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;
import org.springframework.messaging.simp.config.ChannelRegistration;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(1);
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.taskExecutor().corePoolSize(1);
}
}不要忘记在”中添加包
https://stackoverflow.com/questions/49862463
复制相似问题