Problem Description
The spring boot request url path has a special character error: java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
Cause Analysis:
The problem is that the higher version of tomcat will filter the request header.
The new feature in the high version of tomcat: it is to strictly follow the RFC 3986 specification for access analysis, and the RFC 3986 specification defines that only English letters (a-zA-Z), numbers (0-9), -_.~ are allowed in Url 4 special characters and all reserved characters (RFC3986 specifies the following characters as reserved characters: ! * ' ( ) ; : @ & = + $ , / ? # [ ] )
springboot integrates tomcat by default, and tomcat will report an error when your front-end sends a request and there are reserved special characters in the request parameters
solution:
Create a new TomcatConfig class in config
@Configuration
public class TomcatConfig {
@Bean
public TomcatServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((Connector connector) -> {
connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
});
return factory;
}
}