【发布时间】:2014-11-30 17:49:52
【问题描述】:
从question 可以看出,spring security 为 spring boot 管理缓存。从 spring boot documentation 它显示了如何使用以下方法为资源设置缓存:
spring.resources.cache-period= # cache timeouts in headers sent to browser
cache-period 非常适合 Spring Boot 的所有预定义静态位置(即/css**、/js/**、/images/**),但我还生成了一个manifest.appcache 用于离线下载我的静态资产和由于上述所有 spring security/boot 使用 manifest.appcache 发回缓存头
"method": "GET",
"path": "/manifest.appcache",
"response": {
"X-Application-Context": "application:local,flyway,oracle,kerberos:8080",
"Expires": "Tue, 06 Oct 2015 16:59:39 GMT",
"Cache-Control": "max-age=31556926, must-revalidate",
"status": "304"
}
我想知道如何为manifest.appcache 添加排除项。无论我的标头如何,IE 和 Chrome 似乎都对 appcache “做正确的事情”,但 FF 似乎在注意到 appcache 何时发生更改时更加奇特,我认为我的缓存标头搞砸了。
编辑: 我应该从WebMvcAutoConfiguration 的源代码中添加它,它显示了如何为资源设置缓存,我只是不确定如何为我的 1 个案例选择性地禁用,而不会破坏 Spring Boot 在此文件中设置的其余部分.
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod);
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**")
.addResourceLocations(RESOURCE_LOCATIONS)
.setCachePeriod(cachePeriod);
}
}
【问题讨论】:
-
只需添加一个扩展
WebMvcConfigurerAdapter的类并为该资源添加特定规则即可。 -
我试过这个:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { super.addResourceHandlers(registry); registry.addResourceHandler("/manifest.appcache").addResourceLocations("/").setCachePeriod(0); }但我得到了 404。 -
使用
/将使它尝试从Web 应用程序的根目录检索。确保 / 覆盖文件的正确物理位置。 -
我的 manifest.appcache 位于应用的根目录中(在本例中为
/public,与 index.html 所在的位置相同。 -
那么应该是
/public而不是/。
标签: spring-boot html5-appcache