Kotlin – integrate Spring Security & H2 Database
Tags: kotlin, springboot, security
Published: 2021-05-02 17:57:01

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-integrate-spring-security-h2-database

Kotlin – integrate Spring Security & H2 Database

In the past <a href="https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-integrate-h2-database-spring-jpa-spring-boot-embedded-mode">post</a>, We had set-up a Kotlin SpringBoot project to develop with H2 database. But if you enable Spring Security in your project, the H2 database console will be blocked with 403 error. So in the tutorial, we will show you how to make configuration for resolving the Access Denied problem.

<!--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
- H2 database
– Bootstrap
<h2>II. Goal</h2>
<h3>1. Problem</h3>
If your project uses H2 database to develop and also enable Spring Security, then when accessing to H2 console path: '/h2_console', an error Access Denied Page will be thrown.

Why?
-> By default, Spring Security will block '/h2_console' path of H2 database.
<h3>2. Resolve</h3>
Solution is a simple configuration with Spring Security as below segment code:

<pre><code class="language-java">
@Override
protected void configure(HttpSecurity http) throws Exception {
	http.authorizeRequests()
		...
		
	http.csrf().disable();
	http.headers().frameOptions().disable();
</code></pre>

<code>http.csrf().disable()</code>: disable CRSF.
<code>http.headers().frameOptions().disable()</code>: H2 database console runs inside a frame, So we need to disable X-Frame-Options in Spring Security.
<h3>3. Goal</h3>
We create a Kotlin SpringBoot as below structure:

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-H2-project-structure.png" alt="Kotlin - Spring Security H2 - project structure" width="354" height="391" class="alignnone size-full wp-image-10945" />

Make a request to access H2’s console: 'http://localhost:8080/h2_console'

-> It will redirect to Login page.

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-H2-login.png" alt="Kotlin - Spring Security H2 - login" width="587" height="290" class="alignnone size-full wp-image-10946" />

Login with an account: 'user/user', it will redirect to Access Denied Page.

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-H2-access-denied.png" alt="Kotlin - Spring Security H2 - access denied" width="373" height="172" class="alignnone size-full wp-image-10947" />

-> Sign out

Again, make the request to access H2’s console: 'http://localhost:8080/h2_console', then login with user: 'admin/admin', it will redirect to H2’s login page:

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-H2-h2-database-security-h2-login-page.png" alt="Kotlin - Spring Security H2 - h2-database-security-h2-login-page" width="453" height="408" class="alignnone size-full wp-image-10948" />

Press 'Connect'. Then make an request in another tab: 'http://localhost:8080/save'. Then make a query 'select * from customer', We have:

<img src="https://grokonez.com/wp-content/uploads/2018/02/Kotlin-Spring-Security-H2-select-customers.png" alt="Kotlin - Spring Security H2 - select customers" width="537" height="363" class="alignnone size-full wp-image-10949" />

-> Now, It’s already for development Kotlin Spring Boot project with Spring Security and H2 database!
<h2>III. Implementation</h2>
<strong>Step to do</strong>
– Create Kotlin Spring Security project
– Implement bussiness Web Application with H2 database
<h3>1. Create Kotlin Spring Security project</h3>
Follow guides of the article: <a href="https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-springboot-configure-spring-security">Kotlin SpringBoot – Configure Spring Security</a>. Then modify the segment code:

More at:

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-integrate-spring-security-h2-database

Kotlin – integrate Spring Security & H2 Database