当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Boot中启用HTTP响应缓存

公良理
2023-03-14
@Controller
public class MyRestController {
    @RequestMapping(value = "/someUrl", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<String> myMethod(
            HttpServletResponse httpResponse) throws SQLException {
        return new ResponseEntity<String>("{}", HttpStatus.OK);
    }
}
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
    null

共有1个答案

和飞翔
2023-03-14

事实证明,无缓存HTTP头是由Spring Security设置的。在http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#headers中讨论了这一点。

下面的命令禁用HTTP响应标头杂注:no-cache,但不能解决问题:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Prevent the HTTP response header of "Pragma: no-cache".
        http.headers().cacheControl().disable();
    }
}

我最终完全禁用了公共静态资源的Spring Security性,如下所示(在与上面相同的类中):

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/static/public/**");
}
@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter
        implements EmbeddedServletContainerCustomizer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Resources without Spring Security. No cache control response headers.
        registry.addResourceHandler("/static/public/**")
            .addResourceLocations("classpath:/static/public/");

        // Resources controlled by Spring Security, which
        // adds "Cache-Control: must-revalidate".
        registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/")
            .setCachePeriod(3600*24);
    }
}
 类似资料:
  • 问题内容: 我已经使用Spring Boot 1.0.2实现了REST服务器。我无法阻止Spring设置禁用HTTP缓存的HTTP标头。 我的控制器如下: 所有HTTP响应均包含以下标头: 我尝试了以下操作来删除或更改这些标头: 调用控制器。 调用控制器。 定义是回报的,我打过电话。 将属性设置为-1或正值。 以上都不起作用。如何在Spring Boot中为所有或单个请求禁用或更改这些标头? 问题

  • 问题内容: 我想确保浏览器永远不会缓存服务器的响应,这样即使发出两个相同的请求(相隔一纳秒),也始终会与服务器联系。这是实现此目标的正确方法: 谢谢唐 问题答案: 不,那不是正确的方法。这是正确的方法: 您可能会看到其他人在建议其他条目/属性,但是当至少提及上述内容时,它们是完全不相关的。 更改后,请不要忘记在测试之前清除浏览器缓存。 也可以看看: 网站站长缓存教程

  • 我希望能够创建一个自定义的AngularJS服务,当其数据对象为空时发出HTTP“GET”请求,并在成功时填充数据对象。 下一次调用此服务时,我希望绕过再次发出HTTP请求的开销,而是返回缓存的数据对象。 这可能吗?

  • 我希望在spring boot中返回类似以下内容的json响应: 我的RestController如下所示 但我得到的反应是这样的

  • 从我的客户端,我正在从服务器查询一些枚举。现在,我希望在服务器上查询完这些枚举后,立即将其缓存到客户端中。 我尝试设置缓存控制,如下所示: 响应标头似乎已正确设置为缓存控制:最大年龄=3600。我还禁用了安全配置中的所有http头,如下所示: 遗憾的是,响应没有缓存在浏览器中。在查询资源时,查询将再次转到服务器。 同时,我完全删除了Spring Security,但它仍然不起作用。我没有正确理解什

  • 问题内容: 我希望能够创建一个自定义AngularJS服务,该服务在其数据对象为空时发出HTTP“获取”请求,并在成功时填充该数据对象。 下次调用此服务时,我想绕过再次发出HTTP请求的开销,而是返回缓存的数据对象。 这可能吗? 问题答案: Angular的$ http 内置了一个缓存。根据文档: cache – {boolean | Object} – 用$ cacheFactory创建的布尔值