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

问题描述 投票:0回答:1

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

有谁知道为什么设置允许的起源不起作用或是否有任何我遗漏的东西?

spring-mvc spring-websocket
1个回答
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);
    }
}

不要忘记在<context:component-scan base-package =“config”/>中添加包

© www.soinside.com 2019 - 2024. All rights reserved.