Section 17 · Stage 4: Build Applications
Spring Security & Authentication
Protect your APIs: password hashing, sign-in with JWT, roles and permissions, and the common security mistakes to avoid.
What you will be able to do
- Explain authentication vs authorisation and how the Spring Security filter chain works
- Store passwords safely with BCrypt and build sign-up and login endpoints
- Protect a REST API with stateless JWT authentication
- Restrict endpoints and data by role and by owner
- Configure CORS and avoid the common web security mistakes
- Secure ShopLite so customers and admins see only what they should
What you will learn
17.1Security Basics7 lessons
The core ideas you need before touching any security code. Language-neutral: the same ideas apply in Java, Python, JavaScript or any other backend.
17.2How Spring Security Works7 lessons
What changes when you add Spring Security 7 to a Spring Boot 4 app, and how each request is checked.
17.3Users & Password Storage7 lessons
Keep user accounts in the database and store passwords so a leak does not expose them.
17.4HTTP Basic & Sessions6 lessons
The simpler ways to sign in and why REST APIs usually move beyond them.
17.5JWT Authentication8 lessons
Issue and check signed tokens so the API stays stateless, using Spring Security's own OAuth2 resource server JWT support.
- JWT structure: header, payload, signature40 min
- Login endpoint that returns a token45 min
- Validating the token on each request50 min
- Expiry, clock skew and signing keys from configuration45 min
- Refresh tokens and logout (the trade-offs)50 min
- Where clients should store tokens40 min
- Practice: JWT authentication50 min
- Mini project: JWT login for ShopLite75 min
17.6Roles & Permissions7 lessons
Allow each user to do only what their role permits, and only on their own data.
17.7CORS & Browser Clients6 lessons
Let a frontend on another origin call your API safely.
17.8Common Security Mistakes8 lessons
The errors that turn up in real projects and code reviews, and how to avoid them.
- Secrets in code and Git history40 min
- Leaking data through entities and error messages40 min
- Missing ownership checks (IDOR)40 min
- Brute-force login and account lockout45 min
- Logging sensitive data40 min
- Keeping dependencies up to date40 min
- Practice: security code review45 min
- Mini project: ShopLite security review60 min
17.9Security Mini Project: Secure ShopLite3 lessons
Bring everything together: sign-up, JWT login and role-based access in the finished, documented ShopLite API.
