简体   繁体   中英

Thymeleaf incorrectly loads static files

So I had static css files loading correctly and then for whatever reason, can't tell why, they stopped loading right. Here is my project structure:

在此处输入图片说明

import in index.html:

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

I even tried to set spring.resources.static-locations=classpath:/static/ in application.properties to no avail.

And the best part: 在此处输入图片说明 when inspecting website - styles.css is loaded as index.html in templates folder.

what do?

In spring security 4.x - the resources are permitAll in spring security.

In spring security 5.x - you should manually config it.

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

Please try to check below points:
1. ResourceHandler has css location

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

  ...
}

2. Exclude *.css in spring-security rules

class SecurityConfig extends WebSecurityConfigurerAdapter {

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

  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM