Kotlin – Spring Security – Persistent Token Remember-Me authentication
Tags: kotlin, springsecurity, persistent
Published: 2021-05-02 18:04:20

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-spring-security-persistent-token-remember-authentication

Kotlin – Spring Security – Persistent Token Remember-Me authentication

Remember-me authentication is a solution for websites to remember the identity of an user between many sessions. In the tutorial, <a href="https://grokonez.com">JavaSampleApppoach</a> will show you how to configure persistent token remember-me authentication with Kotlin Spring Security web application.

<!--more-->
<h2>I. Technologies</h2>
– Kotlin 1.2.20
– Apache Maven 3.5.2
– Spring Tool Suite – Version 3.9.2.RELEASE
– Spring Boot – 1.5.10.RELEASE
– Bootstrap
– MySQL database
<h2>II. Goal</h2>
We create a Kotlin SpringBoot MVC Web Application as below:

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-Persistent-Token-Remember-Me-authentication-project-structure.png" alt="Kotlin Spring Security - Persistent Token Remember-Me authentication - project structure" width="420" height="249" class="alignnone size-full wp-image-10772" />

We have a login page:

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-Persistent-Token-Remember-Me-authentication-login-page.png" alt="Kotlin Spring Security - Persistent Token Remember-Me authentication - login page" width="598" height="399" class="alignnone size-full wp-image-10773" />

When make the firstly request: <code>http://localhost:8080</code>, login page will be redicted immediately, authenticate with an account user/user, but NOT check 'Remember me'
-> Result: Login successfully, having 1 cookie: JSESSIONID. No record in persistent_logins table

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-Persistent-Token-Remember-Me-authentication-session.png" alt="Kotlin Spring Security - Persistent Token Remember-Me authentication - session" width="429" height="583" class="alignnone size-full wp-image-10774" />

– Delete JSESSIONID and make above request again: <code>http://localhost:8080</code> -> login page will be re-directed immediately for authentication again.

Login with account: user/user, and check 'Remember me'
-> Authentication successfully. Having 2 cookies: JSESSIOINID & javasampleapproach-remember-me 

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-Persistent-Token-Remember-Me-authentication-javasampleapproach-session.png" alt="Kotlin Spring Security - Persistent Token Remember-Me authentication - javasampleapproach session" width="427" height="574" class="alignnone size-full wp-image-10775" />

-> 1 record in persistent_logins table

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-Persistent-Token-Remember-Me-authentication-table-records.png" alt="Kotlin Spring Security - Persistent Token Remember-Me authentication -table records" width="634" height="52" class="alignnone size-full wp-image-10776" />

– Remove JSESSIONID cookie, then make the request: <code>http://localhost:8080</code>
-> NOT redirect to login page (because having javasampleapproach-remember-me cookie

– Remove JSESSIONID & javasampleapproach-remember-me cookie, then make the request: <code>http://localhost:8080</code>, login page will be redirected >>> Right!

It works fine!
<h2>III. Implementation</h2>
<strong>Step to do</strong>
– Create Kotlin SpringBoot project
– Create controller & views
– Setup MySql database configuration
– Configure remember-me security
<h3>1. Create Kotlin SpringBoot project</h3>
Use SpringToolSuite to create a Kotlin SpringBoot project with dependencies:

More at:

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-spring-security-persistent-token-remember-authentication

Kotlin – Spring Security – Persistent Token Remember-Me authentication