DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • How to Use Java to Build Single Sign-on
  • What Java DAO Layer Is Best for Your Project

Trending

  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • DGS GraphQL and Spring Boot
  • Unlocking AI Coding Assistants: Generate Unit Tests
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Cookies to implement a RememberMe functionality

Using Cookies to implement a RememberMe functionality

By 
Mihai Dinca - Panaitescu user avatar
Mihai Dinca - Panaitescu
·
Jun. 26, 12 · Interview
Likes (1)
Comment
Save
Tweet
Share
57.9K Views

Join the DZone community and get the full member experience.

Join For Free

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
servletResponse.addCookie(c);
        

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)) {        
         c.setMaxAge(0);
         c.setValue(null);
         servletResponse.addCookie(c);    
          }
    }        

 

Framework application Data (computing) Session (web analytics) Machine security Java (programming language) Form (document) Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • How to Use Java to Build Single Sign-on
  • What Java DAO Layer Is Best for Your Project

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!