Spring Boot - 即使WebSecurityConfig文件允许,也不会加载图像资源

时间:2018-03-20 06:50:58

标签: spring spring-boot spring-security thymeleaf

我最近想在我的Spring Boot应用程序中的页面上添加一些图像,用Thymeleaf模板化。

即使正在加载并成功提供其他资源(js,css),也不会提供允许的img文件夹内的图像,指定页面上会显示损坏的图像。

这是图片所在的位置:

的src /主/资源/静态/ IMG / image.png

摘自WebSecurityConfig:

.antMatchers("/css/**", "/js/**", "/img/**","/uploads/**","**/favicon.ico").permitAll()
来自HTML的

(尝试了2种方式,一种使用Thymeleaf,一种不使用前缀):

<img class="featurette-image pull-left" th:src="@{../static/img/image.png}" />

<img src="../static/img/image.png"/>

请指导我如何继续?

Ps:我使用的是Java DSL,而不是基于xml的配置。

我已经进行了搜索,并且看到一些解决方案不适合我的情况。 (之前检查了这些答案:

Spring Security Thymleaf static resources don't load

when spring security enabled, resouces(css or js) can't be loaded

HTML Image not displaying, while the src url works

1 个答案:

答案 0 :(得分:2)

在对图像的引用中不需要“静态”,因为Spring Boot默认将这些路径添加到类路径(source):

  • / META-INF /资源/
  • /资源/
  • /静态/
  • /公共/

因此,您对文件的引用应该是直接的:

th:src="@{/img/image.png}"