簡體   English   中英

Thymeleaf 錯誤地加載靜態文件

[英]Thymeleaf incorrectly loads static files

所以我正確加載了靜態 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加載。

做什么?

在 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()
}

請嘗試檢查以下幾點:
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/\**",
      ...
    );
  }

  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM