从 ContentCachingResponseWrapper 中什么都得不到

问题描述 投票:0回答:0

我有一个用于登录我的应用程序的过滤器。现在,我实现了第二个过滤器来将数据存储在 S3 中。问题是我只能从 ContentCachingResponseWrapper 读取响应,只有我禁用了第一个过滤器。两个过滤器一起不能正常工作。

我的日志过滤器:

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

    HttpServletRequest requestToUse = request;

    if (shouldLog(request)) {
        requestToUse = new CachedBodyHttpServletRequest(request);
        final RequestUri RequestUri = new RequestUri(request);
        logger.info(message(requestToUse, RequestUri));
    }

    HttpServletResponse responseToUse = response;

    if (isNotCached(response) && shouldLog(request)) {
        responseToUse = new ContentCachingResponseWrapper(response);
    }

    try {
        filterChain.doFilter(requestToUse, responseToUse);
    } finally {
        if (shouldLog(requestToUse)) {
            final RequestUri RequestUri = new RequestUri(requestToUse);
            logger.info(message(requestToUse, responseToUse, RequestUri));
            ((ContentCachingResponseWrapper) responseToUse).copyBodyToResponse();
        }
    }
}

我的过滤器存储数据和必要的方法:

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

    HttpServletRequest requestToUse = request;

    RequestUri RequestUri = new RequestUri(request);
    if (this.shouldLog(request) && requestToUse.getRequestURI().endsWith(DATA_XML)) {
        requestToUse = new CachedBodyHttpServletRequest(request);
        SourceMessage sourceMessage = createMessageRequest(RequestUri,
            requestToUse);
        String s3Key = generateKey(sourceMessage);
        sendToS3(s3Key, sourceMessage);
    }

    HttpServletResponse responseToUse = new ContentCachingResponseWrapper(response);
    
    // tried already to paste the final block here
    
    try {
        filterChain.doFilter(requestToUse, responseToUse);
    } finally {
        if (this.shouldLog(requestToUse) && requestToUse.getRequestURI().endsWith(DATA_XML)) {
            SourceMessage sourceMessage = createMessageResponse(RequestUri,
                requestToUse, responseToUse);
            String s3Key = generateKey(sourceMessage);
            sendToS3(s3Key, sourceMessage);
            ((ContentCachingResponseWrapper) responseToUse).copyBodyToResponse();
        }
    }
}

SourceMessage createMessageRequest(final HttpServletResponse response) {

        return SourceMessage.builder()
            .logId(logIdProvider.getLogId())
            .xml(xml(response))
            .build();
}

private String xml(final HttpServletResponse response) {
    ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response,
        ContentCachingResponseWrapper.class);

    return wrapper == null ? N_A : this.xml(wrapper.getContentAsByteArray(), response.getCharacterEncoding());
}

如果我单独使用过滤器存储,它工作正常,两个过滤器一起我在 xml 方法中得到 N_A。我已经尝试更改班级级别的过滤器顺序。

任何想法的

谢谢

spring-boot servlet-filters
© www.soinside.com 2019 - 2024. All rights reserved.