Invalid character found in the request target in spring boot

邮差的信 提交于 2019-11-28 09:46:16

According to https://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html, requestTargetAllow is deprecated. For me, the other solutions presented here did not work either. According to the Tomcat documentation I found a way to set the property relaxedQueryChars instead:

@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            connector.setProperty("relaxedQueryChars", "|{}[]");
        }
    });
    return factory;
}

you will start your Spring boot app like this

$ java -jar -Dtomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
 demo-0.0.1-SNAPSHOT.jar

or encode uri like this

document.location = restUrl + "/print?reportParams= " + encodeURI(JSON.stringify(jsonData));

Easy way just add this code in your main class

System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "{}");

Priya Nalang

For SpringBoot 1.5.17.RELEASE. The below code worked for me.

@Configuration
public class ServerConfig {

    @Bean
    public EmbeddedServletContainerFactory webServerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedQueryChars", "|{}[]");
            }
        });
        return factory;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!