不会显示html图像中的Spring thymeleaf

鲍勃·基蒂729

下面,我使用的login.html为其中我加入的图像的页面test.png/static/images/

布局

因此,在login.html中,我有<img src="../static/images/test.png" width="1000" th:src="@{images/test.png}"/>,它提供了一个空白图像。为什么不显示?

在我的SecurityConfiguration.java档案中,我有

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http
      .csrf().disable()
      .authorizeRequests()
      //.antMatchers("/**").hasRole("ADMIN")
      .antMatchers("/static").permitAll()
      .and().formLogin()
      .loginPage("/login")
      .permitAll(); 

当我使用此配置时,它将使用默认的index.html页面,该页面可以很好地显示图像。但是,如果我取消注释.antMatchers("/**").hasRole("ADMIN"),它将带我到login.html,但无法查看图像。

Jakub Ch。

我可以发现3个问题。

  1. 相反的.antMatchers("/static"),你应该宁愿.antMatchers("/images/**")因为从任何src/main/resources/static将从您的应用程序的根服务(如解释在这里-记住,文件夹“公共”与“静”是可以互换的,以春季启动)。
  2. 匹配器的顺序.authorizeRequests()问题!只是作为方法文档的最后一个例子您应该颠倒您的蚂蚁匹配器:
   .antMatchers("/images/**").permitAll() // more detailed paths should go first
   .antMatchers("/**").hasRole("ADMIN")   // more general paths should go last
  1. 考虑使用th:src="@{/images/test.png}"代替th:src="@{images/test.png}"开头的斜杠使相对于应用程序根的路径与第一个建议相伴。Thymeleaf的文档所述

/(例如:)开头的相对URL/order/details将自动以应用程序上下文名称作为前缀。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章