Remember me in Spring Security


I love Spring when it is very easy to add new stuff to your current projects. Recently I was asked to add Remember me checkbox to login page. If you are using Spring Security what you just need to do is as follows:

add this to your spring xml : <remember-me key="Your_key_with_values_like*!casAA='0401./e;2009NrI"/>

and add a checkbox to your login page ( id is important here ) : <h:selectBooleanCheckbox id="_spring_security_remember_me" />

and that is. Spring will do all the work for you.

Just remember if you have an exception about transactions, just add a @Transactional to your method which loads the user from the DB.

One thought on “Remember me in Spring Security

  1. Pretty useful… thanks! Just a sidenote: The Spring documentation says this method of remember-me is not that safe. If you’re using a database backend, you could use the persistent token approach. It’s pretty simple too:

    In short, I just needed the remember-me tag to reference a datasource:

    The datasource must have a table to record the persistent logins. The insert statement is below:

    create table persistent_logins (username varchar(64) not null, series varchar(64) primary key, token varchar(64) not null, last_used timestamp not null)

    The last thing is use the selectBooleanCheckbox as you pointed out. I hope that helps someone.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s