MODULE 8 - Spring Security
Welcome to Module 8! πβ
Your application works perfectly... but anyone can access it! How do you protect your endpoints? How do you authenticate your users? How do you manage roles and permissions?
Spring Security is THE solution to secure your Spring Boot applications. This module teaches you how to protect your application like a pro.
π Learning Objectivesβ
By the end of this module, you will be able to:
- β Understand the architecture of Spring Security
- β Configure form and HTTP Basic authentication
- β Implement JWT authentication (stateless)
- β Integrate OAuth2 for social login (Google, GitHub)
- β Manage roles and permissions
- β Protect against CSRF, XSS, and other attacks
π Prerequisitesβ
Before starting this module, you should have:
- β Completed Modules 1 to 7
- β Mastered REST architecture
- β Understood basic database concepts
- β Be comfortable with Spring annotations
If these prerequisites are not met, go back to the previous modules!
πΊοΈ Module Planβ
Theoretical Part (5 chapters)β
Chapter 1: Introduction to Spring Securityβ
Duration: 45 minutes
Content:
- Why secure an application?
- Spring Security architecture
- Security filters (Filter Chain)
- Authentication vs Authorization
- SecurityContext and Principal
Why important? Understand the foundations before configuring
Chapter 2: Basic Authenticationβ
Duration: 1 hour
Content:
- Modern security configuration (Spring Security 6+)
- HTTP Basic and Form Login
- UserDetailsService
- PasswordEncoder (BCrypt)
- Customizing login/logout pages
Why important? The basis of all authentication
Chapter 3: JWT (JSON Web Tokens)β
Duration: 1h15
Content:
- What is JWT? Why use it?
- Structure of a token (Header, Payload, Signature)
- Token generation and validation
- Refresh Tokens
- Integration with Spring Security
Why important? Standard for modern REST APIs
Chapter 4: OAuth2 and Social Loginβ
Duration: 1 hour
Content:
- OAuth2 concepts (Authorization Code Flow)
- Client vs Resource Server
- Login with Google, GitHub, Facebook
- Spring Security OAuth2 Client configuration
- Customizing the flow
Why important? Modern UX, fewer passwords to manage
Chapter 5: Advanced Securityβ
Duration: 45 minutes
Content:
- CSRF protection
- CORS configuration
- Method-level Security (@PreAuthorize, @Secured)
- Roles vs Permissions (RBAC)
- Security headers
Why important? Defense in depth
Practical Part (4 Labs)β
Lab 8.1: Form Login Authenticationβ
Duration: 1h30
Objective: Secure an application with form authentication
You will:
- Configure Spring Security
- Create a custom login page
- Store users in a database
- Manage session and logout
Lab 8.2: REST API with JWTβ
Duration: 2h
Objective: Create a REST API secured by JWT
You will:
- Implement login/register endpoints
- Generate and validate JWT tokens
- Create an authentication filter
- Protect endpoints by role
Lab 8.3: Role and Permission Managementβ
Duration: 1h30
Objective: Implement a complete RBAC system
You will:
- Create roles (ADMIN, USER, MANAGER)
- Define granular permissions
- Use @PreAuthorize with expressions
- Test access
Lab 8.4: Social Login with Googleβ
Duration: 1h30
Objective: Integrate Google OAuth2 login
You will:
- Configure a Google Cloud project
- Integrate OAuth2 Client
- Retrieve user information
- Combine with local authentication
π Total Durationβ
| Type | Duration |
|---|---|
| Theory | 4h45 (5 chapters) |
| Practice | 6h30 (4 Labs) |
| Total | 11h15 (~3-4 sessions) |
π― What You Will Understandβ
Before this Moduleβ
// Your API is open to everyone! π±
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping
public List<User> getAllUsers() {
return userService.findAll(); // Anyone can see!
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.delete(id); // Anyone can delete!
}
}
After this Moduleβ
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping
@PreAuthorize("hasRole('USER')") // β Authenticated users
public List<User> getAllUsers() {
return userService.findAll();
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')") // β Admins only!
public void deleteUser(@PathVariable Long id) {
userService.delete(id);
}
}
You will know:
- β Who can access what
- β How to authenticate users
- β How to manage JWT tokens
- β How to integrate OAuth2
- β How to protect against attacks
π Why This Module Is Essentialβ
1. Security Is Not Optionalβ
Without security:
- Personal data theft π
- Unauthorized modification π
- Identity theft π
- GDPR fines (up to 4% of revenue) π
2. Highly Demanded Skillβ
Java/Spring job offers:
- "Spring Security experience required" β
- "Knowledge of JWT/OAuth2" β
- "Securing REST APIs" β
3. Common Interview Questionsβ
Typical questions:
- "Explain the difference between Authentication and Authorization"
- "How does JWT work?"
- "How do you protect against CSRF attacks?"
- "What is OAuth2 Authorization Code Flow?"
πΊοΈ Training Progressionβ
Module 8 = Make your application production-ready!
π How to Approach This Moduleβ
Theory BEFORE Practiceβ
-
Read the 5 theoretical chapters
- Chapter 1: Understand the architecture
- Chapter 2: Basic authentication
- Chapter 3: JWT (most used in API)
- Chapter 4: OAuth2 (social login)
- Chapter 5: Advanced security
-
Do the Labs in order
- Lab 8.1: Form Login (foundations)
- Lab 8.2: JWT (REST API)
- Lab 8.3: Roles and permissions
- Lab 8.4: OAuth2 Google
Recommended Timeβ
Intensive Option (2 days):
- Day 1 Morning: Chapters 1-2 + Lab 8.1
- Day 1 Afternoon: Chapter 3 + Lab 8.2
- Day 2 Morning: Chapters 4-5 + Lab 8.3
- Day 2 Afternoon: Lab 8.4 + Review
Progressive Option (3-4 sessions):
- Session 1: Chapters 1-2 + Lab 8.1
- Session 2: Chapter 3 + Lab 8.2
- Session 3: Chapters 4-5 + Lab 8.3
- Session 4: Lab 8.4 + Personal project
β Self-Assessmentβ
Before starting, answer:
- Do you know what "authentication" means? β Yes β No
- Do you know the difference with "authorization"? β Yes β No
- Have you ever heard of JWT? β Yes β No
- Do you know what OAuth2 is? β Yes β No
- Are you familiar with CSRF attacks? β Yes β No
If you answered "No" to any question β This module will teach you everything!
β οΈ Important Warningβ
Security is a critical field.
- NEVER copy security configurations without understanding them
- ALWAYS test your configurations
- Keep your dependencies up to date (security vulnerabilities)
- NEVER store passwords in plain text
π― Next Stepβ
Ready to secure your application?
β‘οΈ Start with Chapter 1: Introduction to Spring Security