Closed as not planned
Description
Describe the bug
In an application (using Spring Boot 3.0.1) the response body does not match the Content-Type
header for a 403 Forbidden response if the request contains the header Accept: application/problem+json, application/json
:
Content-Type: application/problem+json
{"timestamp":"2022-12-23T07:44:25.247+00:00","status":403,"error":"Forbidden","path":"/secret"}
Note: I'm using the shown mime type order because of spring-projects/spring-framework#29588
To Reproduce
- Setup an application with
- basic auth configuration and
- an endpoint that needs specific privileges (e.g.
@Secured("ROLE_ADMIN")
)
- Send a request to that endpoint
- with a valid user/auth but insufficient privileges and
- specify a request header
Accept: application/problem+json, application/json
Expected behavior
- The
Content-Type
response header must reflect the actual type of the content - When Problem Details are enabled, I'd expect that all errors (including 403 Forbidden) are returned as a Problem Detail response (RFC 7807). Also note that 401 Unauthorized does not contain a response body at all – I don't know if this is intended or another bug.
Sample
@SpringBootApplication
@RestController
@EnableWebSecurity
@EnableMethodSecurity
public class Application {
@Bean
public UserDetailsService userDetailsService() {
return new InMemoryUserDetailsManager(User.withDefaultPasswordEncoder()
.username("user").password("password").roles("USER").build());
}
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/secret")
public String secret() {
return "Secret";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Request:
curl -i http://localhost:8080/secret \
-u "user:password" \
-H "Accept: application/problem+json, application/json"
I already opened this issue as spring-projects/spring-security#12450 but learned that is related to Spring Boot, not Spring Security.