当我尝试在 URL 中发送特殊字符时,Tomcat 返回 HTTP 状态 400 – 错误请求状态

标签 tomcat urlencode tomcat9 http-status-code-400

每当我尝试在 Mozilla Firefox 浏览器中将特殊字符放入 URL 中时

https://URL/|

https://URL/]

Tomcat 正在返回 HTTP 状态 400 – 错误请求 和异常堆栈跟踪如下。

Type Exception Report

Message Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Exception

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.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:474)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

Note The full stack trace of the root cause is available in the server logs.

令人惊讶的是,带有特殊字符的相同 URL 在 Google Chrome 浏览器中可以正常工作。

我有以下问题

  1. 我应该在 tomcat 配置文件中做哪些更改才能使其不返回 400 状态代码和异常堆栈跟踪?
  2. 为什么 Mozilla Firefox 无法对 URL 进行编码?

注意:我已经在部署在 tomcat 9 上的应用程序中为类型 4XX 的状态代码配置了一个公共(public)页面,但问题是 tomcat 首先返回 400 错误请求 http 状态代码作为响应。

最佳答案

选项 1 - 允许字符:

如果需要允许字符,请将 relaxedQueryChars 和/或 relaxedPathChars 属性添加到 server.xml 文件中的 Connector 元素.像这样的东西:

<Connector port="8080" protocol="HTTP/1.1"
           relaxedQueryChars="[]{}^|"
           relaxedPathChars="[]{}^|"
           URIEncoding="UTF-8"
           connectionTimeout="20000"
           redirectPort="8443"/>

这些属性的文档在这里:https://tomcat.apache.org/tomcat-9.0-doc/config/http.html#Standard_Implementation

选项 2 - 不错的错误页面:

如果你想禁止这些字符,但显示一个漂亮的错误页面而不是堆栈跟踪,请将错误报告阀添加到 server.xml 中的 Host 元素。像这样的东西:

<Valve className="org.apache.catalina.valves.ErrorReportValve"
       errorCode.400="webapps/ROOT/server_error.html"/>

Valve 的文档在这里:https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Error_Report_Valve

关于当我尝试在 URL 中发送特殊字符时,Tomcat 返回 HTTP 状态 400 – 错误请求状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52810433/

相关文章:

spring - 简单的工作 Spring MVC/Maven 配置

python - 如何将 urlencode 更改为 python 字典

javascript - 如何使用javascript解码URL中带空格的参数?

php - url 编码的正斜杠破坏了我的 codeigniter 应用程序

java - Maven,继承pom模块

maven - 如何使用 CDI 和 JAX-RS、Java EE7 配置 Tomcat 9?

ruby-on-rails - 连接 Tomcat、Solr 和 sunspot_rails 之间的点

web-services - Tomcat 是否支持 TLS v1.2?

java - 我正在尝试部署 helloworld Web 服务,但运行 URL 后出现错误

java - 当我启动我的 Tomcat 时,不是打开欢迎文件,而是下载它。有人可以帮我从这里出去吗。?