springBoot encountered The valid characters are defined in RFC 7230 and RFC 3986 solutions

The error codes are as follows:

Error parsing HTTP request header
 Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
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.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1045)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
    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)

Reasons for error reporting:

Next, let's take a look at how RFC 3986 is standardized

RFC3986 document stipulates that Url can only contain English letters (a-zA-Z), numbers (0-9), -~ 4 special characters and all reserved characters. RFC3986 document makes detailed suggestions on Url encoding and decoding, points out which characters need to be encoded so as not to cause the change of Url semantics, and explains why these characters need to be encoded.

There is no corresponding printable character in the US-ASCII character set: only printable characters are allowed in Url. All 10-7F bytes in US-ASCII code represent control characters, which cannot appear directly in Url. At the same time, 80-FF bytes (ISO-8859-1) cannot be placed in the Url because it has exceeded the byte range defined by US-ACII.

Reserved characters: Url can be divided into several components, such as protocol, host, path, etc. Some characters (: /? #[]@) are used to separate different components. For example, colon is used to separate protocol and host, / is used to separate host and path,? Used to separate paths and query parameters, and so on. Some characters (! $& '() * +,; =) are used to separate each component. For example, = is used to represent key value pairs in query parameters, and the & symbol is used to separate multiple key value pairs in query. When the ordinary data in the component contains these special characters, they need to be encoded.

RFC3986 specifies the following characters as reserved characters:! * ' ( ) ; : @ & = + $ , / ? # [ ]  
Unsafe characters: there are some characters that may cause ambiguity in the parser when they are directly placed in the Url. These characters are considered unsafe for many reasons.  
Spaces: in the process of Url transmission, user typesetting, or text processor processing, irrelevant spaces may be introduced or meaningful spaces may be removed.  
Quotation marks and < >: quotation marks and angle brackets are usually used to separate URLs in plain text  
Pound signs (#) are often used to indicate bookmarks or anchors  
%: the percent sign itself is used as a special character for encoding unsafe characters, so it needs to be encoded  
{}| \ ^ [] ` ~: some gateway or transport agent will tamper with these characters

At the same time, RFC 3986 specification has been proposed in Tomcat 7.0.73, and RFC 7230 is also a supplement or improvement to the former, so this problem will occur in Tomcat 7.0.73 and above.

Final solution:

Write a configuration class with the following code:

package com.library.config;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class TomcatConfig {
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
        tomcatServletWebServerFactory.addConnectorCustomizers((Connector connector) -> {
        return tomcatServletWebServerFactory;

Tags: Java Spring Boot Tomcat

Posted on Thu, 14 Oct 2021 14:34:05 -0400 by dandare