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

如何在Spring启动中启用浏览器缓存

仲阳朔
2023-03-14

我正在尝试让spring boot让浏览器缓存静态资源。我的资源位于“静态”下的类路径中。当我查看发回的标题时,我看到修改标题设置得很好,但不知何故,标题“缓存控制:无存储”也被添加了。

HTTP/1.1 200
Last-Modified: Wed, 24 Aug 2016 08:50:16 GMT
Cache-Control: no-store
Accept-Ranges: bytes
Content-Type: text/css
Content-Length: 434554
Date: Wed, 24 Aug 2016 09:42:42 GMT

我已经看到了如何在Spring Boot中启用HTTP响应缓存的答案,但这似乎不适用于我,因为我没有使用Spring Security性,它不在类路径上。

我正在使用带有胸片的Spring启动1.4.0。

那么,如何让Spring Boot不包含缓存控制头呢?

共有2个答案

翟修永
2023-03-14

至少在最近(2018)版本的SpringBoot中,您可以设置以下属性:

spring.resources.cache.cachecontrol.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
spring.resources.cache.cachecontrol.cache-public= # Indicate that any cache may store the response.
spring.resources.cache.cachecontrol.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
spring.resources.cache.cachecontrol.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
spring.resources.cache.cachecontrol.no-store= # Indicate to not cache the response in any case.
spring.resources.cache.cachecontrol.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
spring.resources.cache.cachecontrol.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
spring.resources.cache.cachecontrol.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
spring.resources.cache.cachecontrol.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

凌成天
2023-03-14

事实证明,这很容易解决。

目录结构为classpath:/static/assets。要在响应中不添加缓存控制标头,请添加此类:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/").setCacheControl(CacheControl.empty());
    }
}

我仍然很困惑,spring boot的默认设置是“无商店”。。

 类似资料:
  • 我想在spring boot中设置禁用浏览器缓存(缓存控制:无缓存,无存储,最大年龄=0)。 我的环境 我搜索如果我想设置响应头的缓存控制:无缓存,只需添加在'application.yml'。 这样地 或 但这并不奏效 我无法在chrome浏览器的响应标头中显示缓存控件标头 在Spring框架中,我只是设置 如何设置禁用浏览器缓存在Spring启动没有Spring安全? 非常感谢。

  • 问题内容: 抱歉,您有愚蠢的问题,但是如何在webdriver中启动Chrome浏览器?我知道我必须指定chromedriver.exe的路径。问题是我无法下载chromedriver.exe,它已被删除。我发现的文件也没有.exe扩展名。我正在使用Eclipse,Java。请帮忙!我按照建议的步骤进行了所有操作,但是没有用。这是我的代码: 这是错误: 失败:测试java.lang.Illegal

  • 有人能帮我吗? 提前谢了。

  • 谁能帮我这个,如何启用铬浏览器使用jitsi满足Android和ios与jitsi满足api 我将disableDeepLinking:true和MOBILE_APP_PROMO:false设置为false,但不起作用。非常感谢。

  • 问题内容: 我已经使用docker安装了Jenkins,尽管我已经跳过了手动设置。当我构建映像并运行它时(仍然有很多事情要做),我需要在浏览器中键入IP和端口以打开Jenkins仪表板。 我的问题是,我可以在运行该映像的泊坞窗中使用脚本自动执行构建的Jenkins应该在浏览器中打开的工作吗? 任何需要运行的命令,请对其进行注释 我试图通过命令打开浏览器,但最终出现错误。任何人都可以使用我的自动化工

  • 据称,JIRA和Confluence附带了一个REST API浏览器工具 尽管默认情况下,REST API浏览器包含在JIRA服务器、Confluence服务器和Stash实例中,或者包含开发人员工具箱插件的任何应用程序中,但在艾特莱森云应用程序中不可用。https://developer.atlassian.com/server/framework/atlassian-sdk/using-the