如何在 Spring Boot 中向静态资源添加 Cache-Control 标头?

2023-12-26

我怎样才能添加Cache-ControlSpring Boot 中静态资源的 HTTP 标头?

尝试在应用程序中使用过滤器组件,它可以正确写入标头,但是Cache-Control标头被覆盖。

@Component
public class CacheBustingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

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

        HttpServletResponse httpResp = (HttpServletResponse) resp;
        httpResp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        httpResp.setHeader("This-Header-Is-Set", "no-cache, no-store, must-revalidate");
        httpResp.setHeader("Expires", "0");

        chain.doFilter(req, resp);
    }

我在浏览器中得到的是:

Cache-Control:no-store
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0

我想要的是:

Cache-Control:no-cache, no-store, must-revalidate
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0

发生这种情况是因为 Spring Security:它重写所有缓存标头以完全禁用缓存。 所以我们需要做两件事:

  1. 禁用静态资源的 spring security
  2. 启用静态资源缓存处理

在当前版本的 Spring Boot 中,我们可以在 application.properties 配置中更改此行为。

禁用某些资源的 spring security:

# Comma-separated list of paths to exclude from the default secured 
security.ignored=/myAssets/**

启用静态资源发送缓存标头:

# Enable HTML5 application cache manifest rewriting.
spring.resources.chain.html-application-cache=true

# Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
spring.resources.chain.enabled=true
# Enable the content Version Strategy.
spring.resources.chain.strategy.content.enabled=true 
# Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.content.paths=/** 

# Locations of static resources.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

就这样。现在 Spring 将检查您的静态文件是否已更改,并且可以发送更智能的响应(If-Modified-Since 等)并重写您的应用程序缓存。

另外,如果有原因不对某些资源使用基于内容的版本 - 您可以使用备用固定版本策略并在配置中显式设置版本:

#Enable the fixed Version Strategy.
spring.resources.chain.strategy.fixed.enabled=false 
# Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.fixed.paths= 
# Version string to use for the Version Strategy.
spring.resources.chain.strategy.fixed.version= 

在文档中查看更多内容 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties-security

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Spring Boot 中向静态资源添加 Cache-Control 标头? 的相关文章

随机推荐

  • VBA Excel 类模块中的子属性

    我有一个名为 cTask 的模块 其中包含以下代码 Private pMile As String Public Property Get Mile As String Mile pMile End Property Public Prop
  • 通过电话号码获取当前时间

    我想编写一个 RoR 移动应用程序 它可以为我提供给定电话号码所在国家 地区的当前时间及其区号 有没有一个好的 api 可以做到这一点 这不是一个好主意 对于拥有多个不同时区和多个区号的国家 地区 您会采取什么措施 时区与区号不一定存在一对
  • 为什么实体不能具有具有一对一强制逆关系的唯一性约束?

    为什么实体不能具有具有一对一强制逆关系的唯一性约束 有两个实体 Person 属性 名称 关系 部门 一对一 非可选 部门 属性 标题 唯一约束 关系 人 对多 可选 模型无法在 iOS 9 XCode 7 0 1 中编译 并出现错误配置的
  • 端口自更新:“macPorts 来源:命令执行失败”

    我在尝试着selfupdate我的 Macports 但我收到以下消息 Error opt local bin port port selfupdate failed Error synchronizing MacPorts sources
  • Android意图街景不再工作

    我曾经从我的应用程序启动一个意图到地图来显示街景全景 自从几天以来 当我启动意图地图时 它说 此处不提供街景视图 我尝试运行一个新项目 其目的只是Google https developers google com maps documen
  • 如何获取类型的所有公共(获取和设置)字符串属性

    我正在尝试创建一个方法来遍历通用对象列表并替换它们的所有类型属性string这是null或空并更换 有什么好的方法可以做到这一点 我有这种 外壳 到目前为止 public static void ReplaceEmptyStrings
  • 停止 Django 将时间转换为 UTC

    时区让我发疯 每次我以为我已经弄清楚了 有人改变了时钟 我就会遇到十几个错误 我想我终于达到了存储正确值的地步 我的时代是timestamp with time zone在保存它们之前我不会删除时区 TIME ZONE Europe Lon
  • 我可以删除或移动文件夹 NuGetFallbackFolder 吗?

    我可以删除文件夹吗C Program Files dotnet sdk NuGetFallbackFolder 或者将其移动到其他文件夹 最好不要放在C盘 如果可以去除的话 怎么去除呢 直接删除文件夹吗 有任何副作用或损害吗 如果可以移动
  • 从未分配的内存中读取安全吗?

    从随机地址读取安全吗 我知道写作是未定义的行为 但是只阅读怎么样 嗯 在许多可视化调试器中 我可以看到任意地址的内存内容 这是怎么做到的 由于行为是不确定的 所以答案也是不确定的 或者至少是不稳定的 如果你幸运的话 随机地址是在内存范围内
  • 通过 Delphi 在网页上按下按钮 [重复]

    这个问题在这里已经有答案了 Ex1 WebBrowser OleObject Document GetElementByID ID HERE Click https stackoverflow com questions 10593494
  • 爪哇和谷歌地图

    是否可以在 Swing 或小程序中渲染谷歌地图 我知道我也可以使用美国宇航局世界风 http worldwind arc nasa gov 但对于一个简单的应用程序来说 这似乎太过分了 SwingX项目中有JXMapViewer组件 您可以
  • Web API 操作参数间歇性为空

    相关问题 Web API ApiController PUT 和 POST 方法间歇性接收空参数 https stackoverflow com questions 16091024 web api apicontroller put an
  • 如何一次处理多个 xpath(基于提要结构)或使用相同的结构创建我自己的提要

    下面的代码经过测试并且可以工作 它打印具有此结构的提要的内容
  • SwiftUI 表单对齐 macOS

    我正在尝试包含一个自定义HStackSwiftUI 中的行Form如下 var body some View Form TextField Text text constant test Toggle Toggle isOn constan
  • 将一个数组拆分为 2 个数组 C#

    编辑 我尝试过 Take Skip 方法 但出现以下错误 Cannot implicitly convert type System Collections Generic IEnumerable
  • 如何设置与爷爷的相对位置!元素?

    我有这样的布局 div class one div class two div class three some text div div div 可以根据祖父元素设置三个相对位置 one 在 CSS 中 看看这个 http jsfiddl
  • Powershell:二维数组

    以下内容按预期工作 values a b c d foreach value in values write host Value 0 value 0 write host Value 1 value 1 结果 1 Value 0 a Va
  • 用Java正则表达式递归替换?

    我可以更换ABC 10 5 with 10 5 using replaceAll ABC 1 2 但我不知道该怎么做ABC ABC 20 2 5 or ABC ABC 30 2 3 2 如果我能够转换为 20 2 5我怎样才能转换回ABC
  • 内省模块类时“#map(&proc)”习惯用法如何工作?

    介绍成语 我找到了一个有趣但无法解释的替代方案 https stackoverflow com a 3680719 1301972到一个被接受的答案 该代码显然可以在 REPL 中运行 例如 module Foo class Bar def
  • 如何在 Spring Boot 中向静态资源添加 Cache-Control 标头?

    我怎样才能添加Cache ControlSpring Boot 中静态资源的 HTTP 标头 尝试在应用程序中使用过滤器组件 它可以正确写入标头 但是Cache Control标头被覆盖 Component public class Cac