how to connect css to spring boot?

My static folder is located in resources.I use the link below, but it doesn't work for me what to do?

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

Upvotes: 0

Views: 209

Answers (1)

FakeIfe
FakeIfe

Reputation: 123

This is the correct syntax if your css is stored in a css folder:

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

What you posted looks right so assuming all your files are where they should be, the issue is probably coming from spring security

If you added spring security as a dependency, you need to tell spring security to not require authentication for your static files.

You can do that by creating a WebSecurityConfig class that extends WebSecurityConfigurerAdapter, and implement the following:

protected void configure(HttpSecurity http) throws Exception {
        String [] staticResources = {
                "/css/**",
                "/js/**"
        };
        http
                .authorizeRequests()
                .antMatchers(staticResources).permitAll()
    }

Spring automatically looks in the static folder the staticResources array should contain, well... all your static resources. If you add an images folder, you should also add it to the staticResources array like: /images/** if you want to be able to use the images in all your pages

The remaining code:

Hey http, I need you to authorizeRequests matching the contents of staticResources. 
I also would like to permitAll users to access the content of staticResources.

Upvotes: 1

Related Questions