Medardas
Medardas

Reputation: 540

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:

enter image description here

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: enter image description here when inspecting website - styles.css is loaded as index.html in templates folder.

what do?

Upvotes: 2

Views: 110

Answers (2)

Idan Str
Idan Str

Reputation: 614

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

Upvotes: 2

khoibv
khoibv

Reputation: 369

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/\**",
      ...
    );
  }

  ...
}

Upvotes: 0

Related Questions