Invalid character found in the request target in spring boot
Asked Answered
L

9

36

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);
Laurettalaurette answered 16/9, 2017 at 7:3 Comment(0)
O
40

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;
}
Olympus answered 31/7, 2018 at 7:55 Comment(3)
where I can find this configure in eclipse? @Matthias LohrJustle
Not sure what you mean with "find". This is code for a Java Bean. There's nothing Eclipse related here.Olympus
Thank you, this config works great for meAdlare
T
20

For a Spring Boot application, just add to properties file:

server.tomcat.relaxed-query-chars=|,{,},[,]

There is also the following key: server.tomcat.relaxed-path-chars

Telemotor answered 3/11, 2020 at 16:50 Comment(0)
E
11

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));
Erdah answered 16/9, 2017 at 16:17 Comment(2)
dahanetoon service :DEnvelope
Does not work for me, seems to be ignored with Spring Boot 2.x.Olympus
G
7

if you use application.yaml just add following config

server:
  tomcat:
    relaxed-query-chars: '|,{,},[,]'
Gusta answered 18/11, 2021 at 12:2 Comment(0)
D
1

Easy way just add this code in your main class

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

Downfall answered 30/7, 2018 at 8:49 Comment(1)
Does (also) not work for me, seems to be ignored with Spring Boot 2.x.Olympus
M
1

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

}
Macario answered 30/11, 2018 at 10:43 Comment(2)
Better if the user also gives the reason for down vote.Macario
I did not vote to the post. I suspect, it did not work by the voter, what could have happened because he is using spring boot 2. But don't worry on it, the upvote is 5 times stronger than the down. If your solution will work by me, I will vote it up.Lymphocyte
S
1

For a Spring Boot application, have this in properties file:

server.tomcat.additional-tld-skip-patterns=xercesImpl.jar,xml-apis.jar,serializer.jar,xml-apis.jar,jaxb-core.jar,jaxb-api.jar

Scarcity answered 4/1, 2021 at 12:26 Comment(0)
P
-1

Encode it to Base 64 string at request end and decode it at the controller end.


Encode

btoa(string)


Decode:

String arr = request.getParameter();
arr = new String(Base64.getDecoder().decode(arr))
Plumbago answered 1/7, 2020 at 15:21 Comment(0)
G
-1

I faced the same problem and for me the problem was solved by replacing @GetMapping by @PostMapping.

Guinn answered 17/1, 2023 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.