在我的部署中设置Cache-Control无缓存,无存储的设置是什么? [英] What is setting Cache-Control no-cache, no-store in my deployment?

查看:219
本文介绍了在我的部署中设置Cache-Control无缓存,无存储的设置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的应用程序部署总是返回带有以下内容的响应标头:

I have an issue that my application deployment always returns response headers with:

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



我正在使用:



I'm using:

春季3.1.2发行

Primefaces JSF 3.4.1

Primefaces JSF 3.4.1

Spring Webflow 2.3.0.RELEASE

Spring Webflow 2.3.0.RELEASE

JBoss AS 7.0.1

JBoss AS 7.0.1



我在应用程序方面尝试了几乎所有可以找到的解决方案:



I've tried nearly every solution on the application side I could find:

  1. 配置WebContentInterceptor(尝试了各种排列)右开箱即用的缓存控制标头过滤器?
  2. 编写一个自定义拦截器,以添加不同的Cache-Control标头(已通过Cache-Control:私有测试)
  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/
  1. Configuring the WebContentInterceptor (tried various permutations of it) Right out of the box cache-control header filter?
  2. Writing a custom interceptor that adds different Cache-Control header (tested with Cache-Control: private)
  3. Writing a customer filter that adds HTTP response parameters. Configure it with Cache-Control: private as init-params in web.xml
  4. Use the context.xml file (tried both in META-INF/ and WEB-INF/) to disable the Cache-Control in JBoss/Tomcat http://daveharris.wordpress.com/2007/07/09/how-to-configure-cache-control-in-tomcat/

在上述所有情况下,响应头都不会以不同的结尾出现,总是没有缓存,没有存储,1970年到期,杂注:没有缓存

In all of the above cases, the response headers never ended up different, always no-cache, no-store, 1970 expires, pragma: no-cache

我的想法不多了,有人知道我的响应中设置了这些标头是什么,这样我就可以针对适当的部署组件来解决此问题?

I'm running out of ideas, does anyone know what is setting these headers in my response so I can target the appropriate deployment component to resolve this?

推荐答案

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

The root code causing this is in Spring MVC, called from the WebContentGenerator. This class is used as base class for several classes in the MVC/Webflow stack: WebContentInterceptor (MVC interceptor), AbstractController (MVC controller), AbstractHandlerMethodAdapter (MVC HandlerAdapter), AnnotationMethodHadlerAdapter (MVC HandlerAdapter), FlowHandlerAdapter (Webflow HandlerAdapter), JsfFlowHandlerAdapter (Webflow + JSF HandlerAdapter)

将CacheControl秒设置为0会调用preventCaching方法.因此,似乎该应用程序默认设置为0.

The CacheControl seconds setting of 0 calls the preventCaching method. So it seems the application is defaulting to a setting of 0.

org.springframework.web.servlet.support.WebContentGenerator

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 Prevention使用后退按钮),因此我要做的就是设置想要的配置

I found out that since I am using JSF + Webflow, the JsfFlowHandlerAdapter is handling the server requests for the flows/views first. This is why configuring interceptors does not help because the JsfFlowHandlerAdapter has already set the Cache-Control and other HTTP Headers at this point. It turns out I had already extended the JsfFlowHandlerAdapter to handle FlowExecutionRestorationFailureException (see Sping Web Flow Preventing Back Button Use) so all I needed to do was set the configuration I wanted ala WebContentInterceptor (since the configurations belong to the base class WebContentGenerator).

自定义JsfFlowHandlerAdapter

Custom JsfFlowHandlerAdapter

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

webmvc-config.xml

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)

CacheControlInterceptor (to set your own HTTP Headers. The methods that do it in WebContentGenerator are final so cannot @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;
    }
}

这篇关于在我的部署中设置Cache-Control无缓存,无存储的设置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆