Quick revision
The short answer
Spring Security 6.4 and later give us passkey support with http.webAuthn() and one-time token login with http.oneTimeTokenLogin(), and Spring Security 7 adds multi-factor support where each completed factor adds an authority and rules can require several of them (check your version for the exact API). On older versions we build a partial authentication state after the password that only allows the second-factor page. Either way we build the code delivery or TOTP verification, encrypted secret storage, recovery codes and attempt limits, or we delegate MFA to an identity provider and require it through acr or amr claims.
What to remember
In this answer, we designed MFA for a Spring Boot application. Spring Security 6.4 and later provide passkeys through webAuthn() and one-time token login, and Spring Security 7 adds multi-factor rules based on factor authorities; on older versions a partial authentication state after the password allows only the second-factor page. We build TOTP verification, encrypted secret storage, recovery codes, attempt limits and safe enrolment ourselves, or delegate MFA to an identity provider and check acr or amr claims.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Security 6.4 and later give us passkey support with http.webAuthn() and one-time token login with http.oneTimeTokenLogin(), and Spring Security 7 adds multi-factor support where each completed factor adds an authority and rules can require several of them (check your version for the exact API). On older versions we build a partial authentication state after the password that only allows the second-factor page. Either way we build the code delivery or TOTP verification, encrypted secret storage, recovery codes and attempt limits, or we delegate MFA to an identity provider and require it through acr or amr claims.
Why this question is asked
Multi-factor authentication is now expected in most business applications, and administrators are the accounts attackers want most. Interviewers ask this design question to see whether we know what the framework gives us today, what it does not, and how to stitch the pieces together without leaving a gap where a half-logged-in user can reach the application. In this answer, we will recap the factors involved, look at what Spring Security provides in recent versions, design the flow for both user groups, cover the approach for older versions, add the parts we must build ourselves, and consider delegating the whole thing to an identity provider.
1A Quick Recap of the Factors
A factor is one kind of proof. A password is something the user knows. A one-time code is proof of something they have: a phone with an authenticator app that generates TOTP codes (time-based one-time passwords, six digits that change every 30 seconds), or an email inbox that receives a code. A passkey is a key pair stored on a device or in a password manager; the private key never leaves it, and it signs a challenge from our site, which makes it resistant to phishing because the browser only uses it on our real domain.
Our requirement has two groups. All users need password plus one-time code. Administrators must use passkeys, so a phished password and code cannot reach the admin pages.
2What Spring Security Provides
Since Spring Security 6.4 there is built-in WebAuthn support, http.webAuthn(...), which handles passkey registration and login ceremonies (it needs the WebAuthn4J library on the classpath and a durable repository for credentials). There is also one-time token login, http.oneTimeTokenLogin(...), which generates a short-lived token and lets us deliver it by email or SMS as a link or code.
Spring Security 7 adds first-class multi-factor authentication: each successful login mechanism contributes an authority that records the factor, and authorisation rules can demand that several factors are present before granting access. That is exactly our case: a user must have both the password factor and the one-time token factor, and admin URLs must have the WebAuthn factor. The annotation and class names are new, so check your version's reference guide for the exact API rather than relying on memory.
Notice what is not in the box: TOTP from an authenticator app. One-time token login sends tokens we generate; verifying codes from an authenticator app is something we add with a small TOTP library and our own verification endpoint.
3The Flow We Want
For a normal user, the password is the first step. After it succeeds the user is not yet logged in; they are in a state where only the second-factor page is reachable. They enter the code, and only then does the application treat them as fully authenticated. For an administrator, the admin area requires a passkey login, and we can also require the passkey to be the method used in the current session, so a password-plus-code login cannot reach /admin even for an admin account.
- Password correct?YesPartial state: only /mfa reachableNoReject, count the failure
- Code correct and within limits?YesFull authenticationNoCount the attempt, lock after 5
- Request under /admin?YesCheck for the passkey factorNoNormal access
- Passkey factor present?YesAdmin accessNoAsk for a passkey login
4 steps in order. Play them, or step through them one at a time.
4The Partial Authentication Approach on Older Versions
Before Spring Security 7, a common pattern is to make the password step produce a temporary authentication with a single authority such as FACTOR_PASSWORD, never the real roles. A custom AuthenticationSuccessHandler sends the user to /mfa. Authorisation rules allow that authority only on /mfa. When the code is verified, our controller replaces the Authentication in the SecurityContext with a full one carrying the user's real authorities, and saves it.
The important detail is that the partial state grants nothing else. The loader must not put the real roles on the first-step authentication, or a user could skip /mfa by typing another URL. AuthorizationManagers.allOf() combines the two checks, so the admin area needs both the role and the passkey factor. And we must change the session id after the second step as well, just as after the first, so a session known before the code was entered does not inherit the full login.
5What We Build Ourselves
Whatever version we use, several pieces are ours. TOTP secrets, the shared key behind each user's authenticator app, are stored encrypted with a key held outside the database, because anyone with the secret can generate valid codes. Each user gets a set of single-use recovery codes, stored hashed like passwords, for the day the phone is lost. Code attempts are rate limited: a six-digit code has a million values, so five attempts per code and a lock after repeated failures keep guessing hopeless. Enrolment needs care too: a new factor should be added only after the user proves an existing one, and every change should send a notification.
6Delegating MFA to the Identity Provider
Many organisations do not build any of this. They put MFA in the identity provider, which already supports TOTP, push approval, passkeys and recovery, and the Spring application logs users in with OIDC. The application then checks what the provider did. OIDC defines the acr claim (the authentication context class, a level agreed with the provider) and the amr claim (the methods used, such as pwd, otp or hwk). The application can request a level for admin pages and check the claim before allowing access.
This is usually the best long-term choice for a company with several applications, because MFA, recovery and fraud controls are built once and maintained by people who specialise in them.
7Follow-up questions
Why passkeys for administrators instead of password plus code?
Codes can be phished: a fake login page asks for the password and the code and replays both within 30 seconds. A passkey signs a challenge bound to our real origin, so a fake site cannot use it. For the accounts with the most power, that resistance is worth the extra setup.
Is an emailed one-time code a real second factor?
It proves access to the inbox, which is weaker than a dedicated device because email accounts are often protected by the same reused password. It is better than nothing for ordinary users, and an authenticator app or passkey is better still.
8Conclusion
In this answer, we designed MFA for a Spring Boot application. Spring Security 6.4 and later provide passkeys through webAuthn() and one-time token login, and Spring Security 7 adds multi-factor rules based on factor authorities; on older versions a partial authentication state after the password allows only the second-factor page. We build TOTP verification, encrypted secret storage, recovery codes, attempt limits and safe enrolment ourselves, or delegate MFA to an identity provider and check acr or amr claims.
Open with
I would start from what the framework gives me. Since Spring Security 6.4 there is passkey support through http.webAuthn, which handles registration and login ceremonies with a durable credential repository, and one-time token login for emailed codes or links.
Then, if they want more
Spring Security 7 adds real multi-factor support, where each completed login mechanism adds a factor authority and rules can require several, so I would require the password and one-time code factors for everyone and the WebAuthn factor for admin URLs; I would check the exact API for our version.
On an older version I would build a partial authentication state: the password step yields only a FACTOR_PASSWORD authority and redirects to the code page, every other URL needs the full factor, and after the code is verified I replace the authentication with one carrying the real roles and change the session id.
Things I build myself are TOTP verification with a library if we use authenticator apps, encrypted storage of TOTP secrets, hashed single-use recovery codes, rate limits of about five attempts per code with locking, and safe enrolment that needs an existing factor and sends a notification.
In a company with many apps I would rather delegate MFA to the identity provider and check acr or amr claims in the OIDC token.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 7, Spring Security 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design multi-factor authentication for a Spring Boot web application where all users...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/f0f7676ebd/