Quick revision
The short answer
TokenBasedRememberMeServices puts the username, an expiry time and a keyed hash in the cookie, so anyone holding the cookie can log in until it expires; only a password change or a new key invalidates it. PersistentTokenBasedRememberMeServices stores a random series and token in a database and changes the token on every use, so a stolen cookie is detected when the series shows up with a wrong token, and tokens can be revoked on the server. The persistent one is safer, and either way remember-me logins should not reach sensitive operations without a fresh login.
What to remember
In this answer, we compared Spring Security's two remember-me implementations. The token-based one keeps no server state and puts a keyed hash of the username, expiry and password in the cookie, so a stolen cookie works until it expires and only a password change revokes it. The persistent one stores a series and a rotating token in a database, detects theft when an old token appears for a known series, removes the user's tokens, and allows server-side revocation. Either way, sensitive operations should require a fresh login through fullyAuthenticated().
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.
TokenBasedRememberMeServices puts the username, an expiry time and a keyed hash in the cookie, so anyone holding the cookie can log in until it expires; only a password change or a new key invalidates it. PersistentTokenBasedRememberMeServices stores a random series and token in a database and changes the token on every use, so a stolen cookie is detected when the series shows up with a wrong token, and tokens can be revoked on the server. The persistent one is safer, and either way remember-me logins should not reach sensitive operations without a fresh login.
Why this question is asked
Remember-me is the feature behind the 'keep me signed in' checkbox. It turns a short session into weeks of access, which makes it valuable to users and to attackers who steal cookies. Interviewers ask this comparison because the two Spring Security implementations look alike in configuration but behave very differently when a cookie is stolen. In this answer, we will recap what remember-me does, take each implementation apart, see how the persistent one detects theft, and finish with the rule that remember-me should never be enough for sensitive actions.
1A Quick Recap of Remember-Me
A normal login lives in the HTTP session, which ends after, say, 30 minutes of inactivity or when the browser is closed. Remember-me adds a second, long-lived cookie, by default called remember-me. When a request arrives without a valid session but with that cookie, Spring Security's RememberMeAuthenticationFilter asks a RememberMeServices implementation to check the cookie and, if it is valid, logs the user in again with a RememberMeAuthenticationToken.
So the remember-me cookie is a credential. Whoever has it can log in as the user for its whole lifetime, often two weeks. The difference between the two implementations is what the cookie contains and what the server can do about a stolen one.
2How TokenBasedRememberMeServices Works
The token-based implementation stores nothing on the server. The cookie holds the username, an expiry time, the hash algorithm name and a hash computed over the username, the expiry, the user's current password hash and a secret key known only to the application. On each visit the server loads the user, recomputes the hash and compares.
Notice the consequences. Because the user's password hash is part of the hash, changing the password invalidates every remember-me cookie, which is the only built-in way to revoke them. Because the server keeps no record, it cannot tell a stolen copy from the original: an attacker who copies the cookie can log in from anywhere until the expiry, and the server has no list to remove it from. Changing the key logs out every user at once.
3How PersistentTokenBasedRememberMeServices Works
The persistent implementation keeps state in a database table, persistent_logins, with the username, a series, a token and the time it was last used. The cookie holds only the series and the token, two random values. The series stays the same for the life of that login on that device; the token changes every time the cookie is used.
| Token-based | Persistent | |
|---|---|---|
| Stored on the server | nothing | series, token, last used |
| Cookie contains | username, expiry, keyed hash | random series and token |
| Changes on each use | no | yes, the token is replaced |
| Stolen cookie | works until expiry, undetected | detected when both copies are used |
| Revoke one user | only by password change | delete the user's rows |
Since every use replaces the token, a given cookie value works only until the next time it is used. The rows also give the server control: deleting a user's rows revokes their remember-me logins immediately, on one device or all of them.
4How the Persistent Version Detects Theft
Suppose an attacker copies Anna's cookie S1:T2 and uses it first. The server finds series S1, sees that the token matches, logs the attacker in and rotates the token to T3. Anna's browser still holds S1:T2. On her next visit the server finds series S1 but with the wrong token. A real series with a wrong token can only mean that someone else used this cookie, so Spring Security throws a CookieTheftException and deletes all of Anna's remember-me tokens, which also logs the attacker out of the remember-me login.
6 messages in order. Play them, or step through them one at a time.
The theft is noticed at Anna's next visit. If she uses the cookie first, the attacker's copy stops working.
5Remember-Me Is Not Enough for Sensitive Operations
Even the persistent approach does not stop an attacker who uses the cookie before the owner does. So remember-me logins should get ordinary access only. For changing the password or email, payments or anything similar, we require a login in the current session. Spring Security distinguishes the two: fullyAuthenticated() allows only users who logged in with their credentials in this session, and a remember-me user who hits such a URL is sent to the login page.
Notice that the user is not rejected; they are asked to prove who they are again, which is the right trade between convenience and safety.
6Follow-up questions
Is the token-based implementation ever the right choice?
For a low-risk application without a convenient database, it can be acceptable, provided the key is long and secret, the lifetime is short and sensitive actions require a fresh login. When a database is available, the persistent implementation costs little and gives revocation and theft detection.
What should we do with persistent_logins rows over time?
Rows for expired cookies should be cleaned up regularly, and we can show users their remembered devices from the table so they can revoke one. On password change we delete all rows for the user.
Why does the persistent approach keep the series fixed?
The series identifies one login on one device. Keeping it fixed lets the server recognise the same cookie lineage after the token has moved on, which is exactly what makes the wrong-token case a theft signal rather than an unknown cookie.
7Conclusion
In this answer, we compared Spring Security's two remember-me implementations. The token-based one keeps no server state and puts a keyed hash of the username, expiry and password in the cookie, so a stolen cookie works until it expires and only a password change revokes it. The persistent one stores a series and a rotating token in a database, detects theft when an old token appears for a known series, removes the user's tokens, and allows server-side revocation. Either way, sensitive operations should require a fresh login through fullyAuthenticated().
Open with
Both implement the keep-me-signed-in cookie, but they handle theft very differently. TokenBasedRememberMeServices stores nothing on the server. The cookie holds the username, an expiry time and a hash over the username, expiry, the user's password hash and a secret key.
Then, if they want more
The server just recomputes the hash. That means anyone who copies the cookie can log in until it expires, and the only ways to revoke it are a password change, because the password is in the hash, or changing the key for everyone.
PersistentTokenBasedRememberMeServices stores a random series and token per login in a table like persistent_logins, and the cookie only holds those two random values. Every time the cookie is used, the token is replaced.
If a stolen cookie is used, the next request from the other party arrives with a known series but an old token, so Spring Security throws a CookieTheftException and deletes all of the user's tokens. Because the tokens live in the database, I can also revoke them on the server, per device or for the whole user.
So the persistent one is safer and I would use it whenever there is a database. With either, I would mark sensitive operations such as payments or password changes as fullyAuthenticated, so a remember-me login has to log in again before doing them.
About this answer
- Last updated
- 12 November 2026
- 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 "Compare Spring Security's two remember-me implementations: TokenBasedRememberMeServices...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/47cfeeec4f/