SpringBoot报错 Error parsing HTTP request header

使用SpringToolSuite4执行Stringboot插入操作时,报请求头错误,具体信息如下

  1. Error parsing HTTP request header
  2. Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level
  3. java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
  4. org.apache.coyote.http11.AbstractHttp11Processor process
  5. 信息: Error parsing HTTP request header
  6. Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
  7. java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
  8. at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)
  9. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
  10. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
  11. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1524)
  12. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1480)
  13. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  14. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  15. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  16. at java.lang.Thread.run(Thread.java:745)

大致意思是,我的请求信息里包含了非法字符,我的参数如下:显而易见,包含了“#{}”非法字符

原因找到了,那我们就来解决它

上网查了一些资料,解决方法有很多,比较容易理解的2种方案

1、降低Tomcat版本:不推荐

2、增加Tomcat配置类:可以插入\"#<>[\\]^`{|}中的任意特殊字符,当然也可以自定义

  1. package com.xkcoding.rbac.security.config;
  2. import org.apache.catalina.connector.Connector;
  3. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class TomcatConfig {
  8. @Bean
  9. public TomcatServletWebServerFactory webServerFactory() {
  10. TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
  11. factory.addConnectorCustomizers((Connector connector) -> {
  12. connector.setProperty("relaxedPathChars", "\"#<>[\\]^`{|}");
  13. connector.setProperty("relaxedQueryChars", "\"#<>[\\]^`{|}");
  14. });
  15. return factory;
  16. }
  17. }

此时,问题已解决,且测试成功

(0)

相关推荐