为SpringBoot应用添加可以配置的拦截器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
@ConditionalOnExpression("${cache.response.enabled:false}")
public class FilterRegistrationBeanConfigration {
@Bean
public FilterRegistrationBean filterRegistrationBean() {
return new FilterRegistrationBean() {{
setFilter(new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
HttpServletResponse responseToUse = new ContentCachingResponseWrapper(response);
filterChain.doFilter(request, responseToUse);
((ContentCachingResponseWrapper) responseToUse).copyBodyToResponse();
}
});
}};
}
}
1
cache.response.enabled=true