CORS允许使用Spring MVC简单消息代理不应用源

时间:2018-04-16 16:50:23

标签: spring-mvc spring-websocket

我正在使用带有Spring& amp;的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是唯一的配置方式(代码中没有注释或配置)。

有谁知道为什么设置允许的来源不起作用或者我遗漏了什么?

1 个答案:

答案 0 :(得分:0)

我尝试在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);
    }
}

不要忘记在

中添加软件包