My entire carrier is linked to Java. I have been working in Java for 15 years and I still love it. I currently develop NextReports application My Personal Blog My NextReports Blog NextReports Site Mihai has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

Using Cookies to implement a RememberMe functionality

  • submit to reddit

Some web applications may need a "Remember Me" functionality. This means that, after a user login, user will have access from same machine to all its data even after session expired. This access will be possible until user does a logout.

If you are using Spring  and its login form, then you should use "Remember Me" functionality already implemented inside the framework.

Some web frameworks also offer a type of SignIn panel which already has "remember me" built-in.

But in case you have to implement "Remember Me" functionality by your own, this can be easily achieved using Cookies. Java has a Cookie class named javax.servlet.http.Cookie.

Algorithm is straight-forward:

  1. your login panel must contain a "Remember Me" check
  2. after a succesfull login with "Remember Me" check selected, you can create two cookies: one to keep the value for rememberMe and one to keep  a token which has to identify the logged user. For sake of security, this token must never contain user name or user password. The ideea is to   generate a random id as token value. And token value aside with user id must be saved in your storage (database)
  3. whenever a login is needed, you have to look if there is any cookie saved by you, and if so and your "rememberMe" value is true, you can take the user from storage based on your token and do an automatic login.
  4. when a logout is done, you have to delete the cookie that keeps the token


To add a cookie, you have to specify the maximum age of the cookie in seconds :

     HttpServletResponse servletResponse = ...;
Cookie c = new Cookie(COOKIE_NAME, encodeString(uuid));
c.setMaxAge(365 * 24 * 60 * 60); // one year

To delete a cookie, you have to find cookie by name and set its maximum age to 0, before adding it to servlet response:

     HttpServletRequest servletRequest = ...;
HttpServletResponse servletResponse = ... ;
Cookie[] cookies = servletRequest.getCookies();                
for (int i = 0; i < cookies.length; i++) {
     Cookie c = cookies[i];            
    if (c.getName().equals(COOKIE_NAME)) {        


Published at DZone with permission of its author, Mihai Dinca - Panaitescu.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Pavel Bernshtam replied on Tue, 2012/06/26 - 2:54pm

Do you know a way implement "Remember me" without cookies ?!

Christos Melas replied on Wed, 2012/06/27 - 3:18am in response to: Pavel Bernshtam

check out the following,


So possibly by throwing ip address along, you might accomplish it to some extent.

Mihai Dinca - P... replied on Wed, 2012/06/27 - 3:50am in response to: Pavel Bernshtam

I am not aware of any "Remember me" implementation without Cookies. Using your ip may be a solution but if you are behind a NAT along with other users, it won't be possible.

Seb Cha replied on Wed, 2012/06/27 - 6:15am

As implemented in Spring-Security, a remember-me cookie contains the following string: "username:expiryTime:Hexa(MD5(username:expiryTime:password:salt))"


Edit: it's one of the two implementations available. The other is based on a persisted secure random ID.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.