My Spring Boot application provides several endpoints. Following this article, I'd like to restrict all endpoints by default, so that they require authentication via a JWT token. Only certain paths shall be public. My understanding is that all paths defined in .antMatchers(PUBLIC_RESOURCES).permitAll()
will be "public" without any authentication.
This works fine for the GET
method but when hitting the same endpoint(s) with a POST
then I get a HTTP 403 (Forbidden). I don't understand the reason for this.
This is my current security configuration:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String[] PUBLIC_RESOURCES = {
"/",
"/user/login"
};
@Override
protected void configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity
.antMatcher("/**").authorizeRequests()
.antMatchers(PUBLIC_RESOURCES).permitAll()
.anyRequest().authenticated();
}
}
And the controller:
@RestController
@RequestMapping(path = "user")
public class LoginController {
@GetMapping
public String get() {
return "Hey, Joe (get)";
}
@PostMapping
public String post() {
return "Hey, Joe (post)";
}
}