2016-11-21 121 views
0

我刚开始一个新的Spring Boot(带有Spring Security)和Thymeleaf项目。由于我想集成静态资源,我尝试了不同的文件结构,但都没有,Spring将文件绑定到了这个文件中。 因为我没有改变静态资源路径,所以我的控制台在启动时输出Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]Spring Boot Thymeleaf静态内容不加载

不同的文件结构,我试过,没有成功。

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

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

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

在HTML/Thymeleaf我试图

<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"/> 

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

到目前为止,这一切都没有奏效。 我真的希望你能帮助我和其他人一样的问题。 在此先感谢!

+0

的可能的复制[春的IntelliJ项目引导找不到与Thymeleaf我的CSS文件] (http://stackoverflow.com/questions/40452365/intellij-spring-boot-project-cant-find-my-css-files-with-thymeleaf) – DimaSan

回答

0

您的问题可能是Spring Security的配置。首先,与您的浏览器的开发人员工具进行核对,看看您在客户端上无法获得的资源的响应情况。响应状态码可能是302或401,具体取决于您当前的配置。

如果是这样的话,你需要一个额外的配置添加到您的项目,如下所示:

@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(); 
    } 
// ... 
} 
+0

它确实是一个302响应代码。先生,非常感谢您的帮助! – Marius

相关问题