Invalid character found in the request target in spring boot

偶尔善良 提交于 2019-11-27 03:10:38

问题


My application is started with java -jar with version 1.5.6.RELEASE of spring boot.
The content of one of my request has the character "{".When it is sended to server the following exception is raised:

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:472) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:683) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

Where is wrong? How do I fix it?

EDIT1:
My rest is like this:

var jsonData = {
                    id: $("#hiddenId").val(),
                    permitNumber: $("#txtPermitNumber").val(),
                    permitToDate: $("#txtPermitToDate").val()
               }
document.location = restUrl + "/print?reportParams= " + JSON.stringify(jsonData);

回答1:


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;
}



回答2:


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));



回答3:


Easy way just add this code in your main class

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




回答4:


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;
    }

}


来源:https://stackoverflow.com/questions/46251131/invalid-character-found-in-the-request-target-in-spring-boot

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