java - 在 SockJS 和 Tomcat 上使用 STOMP 的 Spring 没有升级到 Websockets

标签 java spring tomcat spring-websocket sockjs

我正在构建一个无状态 Spring (4.2.4.RELEASE) 解决方案,使用 STOMP over Websockets 和 SockJS,以及一个使用 JWT 的 Rest Endpoint 来连接具有全双工通信的移动设备。我正在使用 Tomcat 8.0.33 作为 Web 服务器,并使用带有 sockjs javascript 客户端的 html 进行测试。 stomp 协议(protocol)使用 http 后备可以正常工作,但我不能只使用 websocket 协议(protocol)。我以多种方式尝试了 CORS,但我不确定这是 Tomcat 问题还是错误的 spring 配置。我甚至在相同的域和端口中测试了我的 html,SockJS 仍然退回到 xhr 或 iframes。

WebScoketConfig.java

@EnableWebSocketMessageBroker
 public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer
{

@Override
public void registerStompEndpoints(StompEndpointRegistry registry)
{
    RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
    registry.addEndpoint("/ws").setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
    .setAllowedOrigins("*").withSockJS().setSessionCookieNeeded(false)
    .setStreamBytesLimit(512 * 1024)
    .setHttpMessageCacheSize(1000)
    .setDisconnectDelay(30 * 1000);
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
    registration.taskExecutor().corePoolSize(50);
}


@Override
public void configureMessageBroker(MessageBrokerRegistry registry)
{
    registry.enableSimpleBroker("/queue/", "/topic/");
    // registry.enableStompBrokerRelay("/queue/", "/topic/");
    registry.setApplicationDestinationPrefixes("/myapp");
}

 public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
    registration.setMessageSizeLimit(500 * 1024);
    registration.setSendBufferSizeLimit(1024 * 1024);
    registration.setSendTimeLimit(20000);
 }
}

WebSecurityConfig.java

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter
    {

        @Override
        protected void configure(HttpSecurity http) throws Exception
        {
            http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/**").permitAll();                
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception
        {

        }
    }

最佳答案

我解决了我的问题,实际上代码很好,但是防病毒软件 (Kaspersky) 在打开后立即关闭了我的客户端浏览器上的连接。它迫使 SockJS 回退到不同的策略。我在关闭防病毒软件的情况下测试了客户端,并且 Websocket 传输运行良好。也在 mac 和 linux 上测试过。

关于java - 在 SockJS 和 Tomcat 上使用 STOMP 的 Spring 没有升级到 Websockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36448788/

相关文章:

java - Spring boot h2数据库获取驱动程序出错

multithreading - 如何以单线程模式启动我的 grails 开发服务器?

linux - 运行 bash 脚本以从另一个目录关闭 tomcat

java - 如何让倒计时进度条不断向下?

java - 关于 BlueJ 中按钮的几个问题

java - Itext页码在PDF中显示

java - Spring 验证 - 避免重复的约束消息

spring - FlywayAutoConfiguration 没有 "see"数据源

java - Tomcat 6、DB2 JNDI 数据源

java - 计算机启动时 apache 出错