401 vs 403 Status Codes
Fast recall
Quick revision
Core answer
401 Unauthorized means the request does not have acceptable authentication credentials; the response must include a WWW-Authenticate challenge. 403 Forbidden means the server understands the request but refuses to perform it, often because the signed-in user lacks permission.Interview-ready guide
Interview answer
401 Unauthorized means the request does not have valid authentication credentials for the protected resource. The name is old and slightly confusing: it usually means “you are not authenticated here,” not “you are authenticated but unauthorized.” The response must include a WWW-Authenticate challenge that tells the client which authentication scheme applies.
403 Forbidden means the server understood the request but will not perform it. The most common API example is a user with a valid login who lacks the required role, ownership, tenant membership, or account state. HTTP does not require every 403 caller to be authenticated, so “logged in but not allowed” is a useful everyday rule, not the complete protocol definition.
Suppose GET /account arrives with no bearer token or with an expired token. The API returns 401 and a Bearer challenge; a new login or token may let the request pass. If a normal member calls GET /admin/audit-log with a valid token, the API returns 403 because that identity is not allowed to read the audit log. Supplying the same token again will not change that result.
There is one important security boundary. A server may return 404 Not Found instead of 403 when admitting that a protected resource exists would leak information. Whether it returns 403 or a concealed 404, the authorization check must still happen. Keep the public message safe, and never omit the required challenge from a 401 response.
401 vs 403 Status Codes
Direct Answer
401 Unauthorized means the request does not have acceptable authentication credentials; the response must include a WWW-Authenticate challenge. 403 Forbidden means the server understands the request but refuses to perform it, often because the signed-in user lacks permission.
Deep Dive & Architecture
A protected request usually passes through two separate checks. Authentication asks, “Can the server accept these credentials and establish an identity?” Authorization asks, “May this identity perform this action on this resource?” Keeping those checks separate makes both the API response and the security log easier to understand.
The first check can happen at a gateway, a security filter, or the application itself. If the credentials are absent or unacceptable, processing stops with 401 and a challenge. After an identity is established, policy may check a role, record owner, tenant, subscription, or account state. A failed policy check normally produces 403.
With bearer tokens, RFC 6750 also uses the WWW-Authenticate response header to carry safe error information such as invalid_token or insufficient_scope. The API should not turn that header into a detailed security report: public responses should reveal only what the client needs to recover safely.
Code Example
Case 1 — credentials are missing
GET /account HTTP/1.1
Host: api.example.com
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="account-api"
Case 2 — the identity lacks permission
GET /admin/audit-log HTTP/1.1
Host: api.example.com
Authorization: Bearer valid-member-token
HTTP/1.1 403 Forbidden
Check your understanding
Answer these without looking back at the article.
- Which response header is required on a 401 response?
- When can a server return 404 instead of 403?
- Does HTTP require JWT for authentication?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.