使用SpringToolSuite4執行Stringboot插入操作時,報請求頭錯誤,具體信息如下
- Error parsing HTTP request header
- Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level
- java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
- org.apache.coyote.http11.AbstractHttp11Processor process
- 信息: Error parsing HTTP request header
- Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
- 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.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)
- at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017)
- at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
- at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1524)
- at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1480)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
- at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
- at java.lang.Thread.run(Thread.java:745)
大緻意思是,我的請求信息裡包含了非法字符,我的參數如下:顯而易見,包含了“#{}”非法字符
原因找到了,那我們就來解決它
上網查了一些資料,解決方法有很多,比較容易理解的2種方案
1、降低Tomcat版本:不推薦
2、增加Tomcat配置類:可以插入\"#<>[\\]^`{|}中的任意特殊字符,當然也可以自定義
- package com.xkcoding.rbac.security.config;
- 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;
- @Configuration
- public class TomcatConfig {
- @Bean
- public TomcatServletWebServerFactory webServerFactory() {
- TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
- factory.addConnectorCustomizers((Connector connector) -> {
- connector.setProperty("relaxedPathChars", "\"#<>[\\]^`{|}");
- connector.setProperty("relaxedQueryChars", "\"#<>[\\]^`{|}");
- });
- return factory;
- }
- }
此時,問題已解決,且測試成功
有話要說...