Problem statement
Implement token-based login and role-gated access for a clinic records API.
Requirements
SecurityConfig
- Register exactly two staff accounts, exposed through a username-to-user lookup obtainable from the application context: nurse with password nursepass and role NURSE, and doctor with password doctorpass and role DOCTOR
- Store both passwords only in encoded form: the stored value must not equal or contain the raw password, and each account must still sign in with its raw password
- Keep POST /login open to everyone; every other endpoint accepts only requests that carry a valid token
- Never set a cookie on any response and keep no state between requests
The two provided record endpoints must be gated as follows.
GET /records
- With a valid token of role NURSE or DOCTOR, return 200 OK with the three record summaries in stored order
- Each summary carries id, patient, and summary, and never carries details
GET /records/{id}/full
- With a valid DOCTOR token, return 200 OK with the full record: id, patient, summary, and details
- With a valid NURSE token, return 403 Forbidden with error message: "Forbidden"
- With a valid DOCTOR token and an id that matches no record, return 404 Not Found with error message: "Record not found with id: " followed by the requested id
AuthController
POST /login
- Accept a JSON body carrying username and password
- On valid credentials, return 200 OK with a body carrying token
- The issued token is signed with the clinic.jwt.secret value configured in src/main/resources/application.properties, carries the account's username and role, and stops being valid clinic.jwt.ttl-minutes minutes after it is issued
- A token works immediately: logging in and using the returned token right away succeeds
- On a wrong password or an unknown username, return 401 Unauthorized with error message: "Invalid credentials"
JwtAuthenticationFilter and JwtService
Every request to a record endpoint is checked from its token alone:
- A request with no Authorization header, or with a header that does not carry a bearer token, is rejected with 401 Unauthorized and error message: "Unauthorized"
- A bearer value that cannot be verified against the configured secret, including a token whose signature has been tampered with, is rejected with 401 Unauthorized and error message: "Unauthorized"
- A token past its expiry is rejected with 401 Unauthorized and error message: "Token expired"
- A valid, unexpired token signed with clinic.jwt.secret is accepted even if it was not issued by this server's login, and the role it carries decides what the request may access
Sample Requests & Responses
Expand to view details on sample requests and responses.
POST /login
Request:
{
"username": "nurse",
"password": "nursepass"
}
Response (200 OK):
{
"token": "eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJudXJzZSIsInJv..."
}
POST /login
Request:
{
"username": "nurse",
"password": "wrong"
}
Response (401 Unauthorized):
{
"error": "Invalid credentials"
}
GET /records with a valid NURSE token in the Authorization header
Response (200 OK):
[
{
"id": 1,
"patient": "Alice Morgan",
"summary": "Routine checkup, stable"
},
{
"id": 2,
"patient": "Brian Osei",
"summary": "Sprained ankle, recovering"
},
{
"id": 3,
"patient": "Carla Reyes",
"summary": "Migraine follow-up"
}
]
GET /records with no Authorization header
Response (401 Unauthorized):
{
"error": "Unauthorized"
}
GET /records/1/full with a valid DOCTOR token
Response (200 OK):
{
"id": 1,
"patient": "Alice Morgan",
"summary": "Routine checkup, stable",
"details": "Blood pressure 118/76, resting heart rate 64, no medication changes."
}
GET /records/1/full with a valid NURSE token
Response (403 Forbidden):
{
"error": "Forbidden"
}
GET /records with an expired token
Response (401 Unauthorized):
{
"error": "Token expired"
}
GET /records/99/full with a valid DOCTOR token
Response (404 Not Found):
{
"error": "Record not found with id: 99"
}
