<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          相信我,不記錄核心接口的參數(shù)和響應(yīng),你會(huì)后悔的

          共 14273字,需瀏覽 29分鐘

           ·

          2021-04-29 12:43

          點(diǎn)擊上方藍(lán)色字體,選擇“設(shè)為星標(biāo)”

          回復(fù)”學(xué)習(xí)資料“獲取學(xué)習(xí)寶典

          作者:fredalxin
          地址:https://fredal.xin/http-body-recorder

          經(jīng)常會(huì)遇到需要處理 http 請(qǐng)求以及響應(yīng) body 的場(chǎng)景。

          而這里比較大的一個(gè)問(wèn)題是 servle t的 requestBody 或 responseBody 流一旦被讀取了就無(wú)法二次讀取了。

          針對(duì)這個(gè)問(wèn)題,Spring 本身提供了解決方案,即:

          我們編寫一個(gè)過(guò)濾器:

          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();

          }

          這樣自定義一個(gè)filter繼承HttpBodyRecorderFilter,重寫recordBody方法就能自定義自己的處理邏輯了。

          另外,recordCode方法可用于定義在請(qǐng)求響應(yīng)碼為多少的時(shí)候才會(huì)去記錄body,例如可以定義為只有遇到400或500時(shí)才記錄body,用于錯(cuò)誤偵測(cè)。

          過(guò)濾器的匹配規(guī)則比較簡(jiǎn)單,如果想要像springmvc那樣進(jìn)行匹配,我們可以使用:AntPathMatcher。Spring Boot 教程推薦看這個(gè):https://github.com/javastacks/spring-boot-best-practice

          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,從而實(shí)現(xiàn)像springmvc那樣的ant style的匹配。例如對(duì)于以下接口:

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

           //do something

          }
          可以設(shè)置urlPattern為/test/{id:[0-9]+}。


          后臺(tái)回復(fù) 學(xué)習(xí)資料 領(lǐng)取學(xué)習(xí)視頻


          如有收獲,點(diǎn)個(gè)在看,誠(chéng)摯感謝


          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产卡一卡二在线 | 五月丁香网色 | 俺去也WWW在线视频 | 亚洲做爱在线 | 天天综合婷婷 |