Quick revision
The short answer
Spring Security 6 applies authorization to every dispatcher type, including the ERROR dispatch the container makes to /error when a controller throws. If that error dispatch is not authenticated or not authorised, the error page itself is rejected, so the client receives Spring Security's bare 401 instead of Spring Boot's JSON error body. We fix it by permitting the error dispatch, with requestMatchers("/error").permitAll() or dispatcherTypeMatchers(DispatcherType.ERROR).permitAll(), and we add error responses to the checks we run on every security upgrade.
What to remember
In this answer, we saw that Spring Boot writes JSON error bodies in an ERROR dispatch to /error, and that Spring Security 6 started authorizing all dispatcher types, including that one. When the error dispatch has no authenticated user, the error page is rejected and the client gets a bare 401 instead of the real error. Permitting /error or the ERROR dispatcher type restores the body, and adding error responses to the upgrade test suite catches this the next time.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Security 6 applies authorization to every dispatcher type, including the ERROR dispatch the container makes to /error when a controller throws. If that error dispatch is not authenticated or not authorised, the error page itself is rejected, so the client receives Spring Security's bare 401 instead of Spring Boot's JSON error body. We fix it by permitting the error dispatch, with requestMatchers("/error").permitAll() or dispatcherTypeMatchers(DispatcherType.ERROR).permitAll(), and we add error responses to the checks we run on every security upgrade.
Why this question is asked
This is one of the most common surprises in a Spring Boot 3 migration. Nothing in our controllers changed, the happy path still works, and yet clients that relied on a JSON body with status, error and path for failures now receive an empty 401. Interviewers ask it because the explanation needs two pieces of knowledge together: how Spring Boot produces error responses, and what Spring Security 6 changed about which requests it checks. In this answer, we will recap how /error works, see what changed in Spring Security 6, reproduce the problem, fix it two ways, and make sure the next upgrade does not repeat it.
1A Quick Recap: How Spring Boot Builds Error Responses
When a controller throws an exception that no @ExceptionHandler handles, or calls response.sendError(...), the servlet container does not write the error itself. It makes a second internal request, an ERROR dispatch, to the error page Spring Boot registers at /error. That request is handled by Boot's BasicErrorController, which writes the familiar JSON body with timestamp, status, error and path.
A dispatch is one pass of a request through the filter chain and servlet. A normal request is a REQUEST dispatch; the internal call to /error is an ERROR dispatch, and a server-side forward is a FORWARD dispatch. The important detail is that the ERROR dispatch goes through the servlet filters again, including Spring Security's filter chain.
2What Changed in Spring Security 6
In Spring Security 5, the old FilterSecurityInterceptor by default only checked the original REQUEST dispatch. The ERROR dispatch to /error passed through without an authorization check, so the error body was always written.
In Spring Security 6, authorizeHttpRequests uses AuthorizationFilter, and it applies to all dispatcher types by default. The ERROR dispatch to /error is now a request like any other and must pass our rules. With a typical configuration of anyRequest().authenticated(), /error needs an authenticated user.
| Spring Security 5.x | Spring Security 6.x | |
|---|---|---|
| Authorization filter | FilterSecurityInterceptor | AuthorizationFilter |
| Checks REQUEST dispatch | yes | yes |
| Checks ERROR dispatch | no, by default | yes, by default |
| /error for an unauthenticated caller | JSON error body | 401 from the entry point |
3Reproducing the Problem
The cases that break are the ones where the error dispatch has no authenticated user. One is an endpoint that is public, permitAll, and throws: the caller never authenticated, so the error dispatch is anonymous. Another is a stateless API with bearer tokens: the token is processed by a filter that, like most OncePerRequestFilter subclasses, does not run again on the error dispatch, so whether the authentication is still visible during the ERROR dispatch depends on how the security context was stored for the request (check your version's defaults). When it is not visible, /error sees an anonymous caller.
5 messages in order. Play them, or step through them one at a time.
The error page never runs; Spring Security answered first.
Notice that even the status code is wrong: the real error was a 404, but the client sees a 401, because the failure that reached it was the rejection of /error, not the original problem.
4Fix 1: Permit the /error Path
The simplest fix is to make the error page public. The error page only reports on a request that was already let through, so permitting it does not give anyone new access.
Because rules are checked in order and the first match wins, the /error rule must come before anyRequest().
5Fix 2: Permit the ERROR Dispatch Type
Alternatively, we permit every ERROR dispatch regardless of path. This also covers applications that register custom error pages on other paths.
Some teams also permit DispatcherType.FORWARD for applications that forward to views, for the same reason. What we should not do is switch back to the old behaviour globally, for example with the deprecated options that limit filtering to REQUEST dispatches, because the new default closes a real gap: an application that forwards internally to a protected resource is now checked on that forward too.
6Re-testing Error Responses on Every Upgrade
This bug survived into production because tests checked successful responses and the 401 for missing credentials, but not the body of an error. So security upgrade checks should include error responses explicitly: a 404, a 400 validation error and a 500 from both a public endpoint and an authenticated one, asserting the status and the JSON body clients depend on.
A test with MockMvc does not always reproduce the container's ERROR dispatch, because MockMvc does not run the servlet container's error page mechanism; a @SpringBootTest with a real port and a TestRestTemplate or WebTestClient does. Running those few tests on each Spring Security or Spring Boot upgrade turns this from a production incident into a failing build.
7Follow-up questions
Is it safe to permit /error for everyone?
Yes in practice, because /error only renders details about the request that triggered it, and the original request was already authorised or rejected. We should still make sure error responses do not include stack traces or exception messages in production, which Spring Boot hides by default with server.error.include-stacktrace and related properties.
Why do some errors still return the correct body?
Errors on requests where the authentication is visible during the error dispatch pass the anyRequest().authenticated() rule, so /error runs normally. Only the error dispatches without an authenticated user are rejected, which is why the team saw the problem for some errors and not others.
Would a @ControllerAdvice avoid the problem?
Yes, for exceptions it handles, because it writes the response within the original request and no ERROR dispatch happens. But errors outside controllers, such as filter failures or unhandled exceptions, still go to /error, so we need the permit rule anyway.
8Conclusion
In this answer, we saw that Spring Boot writes JSON error bodies in an ERROR dispatch to /error, and that Spring Security 6 started authorizing all dispatcher types, including that one. When the error dispatch has no authenticated user, the error page is rejected and the client gets a bare 401 instead of the real error. Permitting /error or the ERROR dispatcher type restores the body, and adding error responses to the upgrade test suite catches this the next time.
Open with
The difference is that Spring Security 6 authorizes every dispatcher type, not just the original request. When a controller throws, the servlet container makes an ERROR dispatch to /error, where Spring Boot's BasicErrorController writes the JSON body.
Then, if they want more
In Spring Security 5 the old FilterSecurityInterceptor did not check that dispatch by default, so the body was always written. In 6, AuthorizationFilter checks it, so /error falls under rules like anyRequest().authenticated().
When the caller is not authenticated during the error dispatch, for example on a public endpoint that throws, or a token-based request where the authentication is not visible on the second dispatch, the error page itself is rejected and the entry point returns a bare 401, even if the real error was a 404.
The fix is to permit the error dispatch: requestMatchers("/error").permitAll() before anyRequest, or dispatcherTypeMatchers(DispatcherType.ERROR).permitAll(). That is safe because the error page only reports on a request that was already handled. Then I would add tests with a real server for 400, 404 and 500 responses from public and authenticated endpoints, asserting the body, and run them on every security upgrade.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6, Spring Security 5, Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After an upgrade to Spring Security 6, an API returns 401 with an empty body for some...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/575e6f6d8b/