Spring Security · senior
Sessions or JWTs, and password hashing
JWTs are easy to check but hard to revoke. Sessions revoke at once but need shared state. Passwords need a slow, salted hash, and old hashes can be migrated on login.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- JWT: self-contained, checked without a database call, good between services and for mobile. It cannot be revoked before it expires without extra state.
- Sessions: an opaque id in an HttpOnly, Secure, SameSite cookie, with state on the server (Spring Session with Redis for many pods). Instant revocation.
- Browsers: do not keep tokens in localStorage, where any XSS can read them. The BFF pattern: the browser holds only a session cookie for the backend-for-frontend, which holds the tokens.
- Access tokens short (5 to 15 minutes). Refresh tokens rotated on use and stored server-side so they can be revoked.
- SHA-256 is fast, so attackers try billions of guesses a second. No salt means identical passwords have identical hashes and precomputed tables work.
PasswordEncoderFactories.createDelegatingPasswordEncoder(): each stored value starts with an id ({bcrypt}...,{argon2}...), so many formats can live together.- Migrate on login: prefix old rows with
{sha256}with a verify-only encoder, and after a successful login re-hash with bcrypt. For users who never log in, wrap the old hash in a batch:bcrypt(sha256(password)). - Use bcrypt with a work factor tuned on your hardware, or Argon2id. Rate-limit logins. If the table might have leaked, treat it as a breach and force resets.
The red pen
Common traps: mistake → fix
Mistake: JWTs are always better than sessions
Fix: revocation is hard.
Mistake: A salted SHA-256 is good enough
Fix: it is still fast; use bcrypt or Argon2id.
Mistake: Everyone must reset the password to migrate
Fix: a delegating encoder upgrades on login.
Easy to hard
Questions interviewers ask, easy to hard
- Stateless JWTs or server sessions for a web and mobile app?
- Where should a browser keep tokens?
- How do you move unsalted SHA-256 passwords to a proper hash without forcing resets?
- What is a delegating password encoder?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.