AI Hints
Home›Spring Boot Tutorial›Spring Security Basics, Locked by Default and SecurityFilterChain
Spring Boot · Lesson 91 of 120

Spring Security Basics

Who are you?.

⏱ 2 min read▶ Video at 2:01:30🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Right now, anyone can delete our books. A real API must know who is calling, and what they're allowed to do. Spring Security handles both.

  • Anyone can delete books
  • Who is calling?
  • What may they do?

Two questions

Authentication asks, who are you? It checks a user name, and password, or a token. Authorization asks, what are you allowed to do? It checks roles, like user, or admin. Authentication always comes first.

Authentication

  • Who are you?Password or tokenFails with 401

Authorization

  • What may you do?Roles and rulesFails with 403

The filter chain

Every request passes through a chain of security filters, before it reaches a controller. One filter reads the credentials. Another checks them, against the known users. Another checks the rules for this URL. Only then does the request reach our controller.

  1. Request
  2. Read credentials
  3. Check the user
  4. Check the rules
  5. Controller

401 and 403

Four oh one means unauthorized. In practice, it means, we don't know who you are. Four oh three means forbidden. We know who you are, but you're not allowed.

StatusMeaningFix
401 Unauthorizednot logged insend credentials
403 Forbiddenlogged in, not allowedneed another role

Summary

Authentication checks who you are, and authorization checks what you may do. Security filters run before every controller. Next, what happens when we add Spring Security.

Key takeaways
  • Authentication
  • Authorization
  • 401 vs 403
Next up: Locked by Default — secure out of the box