About Us Contact Us Write for Us Advertise
Home > Java > Spring Boot Security & JWT Authentication (Beginner Guide)
Java

Spring Boot Security & JWT Authentication (Beginner Guide)

Understand Spring Security and JWT the easy way: authentication vs authorization, the stateless JWT flow, sending Bearer tokens, role-based access, and password hashing.

Shiv Pandey
Shiv Pandey
Oct 06, 2026 | 125 views
Spring Boot Security & JWT Authentication (Beginner Guide)

Almost every real application needs to answer two questions: "who are you?" (authentication) and "are you allowed to do this?" (authorization). Spring Security handles both — and paired with JWT tokens, it's the standard way to secure modern REST APIs. Security can feel intimidating, so this lesson focuses on the core ideas and the big picture, giving you a solid mental model to build on.

Two words you must not confuse

  • Authentication = who are you? Verifying identity, usually with a username and password (or a login).
  • Authorization = what are you allowed to do? Checking permissions/roles once we know who you are.

You authenticate first (prove identity), then authorization decides what that identity can access. An admin can delete users; a regular user cannot — same login system, different permissions.

The problem with traditional sessions for APIs

Classic web apps store a "session" on the server after you log in, and hand your browser a cookie. That works, but it's awkward for REST APIs — especially when serving mobile apps or multiple servers, because the server has to remember every logged-in user. The modern answer for APIs is stateless authentication with tokens, where the server remembers nothing between requests.

Enter JWT (JSON Web Token)

A JWT is a compact, digitally-signed token that proves who you are. The flow is beautifully simple:

Client Server 1. login (user + password) 2. returns a signed JWT 3. later requests send the JWT 4. server verifies signature & responds
  1. The client logs in with username + password.
  2. The server verifies them and returns a signed JWT.
  3. For every later request, the client sends the JWT in the Authorization header.
  4. The server verifies the signature and knows who you are — no session storage needed.

Because the token is cryptographically signed, the server can trust it without remembering anything. That's what "stateless" means, and it's why JWT scales so well.

Sending the token

The client attaches the JWT to each request using the standard Bearer scheme:

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

On the server, a small filter reads this header, validates the token, and tells Spring Security who the current user is — before your controller even runs.

Restricting access with roles

Once Spring knows who you are, you control what they can do. You configure URL rules, or annotate methods directly:

// only admins can reach this endpoint
@PreAuthorize("hasRole('ADMIN')")
@DeleteMapping("/api/users/{id}")
public void delete(@PathVariable Long id) { ... }

Or in a central security config, you declare rules like "anyone can hit /api/login, but /api/admin/** requires the ADMIN role." Spring enforces them for you on every request.

Security essentials, never skip these: (1) always hash passwords with BCrypt before storing them — never store plain text; Spring Security's PasswordEncoder makes this one line. (2) Keep your JWT signing secret in an environment variable (see the config lesson), never in code. (3) Give tokens a sensible expiry so a stolen one doesn't last forever.

Don't be intimidated — a realistic note

Spring Security has a reputation for being complex, and honestly, the first time it clicks takes patience. The good news: you don't need to master every internal detail to use it well. Understand the flow above — login → get token → send token → server verifies → role check — and you understand 90% of what matters. The wiring is mostly boilerplate you set up once.

Security was the topic I was most nervous about, and I won't pretend the first setup was painless — Spring Security's layers took me a few attempts to truly grasp. But the JWT flow itself is genuinely simple once you draw it out, which is exactly why I drew it above. This website secures its admin panel with Spring Security: passwords are BCrypt-hashed, routes are role-protected, and access is checked on every request. My advice for your first secured app: get the login-returns-token, requests-send-token loop working end to end before worrying about the finer points. Nail the flow, and the details follow.

Key takeaways

  • Authentication = who you are; authorization = what you're allowed to do.
  • JWT enables stateless auth: log in → receive a signed token → send it on each request → server verifies it.
  • Send tokens as Authorization: Bearer <token>; restrict access with role rules or @PreAuthorize.
  • Always hash passwords (BCrypt), keep the JWT secret in env vars, and set token expiry.

← Previous: Spring Lesson 9 — Configuration & Profiles
Next: Spring Lesson 11 — Testing →
↑ Back to the Spring Boot roadmap

Related Articles

Spring vs Spring Boot: The Difference Explained (Beginner Guide)
Java

Spring vs Spring Boot: The Difference Explained (Beginner Guide)

Spring Boot Project Ideas (Beginner to Advanced)
Java

Spring Boot Project Ideas (Beginner to Advanced)

Deploying a Spring Boot App to Production (Beginner Guide)
Java

Deploying a Spring Boot App to Production (Beginner Guide)

Dockerizing a Spring Boot App (Beginner Guide)
Java

Dockerizing a Spring Boot App (Beginner Guide)