Home > Software design >  How Can I Disable Tomcat's permessage-deflate WebSocket Compression in Spring Boot?
How Can I Disable Tomcat's permessage-deflate WebSocket Compression in Spring Boot?

Time:11-22

I have a Spring Boot server that I would like to be able to communicate with websocket clients that are unable or unwilling to handle permessage-deflate compressed messages. I know from both of these similar questions on the topic (linked below) that I can add the VM argument -Dorg.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS=true to disable Tomcat's default deflate compression.

However, I plan to make a program that will be distributed so others can run it, and having to force people to remember to always include a specific VM argument just to change a setting seems quite abrasive.

Is there some alternative way to disable Tomcat's websocket compression which doesn't require users to specify VM arguments at runtime, perhaps using Spring's Java configuration or a custom websocket handshake interceptor?

CodePudding user response:

You can not only set properties using JVM arguments but also programmatically using System.setProperty like the following:

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

If you export your project to a JAR-file using embedded tomcat, you can run it in your main before executing SpringApplication.run:

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

If you package your application to a WAR file, you could try the following:

@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);
    } 
} 
  • Related