The new Certification Class of Learn Spring Security is out:


1. Overview

This tutorial will show how to enable and configure Remember Me functionality in a web application with Spring Security. Setting up the MVC application with security and a simple form login has already been discussed.

The mechanism will be able to identify the user across multiple sessions – so the first thing to understand is that Remember Me only kicks in after the session times out. By default, this happens after 30 minutes of inactivity, but timeout can be configured in the web.xml.

2. The Security XML Configuration

The security namespace will be used to configure the mechanism entirely:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="" 

    <http use-expressions="true">
        <intercept-url pattern="/anonymous*" access="isAnonymous()" />
        <intercept-url pattern="/login*" access="permitAll" />
        <intercept-url pattern="/**" access="isAuthenticated()" />

        <form-login login-page='/login.html' 
          authentication-failure-url="/login.html?error=true" />
        <logout delete-cookies="JSESSIONID" />

        <remember-me key="uniqueAndSecret"/>

                <user name="user1" password="user1Pass" authorities="ROLE_USER" />
                <user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />


As you can see, the basic configuration using <remember-me> element is extremely simple while remaining very flexible though additional namespace options. The key is important here – it is a private value secret for the entire application and it will be used when generating the contents of the token.

Additionally, the time the token is valid can be configured from the default of 2 weeks to – for example – one day:

<remember-me key="uniqueAndSecret" token-validity-seconds="86400"/>

3. The Login form

The login form is similar to the one we used for form login:



    <form name='f' action="login" method='POST'>
                <td><input type='text' name='username' value=''></td>
                <td><input type='password' name='password' /></td>
                <td>Remember Me:</td>
                <td><input type="checkbox" name="remember-me" /></td>
                <td><input name="submit" type="submit" value="submit" /></td>


Notice the newly added checkbox input – mapping to remember-me. This added input is enough to log in with remember me active.

Unfortunately this default path cannot be changed via the namespace in Spring 3.1 – the option is available starting with Spring 3.2 – as follows:

<remember-me remember-me-parameter="custom-remember-me" />

The mechanism will create an additional cookie – the “remember-me” cookie – when the user logs in.

The Remember Me cookie contains the following data:

  • username – to identify the logged in principal
  • expirationTime – to expire the cookie; default is 2 weeks
  • MD5 hash – of the previous 2 values – username and expirationTime, plus the password and the predefined key

First thing to notice here is that both the username and the password are part of the cookie – this means that, if either is changed, the cookie is no longer valid. Also, the username can be read from the cookie.

Additionally, it is important to understand that this mechanism is potentially vulnerable if the remember me cookie is captured. The cookie will be valid and usable until it expires or the credentials are changed.

5. In Practice

To easily see the remember me mechanism working, you can:

  • log in with remember me active
  • wait for the session to expire (or remove the JSESSIONID cookie in the browser)
  • refresh the page

Without remember me active, after the cookie expires the user should be redirected back to the login page. With remember me, the user now stays logged in with the help of the new token/cookie.

6. Conclusion

This tutorial showed how to set up and configure Remember Me functionality in the security configuration, and briefly described what kind of data goes into the cookie.

The implementation can be found in the example github project – this is an Eclipse based project, so it should be easy to import and run as it is.

When the project runs locally, the login.html can be accessed on localhost.

Go deeper into Spring Security with the course:


  • Biniam A.

    Great! Thanks

  • Nice Blog! Thanks

  • Muzimil Basha

    Great Info. Thanks. I am trying to implement this. I have a customized login form with 3 login fields. We also have renamed j_username and j_password to username and password respectively. Can you tell me if I use this, does this still work. If not what do I need to do make this work

    • Hey Basha – yes, it will work just fine. Renaming the defaults is a good practice to hide any specific information about what security framework the application is using. So – since you’re no longer using the defaults, you will need to specify the new values in your Spring Security config, but other than that, there shouldn’t be any problem. And on the 3 fields – without seeing the code and knowing what the 3rd field is I’d say that you should be fine, but – it does depend on what you’re trying to do. Hope that helps. Cheers,

      • Muzimil Basha

        Thanks for the quick reply. I have already defined new names in spring security config. Can we also rename _spring_security_remember_me to any other name.

        • No worries – every default should not be changable – it wasn’t for a while, but this JIRA took care of it.

          • Muzimil Basha

            Wow. That’s really helpful. Thanks a bunch.

          • Glad to help – good luck with securing your app. Cheers,

  • Ashwani kumar

    Great and very simple to apply !!!

  • sagar

    Great post , really helpful and easy to implement ! Can we use database to store user credentials instead of cookie ? If yes then how to implement ?

    • Hey Sagar,
      These are 2 separate things. Yes, you can use the DB to store user credentials (the username and a hash of the password). For that, you can check out my last few security articles. As for the cookie – that’s simply used for authentication, so it doesn’t stop you from storing the credentials in any way. The implementation is in these last few articles. Hope it helps. Cheers,

    • Hey Sagar – yes, you can go down that route as well. It’s a bit more complex but also safer – here’s how to do it: Spring Security – Persistent Remember Me. Hope it helps. Cheers,


    Is it working with Firefox with no expiration time i.e infinite expiry. As Firefox automatically removes cookies when closed.

    • Firefox does not remove your persistent cookies when closed. If it does that – you may have some non-standard configuration, as that’s not the default behavior. Cheers,

      • ALI IMRAAN

        If i set Expiry on server to -1. and also set cookies expiry to -1 i.e never expire. Now if i login and then close the browser window. If i open up it’s not maintaing session. Also Remember Me Checkbox is not got checeked as well. So as a workaround i implemented Remeber Me in Pure JSF Mode as in current project i am using Spring+JSF.

        • I’m not quite clear on what you’re trying to accomplish. If you’re only working with session cookies, these will of course get cleared when the session is closed (and closing the browser will close the session). If you want your cookie to survive the closing/expiration of the session, you’ll need to use a persistent cookie. And one good way to do that – so that you don’t have to roll your own solution (which I would strongly recommend against) – is to use Remember Me. Hope I understood what you’re aiming to do correctly, and that it helps. Cheers,

  • Fabrice

    Thank you, this helped me!

  • Notionquest

    In the remember me method, can I update the expirationTime of the remember me cookie?

    • Yes, you definitely can. In fact, most of the remember-me functionality can be tuned and configured.

  • Sakibul Hasan

    I am trying to implement a custom remember me logic. There wont be any remember me checkbox in login form. The remember me option will set on based on user’s profile setting. Like for a user if the rememberme flag in db is on then when he is logging in remember me will be activated for him. I found there is an option to provide RememberMeServices in Spring security config. Is that is something I have to use? If yes then how?

    • Hmm – that’s a very interesting usecase. There’s no official support for that particular feature as far as I’m aware, but I’m sure it can be implemented manually.
      I can add it to the content calendar of the site if that would help – which means it would be ready and published in 1-2 months.

      • Sakibul Hasan

        That would be great. Thanks.