Kotlin SpringBoot – Configure Spring Security

In the tutorial, JavaSampleApproach will show you how to configure Security for Kotlin SpringBoot web application.

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

II. Goal

We create a Kotlin MVC Web Application as below:

kotlin spring security - project structure

With 5 urls:

– ‘/’: access with everyone.

kotlin spring security - home page

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

kotlin spring security - user page

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

kotlin spring security - admin page

– ‘/login’: login page

kotlin spring security - login page

– ‘/403’: HTTP Error 403 Forbidden

kotlin spring security - access denied

III. Practice

Step to do:
– Create Kotlin Spring Boot project
– Create Controller
– Create View Pages
– Configure WebSecurity

1. Create Kotlin Spring Boot project

Use SpringToolSuite to create a Kotlin SpringBoot project with below dependencies:

2. Create Controller

3. Create View Pages





4. Configure WebSecurity

Use WebSecurityConfigurerAdapter which provides a convenient base class to create a WebSecurityConfigurer instance.

Web application has 2 users:
– Admin: admin/admin
– User: user/user

IV. SourceCode


By grokonez | February 13, 2018.

Related Posts

Got Something To Say:

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