Torna al blog
Cybersecurity

Spring Boot Security: Cookies, Stateful Sessions, and JWT — How Client-Server Authentication Really Works

HttpOnly cookies, Spring Session, and stateless JWT: how I learned to truly understand authentication in Spring Boot, not just make it work.

The Problem with Authentication That 'Works But I Don't Know Why'

When I started with Spring Boot, I implemented authentication by following tutorials and copying configurations. It worked, but I didn't understand what was happening underneath. What's inside a session cookie? How does the server know you're still authenticated? What makes a JWT different from a traditional session?

I built this course to answer exactly these questions — not with theoretical slides, but with practical labs where you can watch cookies in the browser DevTools, inspect tokens, and understand why certain designs are more secure than others.

HTTP Cookies: The Foundation

Everything starts with cookies. A cookie is simply an HTTP header — Set-Cookie in the server response, Cookie in the client request. It's the mechanism that transforms HTTP (stateless by definition) into something that can remember who you are. Critical security attributes: HttpOnly (blocks XSS), Secure (HTTPS only), SameSite (CSRF protection).

Stateful Sessions: The Server Remembers Everything

With Spring Session, the server maintains a map of active sessions in memory or Redis. The cookie contains only a session ID, the server does the lookup and finds your data. Simple to implement, but requires shared state across instances — typically via Spring Session + Redis.

JWT: Stateless Authentication

JWT flips the paradigm: instead of keeping state on the server, state is encoded in the token itself. Three Base64 parts: Header (algorithm), Payload (claims: userId, roles, expiration), Signature (HMAC or RSA). The payload is readable by anyone but cannot be modified without invalidating the signature. Never put passwords or sensitive data in the payload.

Spring Security 6: The Configuration That Scares Everyone

Recent versions (6.x) dropped WebSecurityConfigurerAdapter in favor of SecurityFilterChain beans. If you're following old tutorials, you have code that no longer compiles. The course shows both architectures — session-based and JWT — correctly configured with Spring Security 6, including custom token validation filters and CORS for separate frontends.

What's in the Course

The Spring Boot Security Lab has two complete series: stateful with cookies/sessions, and stateless with JWT — 42 lessons, all with real downloadable code.

Condividi