在 WebFlux 中禁用 HTTP 缓存

cto*_*mek 2 http-caching spring-boot spring-webflux

Spring Boot MVC应用程序中,我以这种方式禁用 HTTP 缓存:

WebContentInterceptor cacheInterceptor = new WebContentInterceptor();
cacheInterceptor.setCacheSeconds(0);
cacheInterceptor.setUseExpiresHeader(true);
cacheInterceptor.setUseCacheControlHeader(true);
cacheInterceptor.setUseCacheControlNoStore(true);
registry.addInterceptor(cacheInterceptor);
Run Code Online (Sandbox Code Playgroud)

如何在Spring Boot WebFlux应用程序中做到这一点?

Bri*_*zel 6

如果您正在使用 Spring Boot 并且希望防止缓存静态资源,则可以使用以下配置属性来实现:

spring.resources.cache.cachecontrol.no-store=true
Run Code Online (Sandbox Code Playgroud)

如果您想禁用所有内容的缓存,包括 REST 调用和视图等;然后你可以实现一个自定义WebFilter,并在你的应用程序中将它作为一个 bean 公开:

class NoStoreWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        exchange.getResponse().getHeaders()
                .setCacheControl(CacheControl.noStore().getHeaderValue());
        return chain.filter(exchange);
    }
}
Run Code Online (Sandbox Code Playgroud)