當前位置:首頁 > 教育 > 正文

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. }

此時,問題已解決,且測試成功

你可能想看:

有話要說...

取消
掃碼支持 支付碼