什么是在部署中设置Cache-Control no-cache,no-store?

时间:2012-11-14 17:56:06

标签: java spring jboss primefaces spring-webflow

我遇到一个问题,我的应用程序部署总是返回响应标头:

Cache-Control: no-cache
Cache-Control: no-store
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:no-cache



我正在使用:

Spring 3.1.2.RELEASE

Primefaces JSF 3.4.1

Spring Webflow 2.3.0.RELEASE

JBoss AS 7.0.1



我已经尝试了几乎我能找到的应用程序方面的所有解决方案:

  1. 配置WebContentInterceptor(尝试了各种排列)Right out of the box cache-control header filter?
  2. 编写自定义拦截器,添加不同的Cache-Control标头(使用Cache-Control:private测试)
  3. 编写添加HTTP响应参数的客户过滤器。使用Cache-Control配置它:private作为web.xml中的init-params
  4. 使用context.xml文件(在META-INF /和WEB-INF /中都尝试过)来禁用JBoss / Tomcat中的Cache-Control http://daveharris.wordpress.com/2007/07/09/how-to-configure-cache-control-in-tomcat/
  5. 在所有上述情况中,响应头从未结束不同,总是没有缓存,没有存储,1970年到期,pragma:no-cache

    我的想法已经用完了,有没有人知道我的响应中设置这些标头是什么,所以我可以定位相应的部署组件来解决这个问题?

1 个答案:

答案 0 :(得分:5)

导致此问题的根代码是在Spring MVC中,从WebContentGenerator调用。此类用作MVC / Webflow堆栈中几个类的基类:WebContentInterceptor(MVC拦截器),AbstractController(MVC控制器),AbstractHandlerMethodAdapter(MVC HandlerAdapter),AnnotationMethodHadlerAdapter(MVC HandlerAdapter),FlowHandlerAdapter(Webflow HandlerAdapter),JsfFlowHandlerAdapter(Webflow + JSF HandlerAdapter)

CacheControl seconds设置为0会调用preventCaching方法。因此,应用程序似乎默认设置为0。

org.springframework.web.servlet.support.WebContentGenerator

protected final void preventCaching(HttpServletResponse response) {
    response.setHeader(HEADER_PRAGMA, "no-cache");
    if (this.useExpiresHeader) {
        // HTTP 1.0 header
        response.setDateHeader(HEADER_EXPIRES, 1L);
    }
    if (this.useCacheControlHeader) {
        // HTTP 1.1 header: "no-cache" is the standard value,
        // "no-store" is necessary to prevent caching on FireFox.
        response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
        if (this.useCacheControlNoStore) {
            response.addHeader(HEADER_CACHE_CONTROL, "no-store");
        }
    }
}

我发现由于我使用的是JSF + Webflow,因此JsfFlowHandlerAdapter首先处理对流/视图的服务器请求。这就是为什么配置拦截器没有帮助的原因,因为JsfFlowHandlerAdapter此时已经设置了Cache-Control和其他HTTP头。事实证明我已经扩展了JsfFlowHandlerAdapter以处理FlowExecutionRestorationFailureException(请参阅Sping Web Flow Preventing Back Button Use)所以我需要做的就是设置我想要的配置ala WebContentInterceptor(因为配置属于基类WebContentGenerator)。

自定义JsfFlowHandlerAdapter

public class MyAppFlowHandlerAdapter extends org.springframework.faces.webflow.JsfFlowHandlerAdapter {
     ...
    }

webmvc-config.xml中

<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
    <bean
        class="com.myapp.MyAppFlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
            <!-- Disable built in Cache-Control settings -->
        <property name="cacheSeconds" value="-1" />
        <property name="useExpiresHeader" value="false" />
        <property name="useCacheControlHeader" value="false" />
        <property name="useCacheControlNoStore" value="false" />
    </bean>

<!-- Maps request paths to flows in the flowRegistry; e.g. a path of /hotels/booking 
    looks for a flow with id "hotels/booking" -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <!-- snip out unimportant -->
    <property name="interceptors">
        <list>
            <ref bean="cacheControlInterceptor" />  
        </list>
    </property>
</bean>
    <bean id="cacheControlInterceptor"
    class="com.myapp.CacheControlInterceptor">

CacheControlInterceptor(设置自己的HTTP标头。在WebContentGenerator中执行此操作的方法是最终的,所以不能@Override)

public class CacheControlInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //Example below: set your Cache-Control, expires, pragma headers here
        response.setHeader("Cache-Control", "private");

        return true;
    }
}