解决 Tomcat 报错 java.lang.IllegalArgumentException: Invalid character found in method name.
Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:414)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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)
1. 原因
随着 Tomcat 版本的升高, 会检验 URL, 当浏览器访问 URL 时, 若其中带有特殊字符, 如花括号冒号点等等.
2. 解决
2.1 添加配置 (推荐)
在
conf/catalina.properties
中添加下面内容.
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
在
conf/server.xml
中的<Connector>
节点中, 添加 2 个属性:
relaxedPathChars="|{}[],"
relaxedQueryChars="|{}[],"
重启 Tomcat.
2.2 其他方法
- 去除 URL 中的特殊字符.
- 使用 Post 方法提交数据.
- 降低 Tomcat 版本规避此问题.