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

    • ContentCachingRequestWrapper
    • ContentCachingResponseWrapper

    编写一个过滤器:

    1. public abstract class HttpBodyRecorderFilter extends OncePerRequestFilter {
    2. private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 1024 * 512;
    3. private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
    4. @Override
    5. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
    6. throws ServletException, IOException {
    7. boolean isFirstRequest = !isAsyncDispatch(request);
    8. HttpServletRequest requestToUse = request;
    9. if (isFirstRequest && !(request instanceof ContentCachingRequestWrapper)
    10. && (request.getMethod().equals(HttpMethod.PUT.name())
    11. || request.getMethod().equals(HttpMethod.POST.name()))) {
    12. requestToUse = new ContentCachingRequestWrapper(request);
    13. }
    14. HttpServletResponse responseToUse = response;
    15. if (!(response instanceof ContentCachingResponseWrapper) && (request.getMethod().equals(HttpMethod.PUT.name())
    16. || request.getMethod().equals(HttpMethod.POST.name()))) {
    17. responseToUse = new ContentCachingResponseWrapper(response);
    18. }
    19. boolean hasException = false;
    20. try {
    21. filterChain.doFilter(requestToUse, responseToUse);
    22. } catch (final Exception e) {
    23. hasException = true;
    24. throw e;
    25. } finally {
    26. int code = hasException ? 500 : response.getStatus();
    27. if (!isAsyncStarted(requestToUse) && (this.codeMatched(code, AdvancedHunterConfigManager.recordCode()))) {
    28. recordBody(createRequest(requestToUse), createResponse(responseToUse));
    29. } else {
    30. writeResponseBack(responseToUse);
    31. }
    32. }
    33. }
    34. protected String createRequest(HttpServletRequest request) {
    35. String payload = "";
    36. ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
    37. if (wrapper != null) {
    38. byte[] buf = wrapper.getContentAsByteArray();
    39. payload = genPayload(payload, buf, wrapper.getCharacterEncoding());
    40. }
    41. return payload;
    42. }
    43. protected String createResponse(HttpServletResponse resp) {
    44. String response = "";
    45. ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(resp, ContentCachingResponseWrapper.class);
    46. if (wrapper != null) {
    47. byte[] buf = wrapper.getContentAsByteArray();
    48. try {
    49. wrapper.copyBodyToResponse();
    50. } catch (IOException e) {
    51. e.printStackTrace();
    52. }
    53. response = genPayload(response, buf, wrapper.getCharacterEncoding());
    54. }
    55. return response;
    56. }
    57. protected void writeResponseBack(HttpServletResponse resp) {
    58. ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(resp, ContentCachingResponseWrapper.class);
    59. if (wrapper != null) {
    60. try {
    61. wrapper.copyBodyToResponse();
    62. } catch (IOException e) {
    63. LOG.error("Fail to write response body back", e);
    64. }
    65. }
    66. }
    67. private String genPayload(String payload, byte[] buf, String characterEncoding) {
    68. if (buf.length > 0 && buf.length < getMaxPayloadLength()) {
    69. try {
    70. payload = new String(buf, 0, buf.length, characterEncoding);
    71. } catch (UnsupportedEncodingException ex) {
    72. payload = "[unknown]";
    73. }
    74. }
    75. return payload;
    76. }
    77. public int getMaxPayloadLength() {
    78. return maxPayloadLength;
    79. }
    80. private boolean codeMatched(int responseStatus, String statusCode) {
    81. if (statusCode.matches("^[0-9,]*$")) {
    82. String[] filteredCode = statusCode.split(",");
    83. return Stream.of(filteredCode).map(Integer::parseInt).collect(Collectors.toList()).contains(responseStatus);
    84. } else {
    85. return false;
    86. }
    87. }
    88. protected abstract void recordBody(String payload, String response);
    89. protected abstract String recordCode();
    90. }

    这样自定义一个filter继承HttpBodyRecorderFilter,重写recordBody方法就能自定义自己的处理逻辑了。
    另外,recordCode方法可用于定义在请求响应码为多少的时候才会去记录body,例如可以定义为只有遇到400或500时才记录body,用于错误侦测。
    过滤器的匹配规则比较简单,如果想要像SpringMVC那样进行匹配,可以使用:AntPathMatcher

    1. class PatternMappingFilterProxy implements Filter {
    2. private final Filter delegate;
    3. private final List<String> pathUrlPatterns = new ArrayList();
    4. private PathMatcher pathMatcher;
    5. public PatternMappingFilterProxy(Filter delegate, String... urlPatterns) {
    6. Assert.notNull(delegate, "A delegate Filter is required");
    7. this.delegate = delegate;
    8. int length = urlPatterns.length;
    9. pathMatcher = new AntPathMatcher();
    10. for (int index = 0; index < length; ++index) {
    11. String urlPattern = urlPatterns[index];
    12. this.pathUrlPatterns.add(urlPattern);
    13. }
    14. }
    15. @Override
    16. public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
    17. throws IOException, ServletException {
    18. HttpServletRequest httpRequest = (HttpServletRequest) request;
    19. String path = httpRequest.getRequestURI();
    20. if (this.matches(path)) {
    21. this.delegate.doFilter(request, response, filterChain);
    22. } else {
    23. filterChain.doFilter(request, response);
    24. }
    25. }
    26. private boolean matches(String requestPath) {
    27. for (String pattern : pathUrlPatterns) {
    28. if (pathMatcher.match(pattern, requestPath)) {
    29. return true;
    30. }
    31. }
    32. return false;
    33. }
    34. @Override
    35. public void init(FilterConfig filterConfig) throws ServletException {
    36. this.delegate.init(filterConfig);
    37. }
    38. @Override
    39. public void destroy() {
    40. this.delegate.destroy();
    41. }
    42. public List<String> getPathUrlPatterns() {
    43. return pathUrlPatterns;
    44. }
    45. public void setPathUrlPatterns(List<String> urlPatterns) {
    46. pathUrlPatterns.clear();
    47. pathUrlPatterns.addAll(urlPatterns);
    48. }
    49. }

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

    1. @PostMapping("/test/{id}")
    2. public Object test(@PathVariable(value = "id",required = true) final Integer index) {
    3. //do something
    4. }

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