Spring Security Remember Me

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://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <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"/>
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="user1" password="user1Pass" authorities="ROLE_USER" />
                <user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

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:

<html>
<head></head>

<body>
    <h1>Login</h1>

    <form name='f' action="j_spring_security_check" method='POST'>
        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='j_username' value=''></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='j_password' /></td>
            </tr>
            <tr>
                <td>Remember Me:</td>
                <td><input type="checkbox" name="_spring_security_remember_me" /></td>
            </tr>
            <tr>
                <td><input name="submit" type="submit" value="submit" /></td>
            </tr>
        </table>
    </form>

</body>
</html>

Notice the newly added checkbox input – mapping to _spring_security_remember_me. Unfortunately this default path cannot be changed via the namespace in Spring 3.1 – the option will be available starting with Spring 3.2.

This added input is enough to log in with remember me active.

The mechanism will create an additional cookie – the SPRING_SECURITY_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.

I usually post about Security on Google+ - you can follow me there:

Get My 3 Spring eBooks
Download the 3 eBooks - Build Your App "The Right Way"
×
Build Web App with Spring (and prototype it to 90% very quickly)

,

  • Biniam A.

    Great! Thanks

  • http://www.translucentcomputing.com/ Robert Golabek

    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

    • http://www.baeldung.com/ Eugen Paraschiv

      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,
      Eugen.

      • 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.

        • http://www.baeldung.com/ Eugen Paraschiv

          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.

          • http://www.baeldung.com/ Eugen Paraschiv

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

          • Muzimil Basha

            I have tried with the following config. . But maven build is getting failed with the error “No UserDetailsService registered.” I came to know that we need to write custom remember me service here. Can you help me here.

          • http://www.baeldung.com/ Eugen Paraschiv

            Well, the user details service is something different then your remember me settings. My suggestion is – make a sample project and put it on github with the problem and I’ll take a look. Cheers. Eugen.

          • Muzimil Basha

            Sure. I will try. Thanks