0

I am using Spring Boot 2.2.2.RELEASE for serving a web and want to allow the static contents of my page such as css for every user, also for unauthenticated ones.

enter image description here

As I understand, resources under static folder are mapped into the root so accessing localhost:8080/css/sobre.css I should access my css file. But instead I get redirected to the login page.

This is ok, I know that with Spring Boot 1.5, resources under static were permited by default, but that changed.

So, I am using a custom WebSecurityConfigurerAdapter to permit that paths, so it looks like:

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/login*").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .and()
                .logout()
                .logoutUrl("/perform_logout")
                .deleteCookies("JSESSIONID");
    }

But when I go to the page which has a link to the resource i get:

No mapping for GET /css/sobre.css

enter image description here

And defenetly mapping every resource into the actual file is not the right approach.

So I don't really know how to do this. What am I missing? I just want to:

 <link rel="stylesheet" href="/css/sobre.css" />

Thank you.

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45
  • Does this answer your question? [Serving static web resources in Spring Boot & Spring Security application](https://stackoverflow.com/questions/24916894/serving-static-web-resources-in-spring-boot-spring-security-application) – dur Mar 07 '20 at 18:56
  • @dur That's URL is another approach to allow the resources, and ir works, but the mapping of the URL fails somehow. – Carlos López Marí Mar 07 '20 at 22:16
  • @dur By default Springs sets that property to /static, /public, /resources and /META-INF/resources and I have it under /static, so, no changes. – Carlos López Marí Mar 08 '20 at 11:27

1 Answers1

0

I had to do three things:

  • Removing @EnableWebMvc from the main class.

  • Adding a exception to that path in the Security adapter:

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/css/**");
    }
    

    OR with HttpSecurity:

    .antMatchers("/css/*").permitAll()
    
  • In case your static folder is not called "static", setting the root path to the resources folder:

    spring.resource.static-locations= "classpath:/publicfiles/"

Carlos López Marí
  • 1,432
  • 3
  • 18
  • 45