【问题标题】:do not log unhandled exception in tomcat不要在 tomcat 中记录未处理的异常
【发布时间】:2015-12-21 04:59:40
【问题描述】:

我们有一个 tomcat webapp,它碰巧在一个正常的无效登录尝试期间在疯狂的 Spring SAML 内部深处抛出了一些可怕的 SAML 身份验证异常。异常未处理,用户被重定向到一个半有用的错误页面,使用

   <error-page>
      <exception-type>com.test.SomeException</exception-type>
      <location>/error.jsp</location>
   </error-page>

它工作正常,除了 tomcat 仍然在 catalina.out 中记录未处理的异常。是否有一些简单的方法可以防止它使用 strack 跟踪(被日志监控软件等拾取)污染日志文件而无需重组所有内容?无论如何,处理这个问题的正确方法是什么?

【问题讨论】:

  • 您是否在使用外部日志框架,例如 log4j?
  • 是的,log4j2。但是用它来过滤东西感觉很糟糕。

标签: java tomcat logging exception-handling spring-saml


【解决方案1】:

你当然可以在 Spring 中处理异常,你不需要让它们冒泡到 web.xml 中的处理程序。请参阅this article 中的一些示例。

【讨论】:

  • 但这不是只适用于 MVC 控制器中发生的异常吗?在我的情况下,在 SAMLContextProviderImpl 的自定义实现中抛出异常(当我们找不到有效的 partnerId 时)。
【解决方案2】:

所以这就是我现在所达到的,并不理想,但有点工作:

   @Override
   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      try {
         doFilterInternal(servletRequest, servletResponse, filterChain);
      } catch (MyVerySpecificSamlAuthException ex) {
         HttpServletRequest request = (HttpServletRequest)servletRequest;
         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/general_error.jsp");
         request.setAttribute(RequestDispatcher.ERROR_MESSAGE, ex.getMessage());
         requestDispatcher.forward(request, servletResponse);
      }
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2013-12-15
    • 1970-01-01
    • 2020-09-01
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多