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:

2. Create controller & views
WebController

home.html

login.html

3. Setup MySql database configuration

Open application.properties file, configure datasource:

On MySQL database: testbdb, create persistent_logins table by below script:

4. Configure remember-me security

IV. Sourcecode

SpringSecurityPersistentRememberMe

By grokonez | February 18, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*