Spring Boot services · Chapter 39 of 41
Project: secure ordering
By the end of this chapter you can secure a whole API on your own: accounts with hashed passwords, stateless JWT login, ownership checks, roles, and refresh tokens that work once. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Projects
- 6 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
Customers will order from their phones next week. Anjali tried the test build: "I could see someone else's order by changing the number in the URL. And I had to log in again every fifteen minutes." Meera adds: "Only managers give refunds."
Every piece is a chapter you have done: password hashes (24, 25), JWTs and the filter (26, 27), roles (28), refresh tokens (30), security errors (31). In a real API they have to fit together, and the gaps between them are where the bugs from Anjali's list come from.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
AuthController.java: sign customers up with a BCrypt hash
Write POST /auth/signup: save a CUSTOMER account whose password is hashed with the PasswordEncoder (a BCrypt hash starting with $2, never the password itself) and answer 201 Created with an AccountView: email and role CUSTOMER, and no password hash.
An email that already has an account answers 409 Conflict. A body that is not valid answers 400: the email must be an email address and the password 8 to 72 characters, neither blank. A refused signup saves nothing.
- Task 2
AuthController.java: log in with an access token and a refresh token
In SecurityConfig.java, write JwtService.issueAccessToken(email, role): a compact JWT signed with the key field (from tapri.jwt.secret), subject the email, a claim role with the role's name ("CUSTOMER"), issued at the Clock's instant and expiring 15 minutes (ACCESS_TTL) later.
In AuthController.java, write tokensFor(account): a new refresh token of 32 random bytes from RANDOM, Base64 URL-encoded without padding, stored on the account (and saved) with an expiry 7 days (REFRESH_TTL) after the clock's instant, returned with an access token as Tokens(accessToken, refreshToken, "Bearer", 900). Then POST /auth/login answers 200 with tokensFor(account) when the password matches the account's hash, and the same 401 body, unauthorized("Wrong email or password"), for a wrong password and for an unknown email.
- Task 3
SecurityConfig.java: authenticate every request by its Bearer token
Write JwtService.parse(token): verify the token with the key field, judge its expiry by the injected Clock (the Check's clock is in 2031), and return its claims; a tampered, foreign-key or expired token throws JwtException. Write JwtAuthFilter: for "Authorization: Bearer <token>" that parses, put an authenticated UsernamePasswordAuthenticationToken in the SecurityContextHolder with the subject (the email) as its name and one authority, "ROLE_" and the role claim; anything else authenticates nobody.
Make the chain stateless (no HTTP session, even for a refused request), open /auth/** and /error to all, want an authenticated caller everywhere else, answer 401 (not 403) when there is none, and run new JwtAuthFilter(jwt) before UsernamePasswordAuthenticationFilter. A good Bearer token places an order (201, customer is the token's email); no token, a token signed with another key, an expired token or "Bearer not-a-token" answers 401.
- Task 4
OrderController.java: customers see only their own orders
Write GET /orders/mine: the caller's orders (the caller's name is the email from the token), oldest first, as OrderViews; another customer's orders never appear.
Write GET /orders/{id}: 200 with the order when it is the caller's, 404 when it belongs to someone else (not 403) or does not exist; a MANAGER may see any order. Needs task 3.
- Task 5
OrderController.java: only managers refund, and only once
Make POST /orders/{id}/refund for the role MANAGER only, with a rule in the chain: a CUSTOMER's token answers 403, no token 401, and the order stays PLACED.
For a manager, mark the order REFUNDED, save it and answer 200 with the OrderView (status REFUNDED). Refunding it again answers 409 Conflict, and an id that does not exist answers 404. Needs task 3.
- Task 6 · interview question
Interview: rotate refresh tokens and revoke them on logout
Write POST /auth/refresh: a refresh token stored on an account and not yet expired by the Clock answers 200 with a new pair from tokensFor(account), which replaces the account's refresh token, so the old one stops working. An unknown, already used or expired refresh token answers 401 (the unauthorized helper).
Write POST /auth/logout: clear the refresh token and its expiry on the account that holds it, and answer 204 No Content whether or not one matched; refreshing with it then answers 401. The new access token must work on GET /orders/mine, so this needs tasks 2, 3 and 4. Be ready to explain why rotation helps when a refresh token is stolen.
What you will learn
- A stateless chain: a token on every request, 401 for anything wrong, and no HTTP session.
- Who is calling: the token's subject, never a field the client sends.
- Ownership: a logged-in customer still sees only their own orders.
- Rotation: each refresh token works once (the interview question).
More in Projects
Build "Project: secure ordering" 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.