Spring Boot services · Chapter 24 of 41
Spring Security basics
By the end of this chapter you can lock an API behind a login, leave the public parts open, store passwords safely, and explain 401, 403 and CSRF. 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
Ravi has news from Mumbai: someone at CSMT found the orders URL and placed forty orders of bun maska, and nobody paid. Tapri Cloud answers anyone who asks. Meera wants the menu to stay public and orders to be for staff only, today.
A login check in every controller method repeats itself, and the one method someone forgets is open to the world. The check belongs in front of all controllers, in one place.
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: keep the menu open to everyone
In SecurityConfig's filter chain, let anyone call GET /menu without a login: it answers 200 with the menu (Masala chai first). Permit /error to everyone too, so errors are not turned into 401.
Every other request will need a login (task 2), so put these rules before that one.
- Task 2
SecurityConfig.java: put orders behind a login, with a Basic challenge
Make every request other than GET /menu (and /error) need a login, and switch on HTTP Basic: GET /orders without a login then answers 401 Unauthorized with a WWW-Authenticate header that starts with Basic.
- Task 3
SecurityConfig.java: let ravi log in with HTTP Basic
Add the user ravi, with the password masala@123 and the role STAFF, to the UserDetailsService. GET /orders with HTTP Basic as ravi then answers 200 with the text "Orders for ravi".
A wrong password (cutting@123) or a user that does not exist (kabir) answers 401. Needs task 2's HTTP Basic.
- Task 4
SecurityConfig.java: store ravi's password as a BCrypt hash
Add a PasswordEncoder bean that hashes with BCrypt, and store ravi's password only as the hash it makes: the stored password is a 60-character BCrypt hash starting with $2a$ (or {bcrypt}$2a$), never masala@123 itself.
The encoder must accept masala@123 against the stored hash, and two encodings of the same password must differ.
- Task 5 · interview question
Interview: turn CSRF off for this stateless API
Turn CSRF protection off in the filter chain, so POST /orders with HTTP Basic as ravi and a JSON body {"item": "Masala chai", "quantity": 2} answers 201 with the order (placedBy ravi, quantity 2), with no CSRF token.
A POST without a login must still answer 401. Be ready to explain why this API may turn CSRF off, and when it would matter.
What you will learn
- The security filter chain: rules every request passes before any controller runs.
- 401 and 403: "who are you?" and "I know, and no".
- Users and BCrypt: a UserDetailsService and a PasswordEncoder.
- CSRF: what it protects, and when an API turns it off (the interview question).
More in Security
Build "Spring Security basics" 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.