0

I know that there are similar questions on this subject, but I can't seem to find a solution. I have a Spring MVC project and the problem is that I get the error specified in the title in the browser log when I run the app. If I open the index.html in browse (not from Spring) the CSS is loaded.

This is my structure.

index.html CSS declaration

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

Controller

@RequestMapping(value = { "/", "/index"}, method = RequestMethod.GET)
public ModelAndView index() {
    ModelAndView model = new ModelAndView();
    model.setViewName("index");

    return model;
}

I even tried to add this in the SecurityConfiguration

@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);

    web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/icons/**", "/javascript/**");

}

but it doesn't work.

The path is good because as I said when I open the index.html in browser the CSS is loaded, I think that the problem is with the SecurityConfiguration class, but I don't know what it is.

Does anyone have any idea?

6
  • From what I searched, Capital letters matter when accesing them this way, maybe in the browser it doesn't matter, can you confirm if maybe that could be? Commented Oct 24, 2019 at 13:44
  • @BugsForBreakfast it's not that, still getting the error Commented Oct 24, 2019 at 13:51
  • stackoverflow.com/questions/40574159/… what about this? Commented Oct 24, 2019 at 13:59
  • From my search, you need to have an 'static' directory, so you have the permission to access it, this is the most accurate stackoverflow.com/questions/41506091/… Commented Oct 24, 2019 at 14:12
  • @BugsForBreakfast thanks for all the research, in the end, I found the problem and I posted the answer. Commented Oct 24, 2019 at 14:27

1 Answer 1

1

So the error was indeed from SecurityConfiguration because I wasn't using the right annotation. The correct one is

@EnableWebSecurity

and I was using

@EnableWebMvc

So now my class looks like this:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable();

    http.authorizeRequests().antMatchers("/", "/index").permitAll().and()
            .exceptionHandling().accessDeniedPage("/libraryAppError");
    http.authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
}
}

And everything works fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.