Setting 'relaxedQueryChars' for embedded Tomcat

后端 未结 3 2098
既然无缘
既然无缘 2021-01-17 13:43

How can I set relaxedQueryChars for Spring Boot embedded Tomcat?

The connector attribute described here, but Spring Boot documentation has no such param

相关标签:
3条回答
  • 2021-01-17 14:25

    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");
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-01-17 14:29

    I did this as a working solution for me:

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer(){
        return new MyCustomizer();
    }
    
    private static class MyCustomizer implements EmbeddedServletContainerCustomizer {
    
        @Override
        public void customize(ConfigurableEmbeddedServletContainer factory) {
            if(factory instanceof TomcatEmbeddedServletContainerFactory) {
                customizeTomcat((TomcatEmbeddedServletContainerFactory) factory);
            }
        }
    
        void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
            factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
                connector.setAttribute("relaxedPathChars", "<>[\\]^`{|}");
                connector.setAttribute("relaxedQueryChars", "<>[\\]^`{|}");
            });
        }
    
    }
    
    0 讨论(0)
  • 2021-01-17 14:36

    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", "<>[\\]^`{|}");
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题