使用spring-boot阻止缓存index.html文件

时间:2015-07-29 22:55:39

标签: spring-boot

我正在使用spring-boot并且想要阻止index.html的缓存但是缓存所有其他资源,我已经将资源文件放在我的类路径上并使用以下内容阻止了缓存。

目前我正在执行以下操作,即缓存所有文件。

@Configuration
public class StaticResourceConfig extends WebMvcConfigurerAdapter {

    private static final int SEVEN_DAYS_IN_SECONDS = 604800;

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:frontend/dist/")
                .setCachePeriod(SEVEN_DAYS_IN_SECONDS);
        super.addResourceHandlers(registry);
    }

}

index.html文件位于frontend / dist / index.html

4 个答案:

答案 0 :(得分:1)

我设法这样做了:

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {

   registry.addResourceHandler("/index.html")
            .addResourceLocations("classpath:frontend/dist/index.html")
            .setCachePeriod(0);

   registry.addResourceHandler("/assets/**")
            .addResourceLocations("classpath:frontend/dist/assets")
            .setCachePeriod(SEVEN_DAYS_IN_SECONDS);

    super.addResourceHandlers(registry);
}

答案 1 :(得分:0)

您可以使用MappedInterceptorWebContentInterceptor作为将Cache-Control标头配置到不同静态资源的更灵活的解决方案。

    @Bean
    public MappedInterceptor cacheControlInterceptor() {
        WebContentInterceptor webContentInterceptor = new WebContentInterceptor();
        webContentInterceptor.setCacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS).cachePublic());
        webContentInterceptor.addCacheMapping(CacheControl.noStore().mustRevalidate(), "/index.html");
        // if using Spring Security CacheControlHeadersWriter:
        // webContentInterceptor.addCacheMapping(CacheControl.empty(), "/", "/index.html");
        return new MappedInterceptor(null, webContentInterceptor);
    }

为什么需要bean?请参阅Note

另请参阅:SPR-10655SPR-13780(这可能很奇怪,因为即使Spring Security CacheControlHeadersWriter也使用4个指令链,例如“no-cache,no-store,max-age = 0,must -revalidate“)

答案 2 :(得分:0)

用jax解决方案很好,但是请注意不要使用extend WebMvcConfigurationSupport,否则它将不起作用,并且使用spring boot可以扩展配置,因此不需要第二部分。这种和平的代码有效:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SinglePageApplicationResourceHandler implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/index.html")
                .addResourceLocations("classpath:/public/index.html")
                .setCachePeriod(0);
    }
}

通常,您需要像这样捕获所有映射来扩展它:

@Controller
public class SinglePageApplicationController {

  @RequestMapping("/**/{path:[^.]*}")
  public String redirect() {
    return "forward:/index.html";
  }
}

答案 3 :(得分:0)

使用Spring Boot 2.1.1和附加的Spring Security 5.1.1。

1。对于在代码中使用resourcehandler的资源(未测试):

您可以通过这种方式添加自定义资源扩展。

registry.addResourceHandler

用于将uri路径添加到获取资源的位置

.addResourceLocations

用于设置文件系统中资源所在的位置( 给定的是一个具有classpath的亲戚

.setCacheControl
.setCachePeriod

用于设置缓存头(说明)。

Resourcechain和resolver是可选的(在这种情况下,与默认值完全相同)。

@Configuration
public class CustomWebMVCConfig implements WebMvcConfigurer {

private static final int SEVEN_DAYS_IN_SECONDS = 604800;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) 
    registry.setOrder(1).addResourceHandler("/index.html")
            .addResourceLocations("classpath:frontend/dist/")
            .setCacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS)
                    .mustRevalidate())
            .setCacheControl(CacheControl.noCache())
            .setCacheControl(CacheControl.noStore())
            .resourceChain(true)
            .addResolver(new PathResourceResolver());

    registry.setOrder(0).addResourceHandler("/**")
            .addResourceLocations("classpath:frontend/dist/")
            .setCachePeriod(SEVEN_DAYS_IN_SECONDS)
            .resourceChain(true)
            .addResolver(new PathResourceResolver());
}

2。在控制器级别

(yourwebsite.com/index.html)

@GetMapping("/index.html")
public void getIndex(HttpServletResponse response) {
    response.setHeader(HttpHeaders.CACHE_CONTROL,
             "no-cache, no-store, max-age=0, must-revalidate");
}