最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Why is my response already committed in my lowest precedence filter in spring when using ContentCachingResponseWrapper? -

programmeradmin0浏览0评论

I have a OncePerRequestFilter:

@Component
public class RestrictedSettingsFilter extends OncePerRequestFilter {
  @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
        try {
            filterChain.doFilter(request, responseWrapper);
            if (responseWrapper.isCommitted()) {
                log.warn("The response has already been committed, cannot modify it");
                return;
            }
            // ...
        } finally { responseWrapper.copyBodyToResponse(); }
}

and I manually register it (probably should be done by annotations, but the project is a bit old):

@Bean
public FilterRegistrationBean<Filter> restrictedSettingsFilter(RestrictedSettingsFilter restrictedSettingsFilter) {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(restrictedSettingsFilter);
        filterRegistrationBean.setOrder(Ordered.LOWEST_PRECEDENCE); 
        return filterRegistrationBean;
}

From what I understand, when I set its order to LOWEST_PRECEDENCE, it should be resolved last in pre-processing and first in post-processing. Therefore, the response should not be committed yet. But I get The response has already been committed, cannot modify it message. On the other hand, when I set the order to HIGHEST_PRECEDENCE, it started working again. How is it possible?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论