Error: Error parsing HTTP request header

Problem description: During joint debugging of the front-end and back-end, the front-end page 404 cannot be displayed, and the back-end reports an error Error parsing HTTP request header

The error message shows:

Cause Analysis:

  • Tip: The above problem is caused by tomcat comparing URL parameters since the 8.5.x series: 8.5.12 and later versions, the 8.0.x series: 8.0.42 and later versions, and the 7.0.x series: 7.0.76 and later versions. Specification restrictions must follow RFC 7230 and RFC 3986 specifications. Non-reserved characters (request parameters in json format) must be escaped, otherwise Invalid character found in the request target will be thrown. The valid characters are defined in RFC 7230 and RFC 3986 error messages.

    It is to strictly follow the RFC 3986 specification for access analysis, and the RFC 3986 specification defines that Url is only allowed to contain English letters (a-zA-Z), numbers (0-9), -_.~4 special characters and all reserved characters. (RFC3986 specifies the following characters as reserved characters: ! * ' ( ) ; : @ & = + $ , / ? # [ ]).

solution:

  • Lower the tomcat version (not recommended) and change the tomcat version to below tomcat8.5
  • Add a tomcat configuration to the SpringBoot project. The configuration file code is as follows
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;
 
/**
 * 解决springboot项目请求出现非法字符问题
 */
@Configuration
public class TomcatConfig {
 
    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((Connector connector) -> {
            connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
            connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
        });
        return factory;
    }
}

The problem is roughly that field string parameters in json format are often used during front-end and back-end interactions, which contain special symbols such as "{}" and "[]". In higher versions of tomcat, requests containing these characters will be intercepted.

Guess you like

Origin blog.csdn.net/m0_62869063/article/details/134535857