使用 Spring Web Sockets 和 STOMP 自定义 Jetty WebSocketPolicy

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

我想在使用带有 STOMP 的 Spring Web Sockets 时自定义 Jetty

WebSocketPolicy

这是我的配置类:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
        registration.setMessageSizeLimit(128 * 1024);
    }

}

当通过

WebSocketTransportRegistration
设置消息大小限制时,它并不能解决问题,因为在Spring涉及之前,Jetty已经进行了短信大小检查。您可以通过以下堆栈跟踪查看它:

org.eclipse.jetty.websocket.api.MessageTooLargeException: Text message size [70412] exceeds maximum size [65536]
    at org.eclipse.jetty.websocket.api.WebSocketPolicy.assertValidTextMessageSize(WebSocketPolicy.java:140) ~[websocket-api-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.websocket.common.Parser.assertSanePayloadLength(Parser.java:127) ~[websocket-common-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.websocket.common.Parser.parseFrame(Parser.java:485) ~[websocket-common-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.websocket.common.Parser.parse(Parser.java:241) ~[websocket-common-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:560) [websocket-common-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:391) [websocket-common-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281) [jetty-io-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102) [jetty-io-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118) [jetty-io-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680) [jetty-util-9.4.11.v20180605.jar:9.4.11.v20180605]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]

因此,我正在寻找一种方法来提供自定义

WebSocketPolicy
并在创建
JettyRequestUpgradeStrategy
时传递它,但我找不到方法来做到这一点。

使用以下配置类时看起来是可能的,但随后它会丢失消息代理配置:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

}
java spring jetty spring-websocket
2个回答
1
投票

解决方案很简单,但没有记录......

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").setHandshakeHandler(handshakeHandler());
    registry.addEndpoint("/ws").setHandshakeHandler(handshakeHandler()).withSockJS();
  }

  @Bean
  public DefaultHandshakeHandler handshakeHandler() {
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    policy.setMaxTextMessageSize(128 * 1024);

    return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy(policy));
  }
}

0
投票

在较新版本的 Spring 中,JettyRequestUpgradeStrategy 中以策略作为参数的构造函数已被删除。所以我对@manash的解决方案做了一个小小的调整。

这就是现在的运作方式:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").setHandshakeHandler(handshakeHandler());
    registry.addEndpoint("/ws").setHandshakeHandler(handshakeHandler()).withSockJS();
  }

  // This has changed
  @Bean
  public DefaultHandshakeHandler handshakeHandler() {
    var updateStrategy = new JettyRequestUpgradeStrategy();
    updateStrategy.addWebSocketConfigurer(config -> {
       config.setMaxTextMessageSize(128 * 1024);
    });

    return new DefaultHandshakeHandler(updateStrategy);
}

供参考:

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