如何防止在Spring MVC中缓存页面?

时间:2015-09-14 09:10:09

标签: jsp spring-mvc caching servlet-filters

考虑一下:

在IE 9中

enter image description here

要清除缓存 ,我做了:

  
      
  1. 打开IE,按F12,然后按Ctrl + R

  2.   
  3. 弹出“您确定要清除浏览器缓存”弹出

  4.   
  5. 选择是。

  6.   

过滤

@WebFilter("*.*")
public class NoCacheFilter
    implements Filter {

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain)
        throws IOException,
            ServletException {

        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Cache-Control",
            "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0); // Proxies.

        System.out.println("Hello World!");

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

        // TODO Auto-generated method stub

    }

}

Java配置:

public class SpittrWebAppInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected String[] getServletMappings() {

        /* Map DispatcherServlet to /, handles, all the requests coming into the web app. */

        return new String[] {"/"};

    }

    @Override
    protected Class<?>[] getRootConfigClasses() {

        return new Class<?>[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {

        /* Specify configuration data */
        return new Class<?>[] {WebConfig.class};
    }

    @Override
    protected Filter[] getServletFilters() {

        return new Filter[] {new NoCacheFilter()};
    }

}

Spring Security:

@Configuration
@EnableWebSecurity
public class SecurityConfig
    extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.formLogin().and().authorizeRequests().antMatchers("/spitter/")
            .authenticated().antMatchers(HttpMethod.GET, "/spitter/register")
            .authenticated().and().logout().logoutSuccessUrl("/login");

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {

        auth.inMemoryAuthentication().withUser("user").password("password")
            .roles("USER").and().withUser("admin").password("password")
            .roles("USER", "ADMIN");
    }

}

登录&amp;注销 两者都运行良好。

当我按下后退按钮时,即使我清除了缓存,我也可以看到缓存页面。

如果我没错, 响应标题 设置正确,

即使退出后,我仍然可以通过按下浏览器后退按钮来查看页面。

有什么建议吗?

0 个答案:

没有答案