Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting 'relaxedQueryChars' for embedded Tomcat

How can I set relaxedQueryChars for Spring Boot embedded Tomcat?

The connector attribute described here, but Spring Boot documentation has no such parameter listed.

How to set Tomcat's Connector attributes in general?

like image 953
Alex Karasev Avatar asked Aug 06 '18 08:08

Alex Karasev


People also ask

Can we configure embedded tomcat server in spring boot?

We can start Spring boot applications in an embedded tomcat container that comes with some pre-configured default behavior via a properties file. In this post, we will learn to modify the default tomcat configurations via overriding respective properties in application.

How do I add relaxedQueryChars to my spring boot?

To allow illegal or invalid chars in a request in Spring Boot, we need to set the “relaxedQueryChars” config. We can do that in two ways: Defining the ConfigurableServletWebServerFactory bean in the configuration. Adding the environment variable to the property file.

Is spring boot embedded tomcat production ready?

As the titles says, is the Tomcat that spring boot can embed production ready? Or is it only for development purposes? Yes. It's the same tomcat, but in library form.


2 Answers

I am not sure if you can do this with properties file. I believe this should work

@Component
public class MyTomcatWebServerCustomizer
        implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setAttribute("relaxedQueryChars", "yourvaluehere");
            }
        });
    }
}
like image 175
pvpkiran Avatar answered Sep 22 '22 06:09

pvpkiran


If you are using Spring Boot 2.x then you need to use WebSeerverFactoryCustomizer as given below.

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> 
    containerCustomizer(){
    return new EmbeddedTomcatCustomizer();
}

private static class EmbeddedTomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            connector.setAttribute("relaxedPathChars", "<>[\\]^`{|}");
            connector.setAttribute("relaxedQueryChars", "<>[\\]^`{|}");
        });
    }
}
like image 20
Sunil Chauraha Avatar answered Sep 20 '22 06:09

Sunil Chauraha