java 如何让 Tomcat 接受 URL 中未转义的括号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44011774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make Tomcat accept unescaped brackets in URLs?
提问by cbnz
I've never used Tomcat before, but I've recently inherited a JSP project and now I need to get it running. I've managed to install Tomcat 8.0 locally in Eclipse and everything works fine. I've also installed Tomcat 8.0 on an Ubuntu VPS. The app is running fine, except for a small issue with how it handles URLs.
我以前从未使用过 Tomcat,但我最近继承了一个 JSP 项目,现在我需要让它运行。我已经成功地在 Eclipse 中本地安装了 Tomcat 8.0,一切正常。我还在 Ubuntu VPS 上安装了 Tomcat 8.0。该应用程序运行良好,除了它如何处理 URL 的一个小问题。
The client-side application produces URLs with unescaped square and curly brackets in parameters, like this:
客户端应用程序生成参数中带有未转义方括号和大括号的 URL,如下所示:
GET /saveItems.json?items=[{%22json%22:%22here%22}]
As much as I would like to change the client-side app, I can't. I just need to get this backend running.
尽管我很想更改客户端应用程序,但我不能。我只需要让这个后端运行。
My local copy of the application handles this fine. On the server, however, I get this error:
我的应用程序的本地副本可以很好地处理这个问题。但是,在服务器上,我收到此错误:
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:286)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1504)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1460)
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:748)
I've been looking for a setting that might affect this, without any luck. What am I missing here?
我一直在寻找可能会影响这一点的设置,但没有任何运气。我在这里错过了什么?
回答by Latif
I make this workable by editing $CATALINA_HOME\conf\server.xml
我通过编辑 $CATALINA_HOME\conf\server.xml 使其可行
Old Value:<Connector ... protocol="HTTP/1.1"... />
旧值:<Connector ... protocol="HTTP/1.1"... />
New Value:<Connector ... protocol="HTTP/1.1"... relaxedQueryChars='[]|{}^\`"<>' />
新值:<Connector ... protocol="HTTP/1.1"... relaxedQueryChars='[]|{}^\`"<>' />
回答by Beck Yang
Tomcat 8.0.39/8.5.9/7.0.73
add additional checks for valid characters to the HTTP request line parsing.
According to this requirement, version 8.0.42/8.5.12/7.0.76
make some changes to allow some invalid characters.{
, }
and |
are allowed if the character present in value of system property
tomcat.util.http.parser.HttpParser.requestTargetAllow
.
Tomcat 8.0.39/8.5.9/7.0.73
向 HTTP 请求行解析添加对有效字符的额外检查。根据此要求,version 8.0.42/8.5.12/7.0.76
进行一些更改以允许一些无效字符。{
,如果字符出现在系统属性的值中}
,|
则允许
tomcat.util.http.parser.HttpParser.requestTargetAllow
。
You can add system property tomcat.util.http.parser.HttpParser.requestTargetAllow={}
to prevent this error. One of the soltion is editing $CATALINA_HOME\conf\catalina.properties
.
您可以添加系统属性tomcat.util.http.parser.HttpParser.requestTargetAllow={}
来防止此错误。解决方法之一是编辑$CATALINA_HOME\conf\catalina.properties
.
回答by pravin patil
I get this error when I try to GET URL
with some special character.
当我尝试GET URL
使用某些特殊字符时出现此错误。
java.lang.IllegalArgumentException: Invalid character found in the request target.
java.lang.IllegalArgumentException: 在请求目标中发现无效字符。
By changing the connector parameter in server.xml
,
we are able to parse a special character in Tomcat version 9.
通过更改 中的连接器参数server.xml
,我们能够解析 Tomcat 版本 9 中的特殊字符。
Connector port="80" protocol="HTTP/1.1" relaxedQueryChars="\ { } |"
回答by Leroy
I was experiencing a similar problem, but using Spring boot too, the following solution resolved my issue:
我遇到了类似的问题,但也使用 Spring boot,以下解决方案解决了我的问题:
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setProperty("relaxedQueryChars", "|{}[]");
}
});
return factory;
}
Credited to Matthias-lohr- https://stackoverflow.com/questions/46251131/invalid-character-found-in-the-request-target-in-spring-boot
归功于Matthias-lohr- https://stackoverflow.com/questions/46251131/invalid-character-found-in-the-request-target-in-spring-boot