Quick revision
The short answer
I would standardise on RFC 9457 problem details (application/problem+json) for security errors as well as all other errors, and ship them in a shared starter that provides AuthenticationEntryPoint and AccessDeniedHandler beans producing the standard body and the correct WWW-Authenticate header. The body never reveals why authentication failed in a way that helps attackers, but always carries a correlation id so support can find the details in the logs. Each service runs contract tests that pin down its 401 and 403 responses.
What to remember
In this answer, we standardised 401 and 403 across 80 services on RFC 9457 problem details, kept the WWW-Authenticate header, and defined a short catalogue of problem types. A shared starter provides the AuthenticationEntryPoint and AccessDeniedHandler beans and the matching controller advice, the body never reveals reasons that help attackers but always carries a correlation id that leads support to the logged details, and contract tests in each service keep the format from drifting during and after rollout.
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.
I would standardise on RFC 9457 problem details (application/problem+json) for security errors as well as all other errors, and ship them in a shared starter that provides AuthenticationEntryPoint and AccessDeniedHandler beans producing the standard body and the correct WWW-Authenticate header. The body never reveals why authentication failed in a way that helps attackers, but always carries a correlation id so support can find the details in the logs. Each service runs contract tests that pin down its 401 and 403 responses.
Why this question is asked
When 80 services each answer 401 and 403 in their own way, every client has to special-case each one: one returns an HTML page, one an empty body, one {"message": ...}, one a 500 because a security exception leaked into a generic handler. Client teams lose time, and inconsistencies also hide security bugs. Interviewers ask this design question to see whether we can pick a standard, make it the easy path for 80 teams, and think about what an error response should not say. In this answer, we will choose the format, decide what goes in the body, design the shared starter, look at what not to leak, and finish with contract tests and rollout.
1Choosing a Standard Format
There is no need to invent a format. RFC 9457, which replaced RFC 7807, defines problem details: a JSON object with media type application/problem+json and a few standard members. type is a URI that identifies the kind of problem, title is a short human-readable summary, status repeats the HTTP status, detail explains this occurrence, and instance identifies the request. Extra members are allowed.
Using it for security errors as well as validation and business errors means clients write one error parser. Spring Framework already supports it with the ProblemDetail class, and Spring MVC can produce it for its own exceptions (spring.mvc.problemdetails.enabled=true in Boot), so security errors are the piece that usually still needs work.
Notice that the WWW-Authenticate header stays. It is part of the HTTP and bearer token standards, and clients and libraries rely on it to decide whether to refresh a token. Problem details add a body; they do not replace the header.
2What Goes in the Body
We define a small catalogue of problem types for security, published on a page the type URIs point to, so client teams can look them up.
| type suffix | Status | When |
|---|---|---|
| unauthenticated | 401 | No credentials, or credentials not accepted |
| token-expired | 401 | Bearer token expired; client should refresh |
| forbidden | 403 | Authenticated, but lacks permission |
| csrf-rejected | 403 | Browser request without a valid CSRF token |
| rate-limited | 429 | Too many failed attempts |
3The Shared Starter
The platform team publishes a Spring Boot starter. It contains a ProblemDetailsAuthenticationEntryPoint and a ProblemDetailsAccessDeniedHandler as beans, a small writer that serialises a ProblemDetail with the correlation id from the current trace, and auto-configuration that registers them in the service's SecurityFilterChain through a customizer. It also provides the matching @ControllerAdvice for method-security exceptions, so a 403 from @PreAuthorize looks the same as one from a URL rule.
Notice that the starter does the work once, for all services. Most teams add the dependency and get the standard. Teams with unusual chains can still inject the beans and set them in exceptionHandling() themselves, which keeps the output identical.
4What Not to Say
Error responses are read by attackers too, so the standard is strict about what the body may reveal. It never says whether a username exists ('user not found' versus 'wrong password'), never echoes the token, never names the signing key, the algorithm or the validation step that failed, and never includes stack traces. The difference between token-expired and unauthenticated is acceptable because it reveals nothing an attacker could not see by decoding their own token, and it helps honest clients.
Instead, the details go into the server log, together with the same correlation id that is in the body. A client team reporting a problem sends the correlation id, and support finds the exact reason (expired by 4 minutes, wrong audience, unknown key id) without the response ever exposing it.
- Request refusedEntry point or access denied handler
- Log the full reasonWith correlation id; never the token itself
- Return problem+jsonGeneric detail plus the same correlation id
- Client reports the correlation idSupport finds the exact reason in logs
4 steps in order. Play them, or step through them one at a time.
5Contract Tests and Rollout
A standard that is not tested drifts. The starter ships a small test kit: a base class or JUnit extension that every service's test suite runs, which sends a request with no token, with an expired token and with a token lacking a scope to one protected endpoint, and asserts the status, the content type, the WWW-Authenticate header, the type URI and the presence of the correlation id. If a team overrides the handlers and breaks the format, its build fails.
For rollout across 80 services, we first agree the format with the main client teams, then release the starter, migrate services in batches of 10 to 20 through normal dependency upgrades, and track adoption with a simple metric: the share of 401 and 403 responses across all services whose content type is application/problem+json. Clients accept both the old and new shapes until that reaches 100 percent.
6Follow-up questions
Why not let Spring Boot's /error endpoint produce the security errors?
Error dispatch adds a second internal request that is itself secured since Spring Security 6, its body depends on Boot's error attributes and properties, and it is easy for services to configure differently. Writing the problem details directly in the security handlers gives the same result everywhere and avoids the extra dispatch.
Should the 403 say which permission was missing?
Usually not in the body, because it maps the permission model for an attacker. For bearer tokens, the WWW-Authenticate header with error="insufficient_scope" may include the required scope, which RFC 6750 allows and some clients need; we decide that once in the standard and log the details server side.
How do you handle services that are not Spring Boot?
The standard is the format and the test kit, not the library. The starter is the easy path for Spring services; other stacks get a short specification with examples and run the same contract tests against their endpoints.
7Conclusion
In this answer, we standardised 401 and 403 across 80 services on RFC 9457 problem details, kept the WWW-Authenticate header, and defined a short catalogue of problem types. A shared starter provides the AuthenticationEntryPoint and AccessDeniedHandler beans and the matching controller advice, the body never reveals reasons that help attackers but always carries a correlation id that leads support to the logged details, and contract tests in each service keep the format from drifting during and after rollout.
Open with
I'd pick an existing standard rather than invent one: RFC 9457 problem details, application/problem+json with type, title, status, detail and instance, used for security errors and every other error, so clients write one parser.
Then, if they want more
Spring already has ProblemDetail, so the missing piece is usually the security handlers. I'd define a short published catalogue of problem types, like unauthenticated, token-expired, forbidden and csrf-rejected, so clients switch on the type URI rather than on text.
Then a shared starter provides an AuthenticationEntryPoint and an AccessDeniedHandler as beans that write the problem body, and auto-configuration registers them in each service's chain. The entry point delegates to BearerTokenAuthenticationEntryPoint first, because the WWW-Authenticate header is part of the standard and clients use it to refresh.
The starter also includes controller advice so a 403 from @PreAuthorize looks the same as one from a URL rule. On content, the body must not help attackers: no 'user not found' versus 'wrong password', no token echo, no key or validation details, no stack traces.
Instead every body carries a correlation id, and the full reason is logged with that id, so support can answer precisely. Finally, the starter ships a contract test kit that every service runs: no token, expired token, missing scope, asserting status, content type, header, type and correlation id.
I'd roll it out through dependency upgrades in batches and track the share of 401 and 403 responses that are problem+json.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6
- 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 "Different services return different error formats for 401 and 403, and client teams...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/165b0be578/