DEV Community

loizenai
loizenai

Posted on

Kotlin – Spring Security – Persistent Token Remember-Me authentication

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, JavaSampleApppoach will show you how to configure persistent token remember-me authentication with Kotlin Spring Security web application.

I. Technologies

– 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

II. Goal

We create a Kotlin SpringBoot MVC Web Application as below:

Kotlin Spring Security - Persistent Token Remember-Me authentication - project structure

We have a login page:

Kotlin Spring Security - Persistent Token Remember-Me authentication - login page

When make the firstly request: http://localhost:8080, 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

Kotlin Spring Security - Persistent Token Remember-Me authentication - session

– Delete JSESSIONID and make above request again: http://localhost:8080 -> 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

Kotlin Spring Security - Persistent Token Remember-Me authentication - javasampleapproach session

-> 1 record in persistent_logins table

Kotlin Spring Security - Persistent Token Remember-Me authentication -table records

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

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

It works fine!

III. Implementation

Step to do
– Create Kotlin SpringBoot project
– Create controller & views
– Setup MySql database configuration
– Configure remember-me security

1. Create Kotlin SpringBoot project

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

Oldest comments (0)