I have a Spring Boot web application, where most endpoints require authentication. However, a few mappings shall allow anonymous access; they are exceptions from the general rule.
I cannot get this to work for POST calls, they are always getting 403.
The security configuration...
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().regexMatchers("/", "/posthello").anonymous()
.and()
.authorizeRequests().anyRequest().authenticated();
}
}
The controller...
@RestController
public class HelloController {
// This returns HTTP 200 and body on anonymous calls
@GetMapping("/")
public String helloWorld() {
return "Hello World!";
}
// This demands authentication, as expected
@GetMapping("/gethello")
public String getHelloWorld(String body) {
return "You got: Hello, World!";
}
// This always returns HTTP 403 on anonymous calls,
// even though it is supposed to be excepted
@PostMapping("/posthello")
public String postHelloWorld(@RequestBody String body) {
return "You posted: " + body;
}
}