Spring Boot services · Chapter 31 of 41
Security errors
By the end of this chapter you can turn every security refusal into a clear JSON answer, and log people in without telling an attacker who has an account. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Security
- 5 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
The station managers' dashboard runs in a browser: log in once, and a session cookie carries you through the shift. Anjali's JavaScript calls /api/orders before logging in and gets a 302 redirect to an HTML login page; a successful login redirects too. "The app speaks JSON," she says. "Its errors should too."
Kabir finds something worse: a login as Bunty answers User account is locked. Now anyone knows Bunty exists. The HttpStatusEntryPoint from chapter 27 sends a bare status and no body, and does nothing for logins.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
SecurityConfig.java: a JSON 401 for a request without a login
Give the chain an AuthenticationEntryPoint, so a request without a login (GET /api/orders with no session) answers 401 with a JSON body (content type application/json) holding status 401 and error "Unauthorized", plus a message, instead of a 302 redirect to a login page.
- Task 2
SecurityConfig.java: a JSON 403 for a user without the role
Give the chain an AccessDeniedHandler, so a logged-in user without the role MANAGER on GET /api/admin/report gets 403 with a JSON body (content type application/json) holding status 403 and error "Forbidden", plus a message. A manager still gets 200.
- Task 3
SecurityConfig.java: a successful login answers 200 with the username
Give form login a success handler, so POST /auth/login with the right username and password answers 200 with a JSON body containing "username":"ravi" (the logged-in name), instead of a 302 redirect. The session it starts must then work: GET /api/orders with it answers 200 with customer ravi.
- Task 4 · interview question
Interview: give every login failure the same message, whatever was wrong
Fix the failure handler so a wrong password (priya), an unknown user (nobody) and a locked account (bunty) all answer 401 with exactly the same body, whose message is "Invalid username or password".
Be ready to explain why every login failure should look the same.
- Task 5
SecurityConfig.java: at most two sessions per user
Limit each user to two sessions at once. A third login succeeds and expires the session used least recently; that session's next request answers 401 with a JSON body holding status 401 and a message that contains the word "expired". The two newer sessions keep answering 200.
What you will learn
- The entry point and the access denied handler: who answers 401, and who answers 403.
- Login handlers: what a successful and a failed form login answer.
- One failure message: the interview question.
- Session limits: how many browsers one login may use.
More in Security
Build "Security errors" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.