Skip to content

osahner/kotlin-spring-boot-rest-jpa-jwt-starter

Repository files navigation

kotlin-spring-boot-rest-jpa-jwt-starter

Quality gate Vulnerabilities Bugs Security Rating Coverage

Features:

  • spring-boot 3.2.x
  • kotlin 1.9.x
  • JWT Authentication/Authorization with spring-security inspired by Auth0
  • 2FA with TOTP (Google Authenticator)
  • JPA mysql / OpenCVS / POI

Install & play

  • create a mysql db
-- for mysql 5.7
CREATE DATABASE starterspringkotlin;
GRANT ALL ON starterspringkotlin.* TO starterspringkotlin@localhost IDENTIFIED BY 'starterspringkotlin';
FLUSH PRIVILEGES;
-- for mysql 8
CREATE DATABASE starterspringkotlin;
CREATE USER 'starterspringkotlin'@'localhost' IDENTIFIED BY 'starterspringkotlin';
GRANT ALL PRIVILEGES ON starterspringkotlin.* TO 'starterspringkotlin'@'localhost';
FLUSH PRIVILEGES;

check src/main/resources/application.yaml for mysql 5.7 or 8 support (keys are spring.jpa.database-platform and spring.datasource.url)

  • compile & integration tests
mvn clean compile test
  • run app
mvn spring-boot:run
  • some CLI tests
curl http://localhost:4080/starter-test/api/v1/test
# result: Pong!%

curl http://localhost:4080/starter-test/api/v1/restricted
# result {"timestamp":"***","status":403,"error":"Forbidden","message":"Access Denied","path":"/starter-test/api/v1/restricted"}%

curl -s -i -H "Content-Type: application/json" -X POST -d '{ "username": "john.doe", "password": "test1234"}' http://localhost:4080/starter-test/login | grep Authorization
# result: Authorization: Bearer ***

curl  -H "Authorization: Bearer ***"  http://localhost:4080/starter-test/api/v1/restricted
# result: Pong!%

some more test can be found in address.http if you are using intelli j.

Docker

./contributed/buildDocker.sh (-p) # see below
docker run -it -p 8888:8888 --rm osahner/kotlin-spring-boot-rest-jpa-jwt-starter:0.11.0-SNAPSHOT

curl http://localhost:8888/starter-test/api/v1/test
# result: Pong!%

❗ If you develop on Apple Silicon (like me) you can use the simple script contributed/buildDocker.sh. Option -p is for production build (--platform=linux/amd64 instead of --platform=linux/arm64/v8 without)

Modify Dockerfile to your needs.

Why

This is my little backend cookbook. I need and use it regularly for various small to medium-sized projects.

  • Like it -> use it.
  • Found an error -> please report.

Changelog

  • v0.11.2-SNAPSHOT: spring-boot 3.3.0, kotlin 2.0.0
  • v0.11.1-SNAPSHOT: fix token validity time
  • v0.11.0-SNAPSHOT: spring-boot 3.2.x, kotlin 1.9.x, java 21
  • v0.10.0-SNAPSHOT: spring-boot 3.1.x, add 2FA, cleanup
  • v0.9.1-SNAPSHOT: spring-boot 3.0.x, kotlin 1.8.x, migrated to SEQ tables
  • v0.8.3-SNAPSHOT: spring-boot 2.7.x, java 17
  • v0.8.1-SNAPSHOT: spring-boot 2.6.x
  • v0.8.0-SNAPSHOT: renamed default branch to main, spring-boot 2.5.x, kotlin 1.4.10
  • v0.7.1-SNAPSHOT: spring-boot 2.4.0
  • v0.6.6-SNAPSHOT: spring-boot 2.3.4, kotlin 1.4.10, update docker build
  • v0.6.5-SNAPSHOT: spring-boot 2.3.2, kotlin 1.3.72, fix JPA uneccessary creation of hibernate_sequence and join tables without primary key, enhanced PoiExportService
  • v0.6.4-SNAPSHOT: spring-boot 2.2.4, kotlin 1.3.70, fix REST API naming convention
  • v0.6.1-SNAPSHOT: add Docker
  • v0.6.0-SNAPSHOT: update spring-boot 2.2.0.RELEASE, add address controller with csv import and xls export
  • v0.5.0-SNAPSHOT: spring-boot 2.1.9, and kotlin 1.3.50
  • v0.4.1-SNAPSHOT: spring-boot 2.1.3 and kotlin 1.3.21, add codecov, fixed code style, fix tests, add coverage
  • v0.3.1-SNAPSHOT: update jdk11, spring-boot 2.1.2 and kotlin 1.3.20
  • v0.1.0-SNAPSHOT: switch to jar packaging standalone app, update kotlin 1.2.61, jwt 0.10.5
  • v0.0.5-SNAPSHOT: update spring-boot 2.0.4.RELEASE, kotlin 1.2.60, jwt 0.10.1

Rename local master branch to main

git branch -m master main
git fetch origin
git branch -u origin/main main

Migrate to SEQ tables

-- migrate existing autoincrement tables to SEQ tables after table update
SET FOREIGN_KEY_CHECKS = 0;
USE starterspringkotlin;

UPDATE app_user_SEQ SET next_val = (SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'starterspringkotlin' AND TABLE_NAME = 'app_user') + 1;
ALTER TABLE app_user MODIFY id INT NOT NULL;

UPDATE app_role_SEQ SET next_val = (SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'starterspringkotlin' AND TABLE_NAME = 'app_role') + 1;
ALTER TABLE app_role MODIFY id INT NOT NULL;

UPDATE address_SEQ SET next_val = (SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'starterspringkotlin' AND TABLE_NAME = 'address') + 1;
ALTER TABLE address MODIFY id INT NOT NULL;

SET FOREIGN_KEY_CHECKS = 1;

LICENCE

MIT © Oliver Sahner