下面,我使用的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,但无法查看图像。
我可以发现3个问题。
.antMatchers("/static")
,你应该宁愿.antMatchers("/images/**")
因为从任何src/main/resources/static
将从您的应用程序的根服务(如解释在这里-记住,文件夹“公共”与“静”是可以互换的,以春季启动)。.authorizeRequests()
问题!只是作为方法文档的最后一个例子。您应该颠倒您的蚂蚁匹配器: .antMatchers("/images/**").permitAll() // more detailed paths should go first
.antMatchers("/**").hasRole("ADMIN") // more general paths should go last
th:src="@{/images/test.png}"
代替th:src="@{images/test.png}"
。开头的斜杠使相对于应用程序根的路径与第一个建议相伴。如Thymeleaf的文档所述:以
/
(例如:)开头的相对URL/order/details
将自动以应用程序上下文名称作为前缀。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句