Authentication
Proving who the caller is: the manager and provider chain, user details and password storage, form and basic login, anonymous authentication, events and logout. Interviewers walk this path because a custom authentication mechanism is a common real-world task.
part ofSpring Frameworkoverview, primer and where to startread it →on this pageshowhide
explore
- AuthenticationManager & ProviderManager5 questions
- PasswordEncoder & DelegatingPasswordEncoder5 questions
- Form Login & HTTP Basic5 questions
- Anonymous Authentication5 questions
- Authentication Events5 questions
- Logout Handling5 questions
questions
page 2 of 2Why is server-side logout largely ineffective for stateless JWT authentication, and how do you design real logout for it?
basics
~20 sA JWT is self-contained and validated by signature, not by server state, so invalidating a session does nothing — the token stays valid until it expires. Real logout needs short-lived tokens plus a server-side denylist or a rotated refresh token.
From a security standpoint, what does DaoAuthenticationProvider do to resist username enumeration and timing attacks, and how do UserDetailsPasswordService and status flags fit in?
basics
~20 sIt hides whether a username exists by converting UsernameNotFoundException to BadCredentialsException (hideUserNotFoundExceptions=true), and it runs a dummy password hash for unknown users so response times don't reveal valid usernames. Account status flags block disabled/locked accounts, and UserDetailsPasswordService lets it transparently upgrade weak password hashes on login.
In a stateless JWT REST API, is anonymous authentication useful, and when would you disable it? What are the trade-offs?
basics
~20 sAnonymous still works statelessly (it is per-request, not session-based) and keeps Authentication non-null, which simplifies code. You might disable it if you want a null context to signal 'no user' explicitly, but usually leaving it on is fine and harmless.
You have two AuthenticationProviders supporting the same token: a primary and a legacy fallback. A user's account is locked. Why might returning DisabledException/LockedException versus BadCredentialsException from the primary produce very different behavior, and how does that inform provider ordering and exception design?
basics
~20 sIf the primary throws an AccountStatusException (like LockedException), ProviderManager stops the chain immediately, so the legacy provider never runs and the user sees 'account locked'. If it throws BadCredentialsException, ProviderManager keeps going and the legacy provider may authenticate or reject, changing the outcome and the audit trail.
You must migrate a production user base from {bcrypt} to {argon2} with zero downtime and no forced password resets. How do you design this with Spring Security's password APIs?
basics
~20 sUse a DelegatingPasswordEncoder whose default encode id is argon2 but whose map still contains bcrypt for verification. Provide a UserDetailsPasswordService so that upgradeEncoding re-hashes each user to {argon2} on their next successful login. Old logins keep working throughout.
showing 31–35 of 35