【问题标题】:Thymeleaf incorrectly loads static filesThymeleaf 错误地加载静态文件
【发布时间】:2019-02-15 20:27:39
【问题描述】:

所以我正确加载了静态 css 文件,然后出于某种原因,不知道为什么,它们停止正确加载。 这是我的项目结构:

在 index.html 中导入:

<head>
  <link rel="stylesheet" th:href="@{/css/styles.css}"/>
</head>

我什至尝试在application.properties 中设置spring.resources.static-locations=classpath:/static/ 无济于事。

最好的部分: 检查网站时 - styles.csstemplates 文件夹中加载为index.html

做什么?

【问题讨论】:

    标签: java spring spring-mvc thymeleaf


    【解决方案1】:

    在 spring security 4.x 中 - 在 spring security 中资源是 permitAll

    在 spring security 5.x 中 - 你应该手动配置它。

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/css/**", "/js/**").permitAll()
    }
    

    【讨论】:

    • 我根本没有 Spring Boot 安全启动器,但无论如何它并没有帮助,仍然将 styles.css 加载为 index.html...
    【解决方案2】:

    请尝试检查以下几点:
    1.ResourceHandler有css位置

    class WebConfig implements WebMvcConfigurer {
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**")
          .addResourceLocations("classpath:/static/css/");
      }
    
      ...
    }
    

    2。在 spring-security 规则中排除 *.css

    class SecurityConfig extends WebSecurityConfigurerAdapter {
    
      @Override
      public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
          "/css/\**",
          "/js/\**",
          "/img/\**",
          ...
        );
      }
    
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 2013-03-03
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2021-12-01
      • 2016-08-17
      • 2017-04-05
      相关资源
      最近更新 更多