如何在过滤器中获取过滤器httpservletresponse输出错误消息

时间:2017-08-06 05:02:21

标签: servlets spring-security

我正在使用spring 4和过滤器我在servletResponse中得到了一些错误响应。它包含带有错误消息的outputstream。 如何在Outputstream中获取此错误消息并审核到我的应用程序?

try{            
            filterChain.doFilter(wrappedRequest, servletResponse);
            HttpServletResponse  httpServletResponse = (HttpServletResponse) servletResponse;
            LOGGER.info("Basic Authenticate HttpServletResponse code {}"+httpServletResponse.getStatus());

            httpServletResponse.getOutputStream();
// I need to read this outputstream, its contains error message.?




    }catch(Exception e){
        LOGGER.error("Error Basic Authenticate Error :{}", e);
        HttpServletResponse  httpServletResponse = (HttpServletResponse) servletResponse;
        AtomicInteger statusCode = new AtomicInteger(httpServletResponse.getStatus());
        Response response = ResponseUtil.populateResponse(statusCode, e.getMessage().toString(), 0);
        httpServletResponse.sendError(httpServletResponse.getStatus(), e.getMessage());
        auditService.updateRestAuditRecord(auditId,StringUtils.EMPTY, response);
    }

1 个答案:

答案 0 :(得分:0)

关键是HttpServletResponseWrapper装饰器类。根据您的特定需求调整这些步骤:

  1. 将要审核的流完全读取为字节缓冲区。
  2. 执行审核任务。
  3. 扩展HttpServletResponseWrapper,覆盖getOutputStream()并返回流式传输字节缓冲区的ServletOutputStream扩展名。
  4. HttpServletResponseWrapper传递给链中的下一个下游过滤器。