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?
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.
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.
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.
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");
}
});
}
}
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", "<>[\\]^`{|}");
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With