首页 > 解决方案 > 当url包含特殊字符时如何重定向错误页面?

问题描述

我有一个关于设置tomcat的问题。

我想在发生错误时显示常见错误页面。这是我的客户安全需求。

但是,如果我访问 www.mydomain.com/..%5c,我的常见错误页面将无法正常工作。

他们显示“HTTP ERROR 400 消息”。我想重定向我的常见错误页面..

这是我的 web.xml 配置。

<error-page>
  <error-code>400</error-code>
  <location>/error.html</location>
</error-page>

<error-page>
  <error-code>401</error-code>
  <location>/error.html</location>
</error-page>

<error-page>
  <error-code>403</error-code>
  <location>/error.html</location>
</error-page>

<error-page>
  <error-code>404</error-code>
  <location>/error.html</location>
</error-page>

<error-page>
  <error-code>405</error-code>
  <location>/error.html</location>
</error-page>

<error-page>
  <error-code>500</error-code>
  <location>/error.html</location>
</error-page>

我添加了 CATALINA_OPTS。-Dorg.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

此选项适用于 www.mydomain.com/%5c 但不适用于 www.mydomain.com/..%5c

访问 www.mydomain.com/..%5c 时如何重定向常见错误页面

标签: apachesecuritytomcat

解决方案


你可以试试这个程序。

我认为您可以在 web.xml 中配置您的过滤器。在过滤器中,您可以实现处理请求编码和特殊字符的逻辑,然后重定向到错误页面。

配置

<filter>
      <filter-name>name</filter-name>
      <filter-class>demoClass</filter-class>
  </filter>


  <filter-mapping>
      <filter-name>name</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

要实现的类

    public class demoClass implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("----init----");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        //TODO code 
    }

    @Override
    public void destroy() {
        System.out.println("----destory----");
    }
}

我的英语很差


推荐阅读