URI 中的斜杠 (/) 给出来自 Tomcat 的 HTTP 400 Bad request 错误

Nav*_*ath 3 tomcat utf-8 bad-request

我使用的是 Tomcat 7.0.54.0。

我的应用程序中有一个使用该GET方法的 REST API。在此 API 的 URI 中,有一个斜杠字符 ( /),我在调用 API 之前对其进行编码。那个例子:http://192.168.168.168:38080/myApp/getDetails/test%2Fstring

URI 参数中的实际字符串是test/string。在调用 API 之前,我使用URLEncoder.encode("test/string", "UTF-8"),这给了我test%2Fstring. 与我在 URL 中使用的相同。

此 API 调用在一种环境中工作正常,但在其他环境中会出现错误HTTP 400 (Bad request)

两台服务器的Java版本、Tomcat版本、操作系统环境(Linux)和版本都相同。在 中server.xml,我像这样配置了连接器端口:

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="38080" protocol="HTTP/1.1" redirectPort="8443"/>
Run Code Online (Sandbox Code Playgroud)

另外,还有一个字符编码过滤器,配置如下代码:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
    HttpServletRequest httpRequest = null;
    HttpServletResponse httpResponse = null;

    if (request instanceof HttpServletRequest)
    {
        httpRequest = (HttpServletRequest) request;
    }

    if (response instanceof HttpServletResponse)
    {
        httpResponse = (HttpServletResponse) response;
    }

    // it means its not http request
    if (httpRequest == null || httpResponse == null)
    {
        chain.doFilter(request, response);
        return;
    }

    httpRequest.setCharacterEncoding("UTF-8");
    httpResponse.setCharacterEncoding("UTF-8");
    chain.doFilter(request, response);
}
Run Code Online (Sandbox Code Playgroud)

当我调试时,我发现请求甚至没有到达这个过滤器。这意味着服务器本身正在限制该 URL。

这个 API 以前工作得很好,但最近开始出现这个问题。唯一的区别是我采用了相同版本的新 Tomcat,并以与现有 Tomcat 完全相同的方式对其进行配置。

谁能解释发生了什么以及如何解决它?

Nav*_*ath 5

我解决了这个问题。我被要求-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true-Dorg.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true我的JAVA_OPTS.

请参阅以下内容了解更多详情:

配置 Tomcat 以在 URL 中编码斜杠