当前位置: > 热文

SpringBoot报错 Error parsing HTTP request header

时间:2022-04-22 08:55:31 热文 我要投稿

使用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;

}

}

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