2014-10-06 70 views
4

从此question它显示弹簧安全管理缓存用于弹簧启动。从春天启动documentation它展示了如何使用设置资源高速缓存:如何选择性地禁用弹簧启动缓存(manifest.appcache)

spring.resources.cache-period= # cache timeouts in headers sent to browser 

cache-period是伟大的春季启动所有预定义的静态位置(即/css**/js/**/images/**),但我也产生manifest.appcache我的静态资产的离线下载,并且由于所有上述弹簧安全的/ 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似乎'app正确的事情'与appcache无关我的标题,但FF似乎是一个更特别的一点在注意什么时候appcache已经改变,我认为我的缓存头正在搞砸了。

编辑: 我应该从源头上增加对WebMvcAutoConfiguration它显示的缓存是如何设置的资源,我只是不知道该如何选择了我1时禁用W/O可能破坏什么春天启动的其余部分在这个文件中设置。

@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); 
     } 
    } 
+1

只需添加扩展'WebMvcConfigurerAdapter'一个类,并添加了该资源应该做的伎俩一个特定的规则。 – 2014-10-06 18:23:59

+0

我试过这个: '''@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { super.addResourceHandlers(registry); registry.addResourceHandler(“/ manifest.appcache”)。addResourceLocations(“/”)。setCachePeriod(0); }''' 但我得到了404。 – JimB 2014-10-08 11:13:39

+0

使用'/'将使它尝试从Web应用程序的根目录检索。确保/覆盖文件的正确物理位置。 – 2014-10-08 17:52:02

回答

2

感谢Q和A!我们遇到了类似的问题:所有浏览器(Chrome,Safari,IE),但是一个(FF)没有缓存清单文件本身,而是在更改后重新加载清单文件。

但是,在Spring Boot中设置缓存周期设置并不能完全解决问题。此外,我不想为Spring Boot应用程序提供的所有文件设置缓存控制参数,但仅限于为清单禁用缓存。

我的解决方案由两个部分组成:

  1. 提供的清单文件中的“转”的评论。虽然未涉及的W3C spec某些浏览器似乎想这样的评论,以检测在清单或引用(缓存)文件的更改:

    # rev 7 
    

    现在我们通过更改缓存清单文件中的“转”参数一个Maven生成的唯一内部版本号:https://github.com/dukecon/dukecon_html5/commit/b60298f0b856a7e54c97620f278982142e3e1f45)。

  2. 通过提供一个过滤器来禁用缓存,该过滤器将一个标题“Cache-Control:no-cache”添加到缓存中。清单文件模式:https://github.com/dukecon/dukecon_server/commit/dc02f26996cb172df804da007546f439df75126d
+0

你的第一点不是改变任何东西的''rev'',浏览器正在查看这些评论并对它进行哈希处理,如果它检测到哈希中发生了变化,它将使缓存和重新加载无效 为你的第二点,这绝对是首选的解决方案,因为它专门针对appcache,并且不会打扰其他缓存时间段,我会将您的答案标记为首选的 – JimB 2015-09-22 14:07:22

3

基于this answer详述IE需要“最大年龄= 1,必须-重新验证”,并通过测试所有的浏览器,设置的

spring.resources.cache-period=1 

一个属性值将允许正确的HTTP标头被写入,允许正确处理appcache清单。这不是我希望的解决方案(能够使用适当的头部来缓存期为0),但它确实使浏览器正常运行并正确使用appcache清单。

再次总结解决方案上下文 - 这是我的应用程序,下载我的所有资产离线(js/css/html)和服务从appcache。

2

在当前版本(2016年2月)中,不需要在代码中执行某些操作来更改默认行为。 只需做你的application.properties一些配置:

# Enable HTML5 application cache manifest rewriting. 
spring.resources.chain.html-application-cache=true 

# Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled. 
spring.resources.chain.enabled=true 
# Enable the content Version Strategy. 
spring.resources.chain.strategy.content.enabled=true 
# Comma-separated list of patterns to apply to the Version Strategy. 
spring.resources.chain.strategy.content.paths=/** 

# Locations of static resources. 
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 

这就是全部。现在Spring会检查你的静态文件是否被修改,并且可以发送更聪明的响应(If-Modiffied-Since和其他),并重写你的appcache。

此外,如果有理由不使用基于内容的版本对一些资源 - 您可以使用备用FixedVersion策略,并设置明确的版本在你的配置:

#Enable the fixed Version Strategy. 
spring.resources.chain.strategy.fixed.enabled=false 
# Comma-separated list of patterns to apply to the Version Strategy. 
spring.resources.chain.strategy.fixed.paths= 
# Version string to use for the Version Strategy. 
spring.resources.chain.strategy.fixed.version= 

附:不要忘记Spring Security:它会重写缓存标头并禁用缓存。

见多docs

+0

不要忘记添加'spring.resources。 cache-period = 3600',时间以秒为单位。 – 2016-08-08 19:22:37