Security Top

I just announced the new Learn Spring Security course, including the full material focused on the new OAuth2 stack in Spring Security 5:

>> CHECK OUT THE COURSE

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.

Note: this tutorial focuses on the standard cookie-based approach. For the persistent approach, have a look at the Spring Security – Persistent Remember Me guide.

2. The Security Configuration

Let's see how to set up the security configuration using Java:

@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean("authenticationManager")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user1").password("{noop}user1Pass").roles("USER")
            .and()
            .withUser("admin1").password("{noop}admin1Pass").roles("ADMIN");
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/anonymous*").anonymous()
            .antMatchers("/login*").permitAll()
            .anyRequest().authenticated()
            
            .and()
            .formLogin()
            .loginPage("/login.html")
            .loginProcessingUrl("/login")
            .failureUrl("/login.html?error=true")
            
            .and()
            .logout().deleteCookies("JSESSIONID")
            
            .and()
            .rememberMe().key("uniqueAndSecret")
            ;
    }
}

As you can see, the basic configuration using the rememberMe() method is extremely simple while remaining very flexible through additional 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 using tokenValiditySeconds():

rememberMe().key("uniqueAndSecret").tokenValiditySeconds(86400)

We can also have a look at the equivalent XML configuration:

<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 id="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="user1" password="{noop}user1Pass" authorities="ROLE_USER" />
            <user name="admin1" password="{noop}admin1Pass" authorities="ROLE_ADMIN" />
        </user-service>
    </authentication-provider>
</authentication-manager>

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="login" method='POST'>
        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='username' value=''></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='password' /></td>
            </tr>
            <tr>
                <td>Remember Me:</td>
                <td><input type="checkbox" name="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 remember-me. This added input is enough to log in with remember me active.

This default path can also be changed as follows:

.rememberMe().rememberMeParameter("remember-me-new")

4. The Cookie

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

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

Security bottom

I just announced the new Learn Spring Security course, including the full material focused on the new OAuth2 stack in Spring Security 5:

>> CHECK OUT THE COURSE
5 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!