【发布时间】:2017-09-11 15:30:59
【问题描述】:
如何在 Spring 4.2 或更高版本中使用 applicationContext.xml 设置 Cache-control: private?
背景:
Cache-control: HTTP 标头可以从 Spring 4.1 中的 applicationContext.xml 设置,如下所示:
<mvc:interceptors>
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptors>
有一些基于注释的实现,例如 https://github.com/foo4u/spring-mvc-cache-control ,但我更喜欢基于 XML 的配置,因为我必须根据测试/生产环境更改 HTTP 标头(例如,如果页面返回,Chrome 会发送另一个“查看页面源”请求与Cache-Control: private, no-store, no-cache, must-revalidate,并使反CSRF令牌不匹配)。
问题:
这些设置自 Spring 4.2 起已弃用。
此外,无法从这些设置中设置Cache-control: private。因为当且仅当 http 标头包含 Cache-Control: private 时,某些 CDN 提供程序才不会存储内容,因此对此 HTTP 标头的支持对于使用 CDN 的系统至关重要。例如http://tech.mercari.com/entry/2017/06/22/204500 或 https://community.fastly.com/t/fastly-ttl/882 。
所以为了安全起见,我正在寻找设置 Cache-Control: private HTTP header from applicationContext.xml 的方法。
【问题讨论】:
标签: java xml spring spring-mvc caching