如何在struts2中禁止缓存? [英] How to disallow caching in struts2?

查看:515
本文介绍了如何在struts2中禁止缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,当用户退出时,他不应该访问他以前在登录时查看的页面。但是,由于浏览器缓存,他可以在单击后退按钮时查看这些页面。

In my web application, when the user logs out, he should not have access to pages he's previously viewed while he was logged in. However, due to browser caching, he can view those pages when clicked on the back button.

我定义了一个Interceptor来处理这个问题:

I defined an Interceptor to handle this:

public String intercept(ActionInvocation invocation) throws Exception {
        // TODO Auto-generated method stub

        final ActionContext context = invocation.getInvocationContext();
        HttpServletResponse response = (HttpServletResponse)context.get(StrutsStatics.HTTP_RESPONSE);
        if(response!=null){
            response.setHeader("Cache-control", "no-cache, no-store");
            response.setHeader("Pragme", "no-cache");
            response.setHeader("Expires", "-1");

        }

        return invocation.invoke();
    }

struts.xml

   <interceptors>

   <interceptor name="cachingHeadersInterceptor" class="com.prosonsulto.interceptor.CachingHeadersInterceptor"/>
   <interceptor-stack name="defaultSecurityStack">
   <interceptor-ref name="defaultStack"/>
   <interceptor-ref name="cachingHeqadersInterceptor"/>
   </interceptor-stack>

   </interceptors>

在添加此内容后,我运行应用程序时出现404错误。

What happens is, after adding this, I get a 404 error when I run my application.

我尝试在页面中添加回复标题:

I tried adding the response headers in the pages:

<%response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", -1);
response.setHeader("Pragma", "no-cache");
%>

但是必须逐个将它添加到所有页面,这将是一件非常繁琐的工作。此外,用户可以随时重新提交表单,并且无需实际输入登录凭据即可再次访问这些页面。

But it's going to be tedious to have to add it to all the pages one by one. Plus, the user could always do a form re-submission and have access to those pages again without actually typing in his login credentials.

我应该做些什么来做好预防浏览器缓存?

What should I be ideally doing to prevent browser caching?

推荐答案

>

from

response.setHeader("Pragme", "no-cache");

response.setHeader("Pragma", "no-cache");

因为您的代码中有错误,所以在修复错误后无法应用此拦截器可以用它来防止缓存。

Because you have errors in your code you can't apply this interceptor, after you fix that errors you can use it to prevent caching.

这篇关于如何在struts2中禁止缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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