欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

怎么記錄http請求/響應(yīng)數(shù)據(jù)

本篇內(nèi)容介紹了“怎么記錄 http 請求/ 響應(yīng)數(shù)據(jù)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站開發(fā),網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:18982081108

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

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

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

  • ContentCachingRequestWrapper

  • ContentCachingResponseWrapper。

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

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方法可用于定義在請求響應(yīng)碼為多少的時(shí)候才會去記錄body,例如可以定義為只有遇到400或500時(shí)才記錄body,用于錯(cuò)誤偵測。

過濾器的匹配規(guī)則比較簡單,如果想要像springmvc那樣進(jìn)行匹配,我們可以使用:AntPathMatcher。

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的匹配。例如對于以下接口:

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

 //do something

}

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

“怎么記錄 http 請求/ 響應(yīng)數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)頁題目:怎么記錄http請求/響應(yīng)數(shù)據(jù)
標(biāo)題URL:http://www.aaarwkj.com/article24/pcceje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)定制開發(fā)、網(wǎng)站設(shè)計(jì)公司企業(yè)建站、品牌網(wǎng)站制作動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
亚洲av一本岛在线播放| 黑人巨大一区二区三区| 人妻少妇亚洲中文字幕| 亚洲人妻av一区二区| 日韩欧美一区二区麻豆| 91精品国产综合久蜜臂| 亚洲天堂成人av蜜臀| 97久久精品亚洲中文字幕| 婷婷激情五月国产丝袜| 乱色熟女一区二区三区| 男人的天堂av东京热一区| 天天操夜夜夜夜夜操| 国产精品一区2区3区| 日本黄色三级三级三级| 日韩精品熟女中文字幕| 免费黄色日韩在线观看| 国产成人精品高清国产三级| 天堂av日韩在线播放| 一级黄片视频免费下载| 亚洲欧美一区二区粉嫩| 少妇高潮特黄在线观看| 亚洲精品精品一区二区| 日本一二不卡高清在线视频| 日韩精品在线免费观看了| 日本韩国三级伦理在线观看| 中文字幕精品高清中国| 国产女技师口爆在线观看| 日本一区二区三区伦理| 丰满人妻的诱惑中文字幕| 色婷婷亚洲婷婷亚洲最大| 在线视频天堂亚洲天堂| 国产爆操美女在线观看| 国产毛片一区二区在线| 国产探花猛操性感美女| 国产伦理自拍视频在线观看| 色婷婷国产精品久久包臀| 亚洲av精二区三区四区| av免费在线观看网页| 日麻批视频在线免费观看| 日本丰满熟女毛茸茸的黑逼| 日本在线中文字幕乱码|