SpringBoot processing request parameters contain special symbols

  • 2021-10-15 10:35:51
  • OfStack

Today, I encountered a problem in writing code. The request parameter is a path "D:/ExcelFile". When I tested with postman, I encountered an error in the following figure

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:491) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.36.jar:9.0.36]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.36.jar:9.0.36]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.36.jar:9.0.36]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]

Baidu gave me the answer: A new and special one has been added to the new version of Tomcat, that is, access parsing is carried out in strict accordance with RFC 3986 specification, and RFC 3986 specification defines that only English letters (a-zA-Z), numbers (0-9), -_. ~ 4 special characters and all reserved characters are allowed in Url:! * '(); : @ & = + $,/? # []).

Solution: Just add @ Bean annotation to the startup class


/* Resolve that the file name contains ":\\" When special characters such as the 400 The question of 
* Tomcat Added to the new version of 1 A new feature is to strictly follow  RFC 3986 Specification for access resolution, while  RFC 3986 The specification defines the Url Only English letters are allowed in ( a-zA-Z ), numbers ( 0-9 ), -_.~4 Special characters 
*  And all reserved characters (RFC3986 The following characters are specified as reserved characters in: ! * ' ( ) ; : @ & = + $ , / ? # [ ]) . */
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory (){
  //  Modify the built-in  tomcat  Container configuration 
  TomcatServletWebServerFactory tomcatServlet = new TomcatServletWebServerFactory();
  tomcatServlet .addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "XXX"));
  return tomcatServlet ;
}

The position of XXX is the position where special symbols are filled in, which needs to be changed to the special symbols in the request parameters, for example: @ # ¥% … & ! # etc


Related articles: