Concept Explanation
Imagine a High-End Nightclub.
- The Bouncer (Spring Security): He stands at the door (
SecurityFilterChain). He checks your ID (Authentication) and makes sure you are allowed in (Authorization). - The ID Card (JWT / Token): Once you are inside, you don't show your passport every time you buy a drink. You wear a Wristband (A Token) that proves who you are and that you already paid.
- The Secret Memo (BCrypt): You never store passwords in plain text ("password123"). You hash them so that even if a thief steals the list, they just see a garbled mess of characters.
Key Security Concepts
- Authentication: "Who are you?" (Username/Password).
- Authorization: "What are you allowed to do?" (Admin vs User).
- CORS: Cross-Origin Resource Sharing. This prevents other random websites from calling your API without permission.
- CSRF: Cross-Site Request Forgery. Preventing hackers from tricking a user into clicking a link that deletes their account.
Best Practices
- Never Log Passwords: Ensure your logs don't accidentally contain user passwords.
- Use BCrypt: For password hashing. It's slow on purpose to prevent hackers from "guessing" millions of passwords per second.
- The Principle of Least Privilege: Only give users the permissions they absolutely need.
Practice Exercises
- The Salt & Pepper: Research what a "Salt" is in password hashing. Why does BCrypt add a different salt for every single user?
- JWT Inspection: Go to jwt.io. Paste a sample token and see how it decodes. Can you see the "Header", "Payload", and "Signature"?
- The 403 vs 401: What is the difference between a 401 Unauthorized error and a 403 Forbidden error? (Hint: One means "I don't know who you are", the other means "I know who you are, but you're not an Admin").