Spring Boot Security- Remember Me Example

This page will guide you on how to configure Spring Boot Security- Remember Me functionality in the application. Remember Me feature help users to access the application without re-login. Mostly, we use the checkbox on the login page to enable it.

Spring Boot Security- Remember Me Example

Spring Security- Remember Me feature stores user’s login information into the web browser cookies which able to identify the user across multiple sessions.

Note: This example is based on the Simple Hash-Based Token Approach which uses the hashing technique to create the unique token. In this technique, a token is created using the key, expiry date, password, and username.  Cookie being composed as follows:

base64(username + ":" + expirationTime + ":" +
             md5Hex(username + ":" + expirationTime + ":" password + ":" + key))

    username:          As identifiable to the UserDetailsService
    password:          That matches the one in the retrieved UserDetails
    expirationTime:    The date and time when the remember-me token expires,
                       expressed in milliseconds
    key:               A private key to prevent modification of the remember-me token

We have one more approach i.e. Persistent Token Approach. In this approach, we will store the token into the database. Table “persistent_logins” will be created to store the login token and series.

Technologies Used

Find the list of all technologies used in this application.

  1. Spring Boot 2.1.2.RELEASE
  2. Spring Security 5.1.4.RELEASE
  3. JDK 8
  4. Maven 3
  5. STS 3
  6. Embedded Tomcat Server

Dependencies Required

To resolve the JAR dependency, add the following code to your pom.xml.

	<!-- JSP compiler jar -->

Project Structure

The final project structure of our application in STS IDE will look like as follows.

Spring Boot Security- Remember Me Example

1. Login Form

Create a simple custom login form and add a checkbox to enable Remember Me feature.

<form action="login" method="post">
		<tr style="color: red;">
			<td>User name:</td>
			<td><input type="text" name="username"></td>
			<td><input type="password" name="password"></td>
			<td>Remember Me:</td>
			<td><input type="checkbox" name="remember-me" /></td>
			<td><input type="submit" value="Login"></td>

2. Security Configuration

Spring security configuration file is used to implement the remember me functionality as given below:

package org.websparrow.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

public class SecurityConfig extends WebSecurityConfigurerAdapter {

	protected void configure(HttpSecurity http) throws Exception {
			//remember me configuration
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

In the above Java configuration class:

rememberMe() method return the RememberMeConfigurer class for further customizations.

key(String key) method sets the key to identify tokens created for remember me authentication.

rememberMeCookieName(String rememberMeCookieName) method sets the name of cookie which stores the token for remember me authentication. Defaults to ‘remember-me’.

tokenValiditySeconds(int tokenValiditySeconds) method allows specifying how long (in seconds) a token is valid for.

3. Test the Application

To test the Remember Me feature of the application follows the below steps:

1. Run your application as Spring Boot.

2. Login with your username, password, and don’t forget to check the Remember Me checkbox.

3. After the successful login, close the browser and reopen it. Try to access your secure pages, this time it will not ask to reenter the username and password.

4. This is because it stores user’s login information into the web browser cookies.

Spring Boot Security- Remember Me Example


  1. Class HttpSecurity
  2. Remember-Me Authentication

Similar Posts

About the Author

I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.