【问题标题】:How Can I Disable Tomcat's permessage-deflate WebSocket Compression in Spring Boot?如何在 Spring Boot 中禁用 Tomcat 的 permessage-deflate WebSocket 压缩?
【发布时间】:2021-12-31 23:18:10
【问题描述】:

我有一个 Spring Boot 服务器,我希望能够与无法或不愿处理 permessage-deflate 压缩消息的 websocket 客户端通信。我从这两个关于该主题的类似问题(链接如下)中知道,我可以添加 VM 参数 -Dorg.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS=true 来禁用 Tomcat 的默认 deflate 压缩。

但是,我计划制作一个可以分发的程序,以便其他人可以运行它,并且不得不强迫人们记住始终包含特定的 VM 参数只是为了更改设置似乎很粗糙。

是否有一些替代方法可以禁用 Tomcat 的 websocket 压缩,它不需要用户在运行时指定 VM 参数,可能使用 Spring 的 Java 配置或自定义 websocket 握手拦截器?

【问题讨论】:

    标签: java spring-boot tomcat websocket


    【解决方案1】:

    您不仅可以使用 JVM 参数设置属性,还可以使用 System.setProperty 以编程方式设置属性,如下所示:

    System.setProperty("org.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS",String.valueOf(true));
    

    如果您使用嵌入式 tomcat 将项目导出到 JAR 文件,则可以在执行 SpringApplication.run 之前在 main 中运行它:

    public static void main(String[] args) {
        System.setProperty("org.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS",String.valueOf(true));
        SpringApplication.run(YourApplicationClass.class,args);
    }
    

    如果您将应用程序打包到 WAR 文件中,您可以尝试以下操作:

    @SpringBootApplication
    public class YourApplicationClass extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            System.setProperty("org.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS",String.valueOf(true));
            return application.sources(YourApplicationClass.class);
        } 
    } 
    

    【讨论】:

      猜你喜欢
      • 2015-05-07
      • 2015-10-26
      • 2014-11-21
      • 2016-12-20
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      相关资源
      最近更新 更多