Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: enable HTTP TRACE method for embedded Tomcat

Standalone Tomcat allows you to enable TRACE HTTP method through allowTrace attribute:

allowTrace - A boolean value which can be used to enable or disable the TRACE HTTP method. If not specified, this attribute is set to false.

If I have to do that same for a Spring Boot project using embedded Tomcat - what kind of config/properties setting I can use for that?

I have looked for the properties supported by Spring Boot for Tomcat server:

properties supported by spring boot for tomcat server

but it seems to be not listed. Any thoughts how to achieve this.

like image 960
samshers Avatar asked Jan 28 '23 19:01

samshers


2 Answers

You can configure Connector.allowTrace property programmatically. In this case you have to define bean for class EmbeddedServletContainerFactory and add connector customizer by calling TomcatEmbeddedServletContainerFactory.addConnectorCustomizers(...) method. It allows you to access Connector object and call any configuration method you need. In this case we simply call connector.setAllowTrace(true):

import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatConfiguration {

    @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();

        factory.addConnectorCustomizers(connector -> {
            connector.setAllowTrace(true);
        });
        return factory;
    }
}

You can configure this bean in a separate configuration class (like in the example above) or you can simply add this bean method to your main Spring Boot application class.

Couldn't it be done with server.tomcat.* like property?

At this moment - nope. Current Spring Boot version (1.5.9-RELEASE) does not allow to set it up with a simple property. All properties with server.tomcat prefix are mapped automatically to class org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat. If you take a look at its javadocs (or source code in your IDE) you will see that there is no method like setAllowTrace(boolean value) or something like that.

like image 137
Szymon Stepniak Avatar answered Feb 23 '23 21:02

Szymon Stepniak


The solution above only works for Spring Boot 1. For Spring Boot 2 the following works:

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
        return customizer -> customizer.addConnectorCustomizers(connector -> {
            connector.setAllowTrace(true);
        });
    }

If you want to apply if on the managemant port you need to create a configuration class that looks something like this:

@ManagementContextConfiguration
public class ManagementInterfaceConfiguration {

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
        return customizer -> customizer.addConnectorCustomizers(connector -> {
            connector.setAllowTrace(true);
        });
    }

}

and a resource (=on the classpath) in META-INF/spring.factories that picks it up:

org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\
com.package.ManagementInterfaceConfiguration
like image 20
Markus T Avatar answered Feb 23 '23 20:02

Markus T