当前位置: 首页 > 面试题库 >

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

方宜
2023-03-14
问题内容

我已经使用Spring Boot 1.0.2实现了REST服务器。我无法阻止Spring设置禁用HTTP缓存的HTTP标头。

我的控制器如下:

@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);
    }
}

所有HTTP响应均包含以下标头:

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache

我尝试了以下操作来删除或更改这些标头:

  1. 调用setCacheSeconds(-1)控制器。
  2. 调用httpResponse.setHeader("Cache-Control", "max-age=123")控制器。
  3. 定义@Bean是回报WebContentInterceptor的,我打过电话setCacheSeconds(-1)
  4. 将属性设置spring.resources.cache-period为-1或正值application.properties
    以上都不起作用。如何在Spring Boot中为所有或单个请求禁用或更改这些标头?

问题答案:

事实证明,Spring Security设置了无缓存HTTP标头。

以下内容禁用了HTTP响应标头Pragma: 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);
    }
}


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

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

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

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

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