tomcat 处理请求url带特殊字符

tomcat版本:7.0.93

报错信息如下:

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.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:194)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1050)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:317)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

具体分析如下:

          tomcat添加了对header请求头的验证,因请求路径中带有[]{}等字符,所以无法通过校验。

解决方案:

1、去除URL中的特殊字符;

2、使用 Post 方法提交数据

3、更换低版本的Tomcat来规避这种问题。

4、在 conf/catalina.properties  添加或者修改: 

tomcat.util.http.parser.HttpParser.requestTargetAllow=|{} 

该配置只对以上三种特定字符有效

5、在 conf/server.xml  <Connector> 节点下添加: 

relaxedPathChars="\"<>[\\]^`{|}"
relaxedQueryChars="\"<>[\\]^`{|}"

如下:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               relaxedPathChars="\"<>[\\]^`{|}"  
               relaxedQueryChars="\"<>[\\]^`{|}" />

如果是springboot项目则将webServerFactory方法加入到springboot启动类

@Configuration
public class TomcatConfig {

    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((Connector connector) -> {
                connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
                connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
        });
        return factory;
    }
}

经测试之后,该问题解决!

 

ps :如何解决tomcat乱码问题

在Connector节点下添加属性 URIEncoding="UTF-8"即可。

 

参考: https://blog.csdn.net/lebron3v/article/details/92593478

 

posted @ 2020-03-30 13:29  jet-angle  阅读(5538)  评论(0编辑  收藏  举报