Quick revision
The short answer
In a magic-link login the user enters an identifier, the server creates a short-lived, single-use, unguessable token and sends a link containing it, usually by email; opening the link submits the token, and Spring Security checks it with a OneTimeTokenService and logs the user in. We enable it with http.oneTimeTokenLogin(...) and must provide a OneTimeTokenGenerationSuccessHandler that delivers the link. The default token service keeps tokens in memory, so a cluster needs the JDBC-backed one.
What to remember
In this answer, we followed a magic-link login: the user enters an identifier, the server stores a short-lived, single-use random token and emails a link, and opening the link submits the token, which a OneTimeTokenService validates before the user is logged in. We configured http.oneTimeTokenLogin(...), wrote the required OneTimeTokenGenerationSuccessHandler with a fixed base URL, replaced the in-memory token service with the JDBC one for clusters, and covered expiry, single use, randomness and rate limits.
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.
In a magic-link login the user enters an identifier, the server creates a short-lived, single-use, unguessable token and sends a link containing it, usually by email; opening the link submits the token, and Spring Security checks it with a OneTimeTokenService and logs the user in. We enable it with http.oneTimeTokenLogin(...) and must provide a OneTimeTokenGenerationSuccessHandler that delivers the link. The default token service keeps tokens in memory, so a cluster needs the JDBC-backed one.
Why this question is asked
Magic links are a popular way to offer login without a password: the user proves they own an email address, and that is the login. Spring Security 6.4 made this a built-in feature, which means interviewers can now ask how it works and what we must configure ourselves. In this answer, we will walk through the flow step by step, configure it in Spring Security, write the handler that sends the link, deal with storage in a cluster, and look at the properties that keep the tokens safe. As with any newer feature, check your Spring Security version for exact API names.
1How a Magic-Link Login Works
The user opens the login page and types their username or email. The server generates a token, a long random string, stores it with the username and an expiry time a few minutes away, and sends the user a link that contains it. The user opens their email and clicks the link. The browser brings the token back to the server, which looks it up, checks that it has not expired and has not been used, deletes it, and logs the user in as the stored username.
Notice where the security comes from. Only someone who can read the user's email receives the token, the token is too random to guess, and it works once for a few minutes. The login is therefore as strong as the user's email account, which is the main trade-off of magic links.
7 messages in order. Play them, or step through them one at a time.
2Configuring oneTimeTokenLogin
In Spring Security 6.4 and later, one call adds the filters for both halves of the flow. By default the token is requested with a POST to /ott/generate (the default login page shows a form for it when one-time token login is enabled), and the token is submitted with a POST to /login/ott. Opening the link with a GET shows a small page that submits the token when the user presses the sign-in button.
We still need a UserDetailsService, because after the token is validated Spring Security loads the user to build the Authentication with their authorities. And one bean is required: without a handler that delivers the token, the application fails to start, because Spring Security has no idea whether we want email, SMS or something else.
3Writing the Handler That Sends the Link
OneTimeTokenGenerationSuccessHandler is called after a token has been generated. It receives the OneTimeToken with its value, username and expiry, builds the link, sends it, and then responds to the browser, typically with a redirect to a page that says 'check your email'.
Two details matter. We build the link from a fixed, configured base URL rather than from the incoming Host header, so an attacker cannot make us send links to their own domain. And the response is the same whether the user exists or not, so the form cannot be used to discover which email addresses have accounts.
4Storing Tokens in a Cluster
The default OneTimeTokenService is InMemoryOneTimeTokenService. It keeps tokens in a map inside one JVM. With two or more instances, the link may be generated on instance 1 and clicked on instance 3, which has never seen the token, so the login fails. Tokens are also lost when an instance restarts.
For a cluster we switch to JdbcOneTimeTokenService, which stores tokens in a database table that all instances share. Spring Security ships the table definition with the class; check your version for the exact schema file.
Expired tokens should be cleaned up regularly; the JDBC service has support for removing expired rows, but check how your version schedules it.
5What Makes the Tokens Safe
Three properties are essential. Tokens must expire quickly; the default lifetime is five minutes, which is long enough to open an email and short enough to limit the damage of a forwarded or leaked message. Tokens must be single use: consuming one deletes it, so a link that sits in a mailbox cannot be replayed later. And tokens must be unguessable: the default generator uses a random UUID, and anything custom must use a secure random source with enough bits.
Around the token we add rate limits on /ott/generate per user and per IP, so nobody can flood a victim's inbox or our mail provider, and logging of generation and redemption so we can investigate complaints. Corporate email scanners that open links before the user does are a known problem with single-use links, which is one reason the link opens a page with a button instead of logging in on the GET.
6Follow-up questions
How is a magic link different from a password reset link?
Technically they are almost the same: a single-use, short-lived token sent by email. The difference is what happens after: a reset link lets the user set a password, while a magic link logs them in directly. Both make the email account the key to our account.
Can the token be sent as a code instead of a link?
Yes. The handler decides how to deliver it, so it can send the token, or a shorter code mapped to it, by SMS or show it in an email to type in. A short code must have stricter attempt limits, because it has fewer possible values.
Is a magic link a second factor?
On its own it is a single factor: possession of the email account. Combined with a password it can act as a second step, and Spring Security 7's multi-factor support can require both, but it is weaker than an authenticator app or a passkey.
7Conclusion
In this answer, we followed a magic-link login: the user enters an identifier, the server stores a short-lived, single-use random token and emails a link, and opening the link submits the token, which a OneTimeTokenService validates before the user is logged in. We configured http.oneTimeTokenLogin(...), wrote the required OneTimeTokenGenerationSuccessHandler with a fixed base URL, replaced the in-memory token service with the JDBC one for clusters, and covered expiry, single use, randomness and rate limits.
Open with
A magic-link login works like this: the user enters their username or email, the server generates a random token, stores it with the username and a short expiry, and emails a link containing it.
Then, if they want more
When the user opens the link, the token comes back to the server, which checks it exists, has not expired, deletes it so it is single use, and logs in the stored user. In Spring Security 6.4 and later I enable it with http.oneTimeTokenLogin.
By default the token is requested by a POST to /ott/generate and submitted by a POST to /login/ott; opening the link shows a page with a sign-in button that does the POST. I still need a UserDetailsService to load the user after the token is validated.
The one bean I must provide is a OneTimeTokenGenerationSuccessHandler, which builds the link and sends it; I build it from a configured base URL, not the Host header, and respond the same way whether or not the user exists. The default token service is in memory, which breaks with several instances or restarts, so in a cluster I use JdbcOneTimeTokenService.
The tokens must be short-lived, five minutes by default, single use and unguessable, and I would add rate limits on generation and log redemptions. I would check the exact API names for our version.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Security 6, Spring Security 7
- 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 "Spring Security 6.4 added one-time token login. Explain how a magic-link login flow...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/1e13bfa13f/