首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CORS允许的来源未使用Spring MVC simple message broker应用

CORS允许的来源未使用Spring MVC simple message broker应用
EN

Stack Overflow用户
提问于 2018-04-17 00:50:23
回答 1查看 346关注 0票数 0

我在Spring中使用websockets &试图为我的STOMP端点配置CORS。我的application-context.xml中包含以下内容:

代码语言:javascript
复制
<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请求确认了这一点:

代码语言:javascript
复制
curl http://localhost:8080/notifier/info 
{"entropy":2116774357,"origins":["*:*"],"cookie_needed":true,"websocket":true}

XML是唯一的配置方法(代码中没有注释或配置)。

有没有人知道为什么设置允许的原点不起作用,或者我是否遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2019-03-14 12:20:42

我尝试使用xml进行配置,但失败了,我使用了注释。

在函数registerStompEndpoints.中添加.setAllowedOrigins("*")

这是我的代码。

代码语言:javascript
复制
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);
    }
}

不要忘记在”中添加包

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49862463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档