Problem statement
Secure a locker room API so that only known members can read locker data.
Requirements
SecurityConfig
The locker data, the controller that serves it, and the not-found handling are already written. Your work is SecurityConfig, which ships empty. Protect the locker endpoints with HTTP Basic authentication.
- Exactly two users can authenticate, seeded inside the security configuration: marco with password dive2026, and sana with password swim2026
- Expose the password-encoding rule as a bean and the username-to-user lookup as a bean, so both can be obtained from the application context
- Hold each password only in encoded form: for both seeded users, the stored password must not equal the raw password
- The encoding bean confirms a user's own raw password against the stored form, and rejects a different user's password as well as an empty password
- Every endpoint under /lockers requires credentials, and those credentials arrive with each request in the standard HTTP Basic Authorization header
- Paths outside /lockers, such as the root path /, require no credentials
- The application stays stateless: a successful authenticated response carries no Set-Cookie header, and authenticating one request never lets a following request without credentials through
- A request to a /lockers endpoint without credentials is answered with 401 Unauthorized and a JSON body whose only field is error, set to "Unauthorized". It is never redirected: no 302 status and no Location header
- Credentials that do not match a seeded user are answered the same way. This covers a wrong password, an unknown username, one user's password on the other user's name, and an empty password
GET /lockers
- With valid credentials of either user, return 200 OK with all three lockers in id order, each carrying its id, owner, and zone
GET /lockers/{id}
- With valid credentials, return 200 OK with that locker's id, owner, and zone
- With valid credentials and an unknown id, return 404 Not Found. The error message is "Locker not found with id: " followed by the requested id
Sample Requests & Responses
Expand to view details on sample requests and responses.
GET /lockers without credentials
Response (401 Unauthorized):
{
"error": "Unauthorized"
}
GET /lockers with credentials marco / dive2026
Response (200 OK):
[
{
"id": 1,
"owner": "Ravi Menon",
"zone": "POOL"
},
{
"id": 2,
"owner": "Elsa Brandt",
"zone": "GYM"
},
{
"id": 3,
"owner": "Tomas Silva",
"zone": "SAUNA"
}
]
GET /lockers/3 with credentials sana / swim2026
Response (200 OK):
{
"id": 3,
"owner": "Tomas Silva",
"zone": "SAUNA"
}
GET /lockers with credentials marco / wrongpin
Response (401 Unauthorized):
{
"error": "Unauthorized"
}
GET /lockers/99 with credentials marco / dive2026
Response (404 Not Found):
{
"error": "Locker not found with id: 99",
"timestamp": "2026-08-05T21:25:00.701402",
"path": "/lockers/99"
}
GET / without credentials
Response (200 OK):
Backend server running on port 8000
