The 30 second answer
Authentication answers who you are: the server checks a password, token or certificate and establishes an identity. Authorization answers what that identity may do: can this user read this order or call this admin endpoint. Authentication comes first, and authorization runs on every request. A missing or invalid login gets 401; a valid login without permission gets 403. Most real breaches I'd worry about are authorization bugs, like reading someone else's data.
Authentication (authn) establishes identity. The client proves who it is with a password, a session cookie, a bearer token, a client certificate or a one-time code, and the server turns that proof into an authenticated principal for the request.
Authorization (authz) decides whether that principal may perform this action on this resource. It can be role-based (admins may delete products), attribute- or ownership-based (a customer may read only their own orders), or scope-based (this token was granted orders:read but not orders:write). Authentication usually happens once per session or token; authorization is checked on every request and often on every object.
The HTTP status codes. RFC 9110 defines 401 Unauthorized as a request that lacks valid authentication credentials, and the server must send a WWW-Authenticate challenge with it. 403 Forbidden means the server understood the request and refuses it; if credentials were sent, they are not enough. Despite its name, 401 is about authentication. RFC 9110 also allows a 404 instead of a 403 when the server wants to hide that a forbidden resource exists.
Where they live in Spring Security. Authentication is handled by filters and an AuthenticationManager, which put an Authentication into the SecurityContextHolder. Authorization is then decided by an AuthorizationManager: for URLs through authorizeHttpRequests, and for methods through @PreAuthorize with method security enabled.
@Bean
SecurityFilterChain api(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").hasRole("ADMIN") // authorization by role
.anyRequest().authenticated()) // any identity is enough
.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults())); // authentication by JWT
return http.build();
}
@PreAuthorize("#customerId == authentication.name") // ownership check per call
public List<OrderDto> ordersFor(String customerId) { ... }OAuth 2.0 and OpenID Connect. A common follow-up. OAuth 2.0 is an authorization framework: it lets a client get an access token to call an API on someone's behalf, and the token says what access was granted. OpenID Connect is an identity layer on top of OAuth 2.0 that adds authentication: the client gets an ID token stating who the user is. Saying "we use OAuth for login" is imprecise; login is OpenID Connect.
Alternate interview scenarios
"A logged-in customer changes the order ID in the URL and sees someone else's order. What went wrong?"
Authentication worked; authorization was missing at the object level. The endpoint checked that the caller was logged in but not that the order belongs to them. OWASP lists this as the first API security risk, Broken Object Level Authorization. The fix is to check ownership on every access, for example by querying with both IDs (findByIdAndCustomerId) or with a @PreAuthorize rule, return 404 or 403 when it fails, and add a test that one user cannot read another user's resource. Unguessable IDs help a little, but they do not replace the check.
"Should this response be 401 or 403?"
No token, an expired token or a bad signature is 401, because the caller is not authenticated. A valid token for a user without the needed role or scope is 403, because the identity is known and the action is refused.
Sources (7)
- RFC 9110: HTTP Semantics, 401 Unauthorized and 403 Forbiddenrfc-editor.org
- Spring Security reference: Servlet authentication architecturedocs.spring.io
- Spring Security reference: Authorization architecturedocs.spring.io
- Spring Security reference: Method securitydocs.spring.io
- RFC 6749: The OAuth 2.0 Authorization Frameworkrfc-editor.org
- OpenID Connect Core 1.0openid.net
- OWASP API Security Top 10 2023: API1 Broken Object Level Authorizationowasp.org