Spring Boot中的Thymeleaf缓存和安全性

问题描述 投票:0回答:3

在我的网络应用程序(Spring Boot + Spring Security + Thymeleaf)中,出于安全目的,我禁用了缓存:

spring.thymeleaf.cache=false

我不记得从哪里获得这些信息,或者它是否真实。我的网站上有很多图片,我想要缓存它们。你会推荐什么?

spring spring-boot spring-security thymeleaf
3个回答
0
投票

看看Spring Security – Cache Control Headers,它解释了ResponseEntity对象的缓存,它可以用来以字节数组的形式返回一个图像。

This还展示了一种专门缓存图像的方法,虽然帖子本身是从2015年开始的,但某些部分仍然可以相关。


0
投票

实际上spring.thymeleaf.cache属性与安全无关,但更多与性能有关。如果禁用Thymeleaf缓存,模板将在需要解析时自动重新加载,它与热交换服务器端模板有关。

这在开发过程中很有用,因为您可以立即看到模板的更改。如果不是,则必须重新启动应用程序。

请参阅Developer tools上有关其用途的文档。

从版本4.x到某个地方的Spring有几种方法来实现static resource caching版本控制(缓存清除机制)。假设您通过Spring将图像作为静态资源提供服务,您可能需要查看这些内容。

如果图像本身不需要保护,那么将它们作为静态资源提供,应用缓存应该就足够了。


0
投票

Spring Security会自动“缓存半身像”所有请求,which is by design

但是,对于不需要由Spring Security管理的图像,您可以在WebSecurityConfigurerAdapter中禁用特定资源目录(在这种情况下,images目录将位于../resources/static目录中)。

@Override
public void configure(WebSecurity webSecurity) {
    webSecurity.ignoring().antMatchers(
                "/images/**"
    )
}

Spring Boot way是补充

spring.resources.cache-period=your value here in seconds

当您想要对模板进行热重载时,您只需要在开发中关闭Thymeleaf缓存。

测试它的工作原理

curl -X GET -I https://your-site/images/foo.png

这将在浏览器级别缓存它们。如果需要在服务器级别缓存它们,可以使用像nginx这样的反向代理。

© www.soinside.com 2019 - 2024. All rights reserved.