Problem statement
Secure a newsroom article API with three fixed accounts, a role-based permission matrix, and custom JSON rejection responses.
Requirements
SecurityConfig
- Exactly three accounts exist: reader with password readerpass and role READER, writer with password writerpass and role WRITER, and editor with password editorpass and role EDITOR
- Callers authenticate by sending their username and password with the request using the HTTP Basic scheme
- Passwords must be kept only in encoded form: the stored password value must neither equal nor contain the raw password
- GET /articles is open to every signed-in account: reader, writer, and editor all receive the article list
- POST /articles is allowed for writer and editor; a signed-in reader is refused
- DELETE /articles/{id} is allowed for editor only; a signed-in reader or writer is refused
- Permission is decided before the request body is examined: a reader sending an invalid body to POST /articles still receives 403 Forbidden, and a request without credentials still receives 401 Unauthorized
- A refused request never changes the stored articles: a refused create adds nothing and a refused delete removes nothing
- Every request stands alone: nothing is remembered between requests, an authenticated response carries no Set-Cookie header, and a request without credentials is refused even immediately after a successful one
- A state-changing request succeeds with valid credentials and a permitted role alone, with no extra token of any kind
RestAuthenticationEntryPoint and RestAccessDeniedHandler
- A request without credentials receives, on every endpoint, 401 Unauthorized with a JSON body holding a single field error with the exact value "Unauthorized"
- A request with a wrong password or an unknown username receives the same 401 Unauthorized response
- A signed-in account calling an endpoint its role does not allow receives 403 Forbidden with a JSON body holding a single field error with the exact value "Forbidden"
Provided article endpoints
The article endpoints are provided complete and must keep working through the security layer:
- GET /articles returns 200 OK with every stored article, each carrying its id and title; an empty newsroom yields an empty array
- POST /articles accepts a JSON body with a title and returns 201 Created with the stored article, including its generated id; the stored article is readable on a fresh read of the store
- If title is missing, empty, or whitespace-only, the response is 400 Bad Request and a rejected body stores nothing. The error message is "Title must not be blank"
- DELETE /articles/{id} removes exactly that article and returns 204 No Content with an empty body
- Deleting an unknown id returns 404 Not Found and removes nothing. The error message is "Article not found with id: " followed by the requested id
Sample Requests & Responses
Expand to view details on sample requests and responses.
POST /articles
curl -u writer:writerpass -H 'Content-Type: application/json' -d '{"title":"Transit Strike Enters Third Day"}' http://localhost:8000/articles
Response (201 Created):
{
"id": 1,
"title": "Transit Strike Enters Third Day"
}
GET /articles
curl -u reader:readerpass http://localhost:8000/articles
Response (200 OK):
[
{
"id": 1,
"title": "Transit Strike Enters Third Day"
},
{
"id": 2,
"title": "Harbor Lighthouse Restoration Complete"
}
]
POST /articles
curl -u reader:readerpass -H 'Content-Type: application/json' -d '{"title":"City Council Election Preview"}' http://localhost:8000/articles
Response (403 Forbidden):
{
"error": "Forbidden"
}
GET /articles
curl http://localhost:8000/articles
Response (401 Unauthorized):
{
"error": "Unauthorized"
}
GET /articles
curl -u reader:wrongpass http://localhost:8000/articles
Response (401 Unauthorized):
{
"error": "Unauthorized"
}
POST /articles
curl -u writer:writerpass -H 'Content-Type: application/json' -d '{"title":" "}' http://localhost:8000/articles
Response (400 Bad Request):
{
"error": "Title must not be blank",
"timestamp": "2026-08-05T21:27:56.853342",
"path": "/articles"
}
DELETE /articles/1
curl -u editor:editorpass -X DELETE http://localhost:8000/articles/1
Response (204 No Content): empty body
DELETE /articles/99
curl -u editor:editorpass -X DELETE http://localhost:8000/articles/99
Response (404 Not Found):
{
"error": "Article not found with id: 99",
"timestamp": "2026-08-05T21:27:57.106191",
"path": "/articles/99"
}
