【发布时间】:2020-05-29 18:35:30
【问题描述】:
我正在尝试在 Spring Boot 中为特定静态文件类型设置缓存标头。 在目录 src/main/resources/static 中有几个不同文件类型的子目录:
src/main/resources/static/font --> *.otf
src/main/resources/static/lib --> *.js
src/main/resources/static/images --> *.png, *.jpg
有没有办法在 Spring 配置中按文件类型放置缓存头?
*.otf 365 days
*.png 30 days
*.jpg 7 days
Spring 版本是 5.2.3 和 Spring Boot 2.2.4 - Spring Boot 是否有可能处理它并使其不起作用?
试过
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
final CacheControl oneYearPublic = CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic();
// it does not "work" with "/static/fonts/"
registry.addResourceHandler("/fonts/{filename:\\w+\\.otf}").setCacheControl(oneYearPublic);
}
但我得到了奇怪的结果。当检查 DevTools 的 Network 选项卡时,我得到了这些标题:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
但是当我直接访问 URL 时,我得到 404
http://localhost/fonts/1952RHEINMETALL.otf
如果没有任何配置,我会得到“no-store”Cache-Control 标头。
【问题讨论】:
标签: spring spring-boot caching http-headers content-type