Kotlin – integrate Spring Security & H2 Database

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 post, 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.

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

  • H2 database – Bootstrap

    II. Goal

    1. Problem

    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.

2. Resolve

Solution is a simple configuration with Spring Security as below segment code:


@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        ...
        
    http.csrf().disable();
    http.headers().frameOptions().disable();

http.csrf().disable(): disable CRSF.
http.headers().frameOptions().disable(): H2 database console runs inside a frame, So we need to disable X-Frame-Options in Spring Security.

3. Goal

We create a Kotlin SpringBoot as below structure:

Kotlin - Spring Security H2 - project structure

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

-> It will redirect to Login page.

Kotlin - Spring Security H2 - login

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

Kotlin - Spring Security H2 - access denied

-> 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:

Kotlin - Spring Security H2 - h2-database-security-h2-login-page

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

Kotlin - Spring Security H2 - select customers

-> Now, It’s already for development Kotlin Spring Boot project with Spring Security and H2 database!

III. Implementation

Step to do
– Create Kotlin Spring Security project
– Implement bussiness Web Application with H2 database

1. Create Kotlin Spring Security project

Follow guides of the article: Kotlin SpringBoot – Configure Spring Security. 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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

447 bài viết.
78 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/kotlin/kotlinreadwritecsvfileexample How to read/write CSV file in Kotlin In this tutorial, we're gonna look at examples tha...
loveprogramming viết 7 ngày trước
0 0
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...
loveprogramming viết 1 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
447 bài viết.
78 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!