Spring Boot Thymeleaf 静态内容不加载 [英] Spring Boot Thymeleaf static content doesn´t load

查看:55
本文介绍了Spring Boot Thymeleaf 静态内容不加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了一个新的 Spring Boot(使用 Spring Security)和 Thymeleaf 项目.由于我想集成静态资源,我尝试了不同的文件结构,但都没有,Spring 将文件绑定在其中.由于我没有更改静态资源路径,我的控制台在启动时将 Mapped URL path [/**] 输出到类型为 [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 的处理程序上.>

我尝试过但没有成功的不同文件结构.

src|--main|--java|--资源|--静态|--css|--bootstrap.min.css|--custom_test.css|--模板|--....

还有

src|--main|--java|--资源|--webjars|--静态|--css|--bootstrap.min.css|--custom_test.css|--模板|--....

src|--main|--java|--资源|--公开|--css|--bootstrap.min.css|--custom_test.css|--模板|--....

在 HTML/Thymeleaf 中我尝试过

 <link rel="stylesheet" type="text/css" href="webjars/static/css/custom_test.css"/>

 <link rel="stylesheet" type="text/css" th:href="@{/css/custom_test.css}"/>

到目前为止,这些都没有奏效.我真的希望你能帮助我和其他有同样问题的人.提前致谢!

解决方案

你的问题可能是 Spring Security 的配置.首先,检查您浏览器的开发人员工具,看看您在客户端上没有获得的资源的响应是什么.响应状态代码可能是 302 或 401,具体取决于您当前的配置.

如果是这种情况,您需要向您的项目添加一个额外的配置,如下所示:

@Configuration@启用网络安全公共类 WebSecurityConfig 扩展了 WebSecurityConfigurerAdapter{@覆盖protected void configure(HttpSecurity http) 抛出异常{http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/favicon.ico").permitAll().antMatchers("/css/**").permitAll().antMatchers("/js/**").permitAll().antMatchers("/static/**").permitAll().antMatchers("/images/**").permitAll().antMatchers("/blog/**").permitAll().anyRequest().authenticated().和().formLogin().loginPage("/登录").permitAll().和().登出().permitAll();}//...}

I just started a new Spring Boot (with Spring Security) and Thymeleaf project. Since I wanted to integrate static resources I tried different file structures but with none of them, Spring bound the files in. Since I havent changed the static-resource path my console outputs Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] on startup.

Different file structures I tried and didn´t succeed with.

src
 |--main
    |--java
    |--resources
       |--static
          |--css
             |--bootstrap.min.css
             |--custom_test.css
          |--templates
             |--....

also

src
 |--main
    |--java
    |--resources
       |--webjars          
          |--static
             |--css
                |--bootstrap.min.css
                |--custom_test.css
       |--templates
          |--....

or

src
 |--main
    |--java
    |--resources
       |--public
          |--css
             |--bootstrap.min.css
             |--custom_test.css
          |--templates
             |--....

In HTML/Thymeleaf I tried

    <link rel="stylesheet" type="text/css" href="webjars/static/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="webjars/static/css/custom_test.css"/>

or

    <link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/custom_test.css}"/>

None of this worked so far. I really hope you can help me and others with the same poblem. Thanks in advance!

解决方案

Your problem may be the configuration of Spring Security. First, check with the developer tools of your browser and see what the response is for the resources that you do not get on the client. The response status code could be 302 or 401, depending on your current configuration.

If that is the case, you need to add an additional configuration to your project that looks like the following:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{        
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/static/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/blog/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }
// ...
}

这篇关于Spring Boot Thymeleaf 静态内容不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆