1

So I've got a styles.css file under

resources
    |-static
      |-css
        |-styles.css 

... all standard stuff.

My WebSecurityConfig looks as follows:

protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .headers().frameOptions().sameOrigin().and() // allow the h2-console to be used in a frame
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/h2-console/**").permitAll() // enable access to the h2-console
                .antMatchers("/js/**").permitAll() // permit JS resources
                .antMatchers("/css/**").permitAll() // permit CSS resources
                ...
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }

But whenever I try to open the .css file on my localhost (via http://localhost:8080/css/styles.css) I recieve an unexpected 405 error (Method Not Allowed).
Trying to apply the stylesheet in a html documents prints out the following error in the Google Chrome console:

"refused to apply style from "..." because its MIME type ('application/json') is not a supported stylesheet MIME type, and strict MIME checking is enabled".

I am fairly new to java Spring and have only worked with bootstrap styles up to this point, so I might be missing something really obvious - been searching for an answer for hours -please help me out!

I am running Spring Boot Gradle,

springBootVersion = '2.0.4.RELEASE'
Denny
  • 27
  • 6
  • In your **html** add `` after adding all the css. – Sharan De Silva Sep 05 '18 at 14:46
  • What is this supposed to solve? If try to open the css file directly via http://localhost:8080/css/styles.css there is no html code in between me and the css file... – Denny Sep 05 '18 at 14:53
  • Trying to apply the stylesheet in a html documents prints out the following error in the Google Chrome console - you told. I meant that document – Sharan De Silva Sep 05 '18 at 14:55
  • But that's secondary - it's very likely due to the main error which is: I get a 405 error whenever I try to call the css file directly and manually. – Denny Sep 05 '18 at 15:01

1 Answers1

2

I had the exact same issue.

For me the 405 was a red-herring. If you disable everything in your Spring Security and go to a non-existent url it will still say 405, which makes no sense.

Turns out in my login controller I put accidentally

@PostMapping
public void postLogin(@ModelAttribute LoginForm form) {

With nothing next to the @PostMapping, so it was defaulting effectively to @PostMapping("/"). The fix was basically just to do the following:

@PostMapping("/login")

Toofy
  • 804
  • 9
  • 19