In the tutorial, JavaSampleApproach will show you how to create a Kotlin Spring Security JDBC Authentication with SpringBoot + MySQL + Bootstrap.
I. Technologies
– 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
II. Goal
We create a Kotlin MVC Web Application as below:
With 5 urls:
– ‘/’: access with everyone.
– ‘/user’: must authenticate and be accessed with user ROLE {USER, ADMIN}
– ‘/admin’: accessed by user with role Admin
– ‘/login’: login page
– ‘/403’: HTTP Error 403 Forbidden
We create 2 MySQL tables for 2 users (username/password):
– jack/jack has 2 roles {USER, ADMIN}
– peter/peter has 1 role USER
-> jack/jack can access both pages {user.html, admin.html}. While peter/peter just accesses 1 page user.html.
III. Implementation
– Create Kotlin Spring Boot project
– Create Controller
– Create View Pages
– Configure Database
– Configure WebSecurity
1. Create Kotlin Spring Boot project
Use SpringToolSuite to create a Kotlin SpringBoot project with below dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2. Create Controller
package com.javasampleapproach.kotlin.springsecurity.controller import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller class WebController { @RequestMapping(value="/") fun home(): String{ return "home" } @RequestMapping(value="/user") fun welcome(): String{ return "user" } @RequestMapping(value="/admin") fun admin(): String{ return "admin" } @RequestMapping(value="/login") fun login(): String{ return "login" } @RequestMapping(value="/403") fun Error403(): String{ return "403" } }
3. Create View Pages
home.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Security with Spring Boot</title> </head> <body> <h1>Hello, This is Home page!</h1> <a style="color: blue" th:href="@{/user}">User Page</a> <br /> <a style="color: blue" th:href="@{/admin}">Admin Page</a> </body> </html>
user.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Welcome Security with Spring Boot!</title> </head> <body> <h1>Hello, the page is for Users!</h1> <a style="color: blue" th:href="@{/}">Home</a> <form th:action="@{/logout}" method="post"> <input type="submit" value="Sign Out" /> </form> </body> </html>
admin.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Welcome Security with Spring Boot!</title> </head> <body> <h1>Hello, the page is for Admin!</h1> <a style="color: blue" th:href="@{/}">Home</a> <form th:action="@{/logout}" method="post"> <input type="submit" value="Sign Out" /> </form> </body>