【内有源码】如何在框架层面上自动记录 http 请求和响应的数据?

经常会遇到需要处理 http 请求以及响应 body 的场景。而这里比较大的一个问题是 servlet 的 requestBody 或 responseBody 流一旦被读取了。就无法二次读取了。针对这个问题,spring 本身提供了解决方案,即 ContentCachingRequestWrapper/ContentCachingResponseWrapper。

我们编写一个过滤器:

public abstract class HttpBodyRecorderFilter extends OncePerRequestFilter {

    private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 1024 * 512;

    private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;

    @Override

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        boolean isFirstRequest = !isAsyncDispatch(request);

        HttpServletRequest requestToUse = request;

        if (isFirstRequest

                && !(request instanceof ContentCachingRequestWrapper)

                && (request.getMethod().equals(HttpMethod.PUT.name())
                        || request.getMethod().equals(HttpMethod.POST.name()))) {

            requestToUse = new ContentCachingRequestWrapper(request);

        }

        HttpServletResponse responseToUse = response;

        if (!(response instanceof ContentCachingResponseWrapper)

                && (request.getMethod().equals(HttpMethod.PUT.name())
                        || request.getMethod().equals(HttpMethod.POST.name()))) {

            responseToUse = new ContentCachingResponseWrapper(response);

        }

        boolean hasException = false;

        try {

            filterChain.doFilter(requestToUse, responseToUse);

        } catch (final Exception e) {

            hasException = true;

            throw e;

        } finally {

            int code = hasException ? 500 : response.getStatus();

            if (!isAsyncStarted(requestToUse)

                    && (this.codeMatched(code, AdvancedHunterConfigManager.recordCode()))) {

                recordBody(createRequest(requestToUse), createResponse(responseToUse));

            } else {

                writeResponseBack(responseToUse);

            }

        }

    }

    protected String createRequest(HttpServletRequest request) {

        String payload = "";

        ContentCachingRequestWrapper wrapper =

                WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);

        if (wrapper != null) {

            byte[] buf = wrapper.getContentAsByteArray();

            payload = genPayload(payload, buf, wrapper.getCharacterEncoding());

        }

        return payload;

    }

    protected String createResponse(HttpServletResponse resp) {

        String response = "";

        ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(resp, ContentCachingResponseWrapper.class);

        if (wrapper != null) {

            byte[] buf = wrapper.getContentAsByteArray();

            try {

                wrapper.copyBodyToResponse();

            } catch (IOException e) {

                e.printStackTrace();

            }

            response = genPayload(response, buf, wrapper.getCharacterEncoding());

        }

        return response;

    }

    protected void writeResponseBack(HttpServletResponse resp) {

        ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(resp, ContentCachingResponseWrapper.class);

        if (wrapper != null) {

            try {

                wrapper.copyBodyToResponse();

            } catch (IOException e) {

                LOG.error("Fail to write response body back", e);

            }

        }

    }

    private String genPayload(String payload, byte[] buf, String characterEncoding) {

        if (buf.length > 0 && buf.length < getMaxPayloadLength()) {

            try {

                payload = new String(buf, 0, buf.length, characterEncoding);

            } catch (UnsupportedEncodingException ex) {

                payload = "[unknown]";

            }

        }

        return payload;

    }

    public int getMaxPayloadLength() {

        return maxPayloadLength;

    }

    private boolean codeMatched(int responseStatus, String statusCode) {

        if (statusCode.matches("^[0-9,]*$")) {

            String[] filteredCode = statusCode.split(",");

            return Stream.of(filteredCode).map(Integer::parseInt).collect(Collectors.toList()).contains(responseStatus);

        } else {

            return false;

        }

    }

    protected abstract void recordBody(String payload, String response);

    protected abstract String recordCode();

}

这样自定义一个 filter 继承 HttpBodyRecorderFilter,重写 recordBody 方法就能自定义自己的处理逻辑了。另外,recordCode 方法可用于定义在请求响应码为多少的时候才会去记录 body,例如可以定义为只有遇到 400 或 500 时才记录 body,用于错误侦测。

过滤器的匹配规则比较简单,如果想要像 springmvc 那样进行匹配,我们可以使用https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html。

class PatternMappingFilterProxy implements Filter {

    private final Filter delegate;

    private final List<String> pathUrlPatterns = new ArrayList();

    private PathMatcher pathMatcher;

    public PatternMappingFilterProxy(Filter delegate, String... urlPatterns) {

        Assert.notNull(delegate, "A delegate Filter is required");

        this.delegate = delegate;

        int length = urlPatterns.length;

        pathMatcher = new AntPathMatcher();

        for (int index = 0; index < length; ++index) {

            String urlPattern = urlPatterns[index];

            this.pathUrlPatterns.add(urlPattern);

        }

    }

    @Override

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;

        String path = httpRequest.getRequestURI();

        if (this.matches(path)) {

            this.delegate.doFilter(request, response, filterChain);

        } else {

            filterChain.doFilter(request, response);

        }

    }

    private boolean matches(String requestPath) {

        for (String pattern : pathUrlPatterns) {

            if (pathMatcher.match(pattern, requestPath)) {

                return true;

            }

        }

        return false;

    }

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

        this.delegate.init(filterConfig);

    }

    @Override

    public void destroy() {

        this.delegate.destroy();

    }

    public List<String> getPathUrlPatterns() {

        return pathUrlPatterns;

    }

    public void setPathUrlPatterns(List<String> urlPatterns) {

        pathUrlPatterns.clear();

        pathUrlPatterns.addAll(urlPatterns);

    }

}

这样子,PatternMappingFilterProxy 装饰了真正的 HttpBodyRecorderFilter,支持传入 urlPatterns,从而实现像 springmvc 那样的 ant style 的匹配。例如对于以下接口:


 @PostMapping("/test/{id}")

 public Object test(@PathVariable(value =  "id",required =  true)  final Integer index)  {

 //do something

 }

可以设置 urlPattern 为/test/{id:[0-9]+}

以上代码存在于 https://github.com/fredalxin/httpBodyRecorder


作者:fredalxin

来源链接:

https://fredal.xin/http-body-recorder

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值