Kotlin – Spring Security JDBC Authentication – SpringBoot + MySQL + Bootstrap

Tags: kotlin, springboot, jdbcauthentication, mysql
Published: 2021-05-14 15:43:58

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-spring-security-jdbc-authentication-springboot-mysql-bootstrap

Kotlin – Spring Security JDBC Authentication – SpringBoot + MySQL + Bootstrap

In the tutorial, <a href="https://grokonez.com">JavaSampleApproach</a> will show you how to create a Kotlin Spring Security JDBC Authentication with SpringBoot + MySQL + Bootstrap.

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

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-project-structure.png" alt="Kotlin Spring Security JDBC Authentication - project structure" width="395" height="309" class="alignnone size-full wp-image-10678" />

With 5 urls:

– ‘/’: access with everyone.

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-home-pagepng.png" alt="Kotlin Spring Security JDBC Authentication - home pagepng" width="411" height="169" class="alignnone size-full wp-image-10679" />

– ‘/user’: must authenticate and be accessed with user ROLE {USER, ADMIN}

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-users.png" alt="Kotlin Spring Security JDBC Authentication - users" width="395" height="180" class="alignnone size-full wp-image-10680" />

– ‘/admin’: accessed by user with role Admin

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-admin.png" alt="Kotlin Spring Security JDBC Authentication - admin" width="410" height="177" class="alignnone size-full wp-image-10681" />

– ‘/login’: login page

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-login.png" alt="Kotlin Spring Security JDBC Authentication - login" width="598" height="303" class="alignnone size-full wp-image-10682" />

– ‘/403’: HTTP Error 403 Forbidden

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-access-denied.png" alt="Kotlin Spring Security JDBC Authentication - access-denied" width="291" height="164" class="alignnone size-full wp-image-10683" />

We create 2 MySQL tables for 2 users (username/password):
- jack/jack has 2 roles {USER, ADMIN}
- peter/peter has 1 role USER

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-JDBC-Authentication-tables.png" alt="Kotlin Spring Security JDBC Authentication - tables" width="652" height="169" class="alignnone size-full wp-image-10684" />

-> <strong>jack/jack</strong> can access both pages {user.html, admin.html}. While <strong>peter/peter</strong> just accesses 1 page user.html.
<h2>III. Implementation</h2>
– Create Kotlin Spring Boot project
– Create Controller
– Create View Pages
– Configure Database
– Configure WebSecurity
<h3>1. Create Kotlin Spring Boot project</h3>
Use SpringToolSuite to create a Kotlin SpringBoot project with below dependencies:

More at:

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-spring-security-jdbc-authentication-springboot-mysql-bootstrap

Kotlin – Spring Security JDBC Authentication – SpringBoot + MySQL + Bootstrap