When I run my Spring Boot Project with Thymeleaf, the browser won't load the css files but path for href and th:href is correct

Maido 47

so I read a lot of articles and threads about my problem, like these ones:

Refused to apply style because its MIME type ('application/json') is not a supported

https://github.com/froala/angular-froala/issues/170

But none of the answers can actually help me so I am asking my own question.

The current situation is:

I have a Spring Boot Project using thymeleaf and I have an html file under resources/templates and I also do have a css under ressources/templates/css.

Here is the structure:

  • src
    • main
      • resources
        • static
          • css
            • "my css file"
        • templates
          • "my html file"

Here is my html file:

<!DOCTYPE html>
<html lang="de" xmlns:th="http://www.thymeleaf.org">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Welcome to Mreza Mladih</title>
        <link rel="stylesheet" type="text/css" href="../static/css/styles.css" th:href="@{../static/css/styles.css}">
        <link href="https://fonts.googleapis.com/css2?family=Lato&family=Raleway&display=swap" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300&display=swap" rel="stylesheet">
    </head>

And after running the project, google chrome won't load the css file:

Refused to apply style from...

Furthermore:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter  {    
@Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers(
                    "/registration**",
                    "/js/**",
                    "/static/css/**",
                    "/static/img/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/")
                    .permitAll()
                    .and()
                    .logout()
                    .invalidateHttpSession(true)
                    .clearAuthentication(true)
                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                    .logoutSuccessUrl("/login?logout")
                    .permitAll();
        }
}

The funny thing about this is that my css actually is correct and it works! I use intellij and the IDE has a preview feature for html files and it actually works.

It would be really great if someone can help and thanks to you!

Greetings from Germany

Wim Deblauwe

Spring Boot serves anything that is in src/main/resources/static at the root of your application.

So the URL to use to get the CSS with Thymeleaf should be /css/styles.css.

Edit your HTML file like this:

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

In your security configuration, also use /css and not /static/css:

.antMatchers(
                    "/registration**",
                    "/js/**",
                    "/css/**",
                    "/img/**").permitAll()

You can also use:

http.authorizeRequests()
    .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
         .permitAll()
    .antMatchers("/registration**",
                 "/img/**")
        .permitAll()

PathRequest.toStaticResources().atCommonLocations() includes /css/*, /js/*, /images/*, /webjars/* and favicon.ico.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to transfer parameters in th:href in spring boot Thymeleaf?

Can't load my css when using spring security and thymeleaf

Why won't Django load my CSS files when I already used static?

Difference between th:href and href when linking absolute URLs in Thymeleaf

th:href Thymeleaf redirect url with path variables and object id's with Spring MVC

Why when inserting href links in my nav bar my grid loses its shape? what am i doing wrong? CSS HTML. Also submit button won't ask for the "required"

Why won't React.js load the images from my disk even when the specified path is correct?

Conditions in Thymeleaf th:href parameter

Why won't my image load when i run my app?

Why Does My CSS Grid when I add <a href> to an image?

My Spring Boot project doesn't load css properly from the static folder

Admin css won't load when deploying Django project on pythonanywhere

Spring Boot Thymeleaf doesnt load static files

Spring Boot and Thymeleaf - "a href"s to another sites going into 404 error

css file won't load when opening my website

Thymeleaf: Use a link with 'th:href' in Javascript

Can't load image with spring boot thymeleaf

How to get th:href="@{}" instead of href="" in links spring boot/angular app

Why won't my buttons appear when I run this JFrame?

My SpringBoot project won't load into Eclipse

Spock test won't run with a new spring-boot project created by start.spring.io

Using Spring Boot 1.3, spring-boot-devtools and Thymeleaf templates won't do live reload when changed in Netbeans

Springboot won't load my HTML page using Thymeleaf

i have errors when run spring boot project

JavaScript accordion behavior when it won't expand - set an href?

In my HTML project I've an issue on my folder href

Spring boot application won't run when trying to run from the jar file

When I want to run my j2ee+maven+spring-boot+mysql project on idea, there comes a error

Why can't I load js and css files on my pages?