Spring boot with Redis and Postgres using Docker

This Spring boot project was developed with the default authentication settings in mind and documentation to serve as a basis for developing other projects.

Publicated: 2023-09-10


Modelo

About:

  • Role-based access control with JWT
  • technology used
    • Java 8
    • Maven 3.6.3
    • Spring Boot 2.7.0
      • Starter Web
      • Starter Data JPA
        • Starter Test (JUnit / Mockito)
        • Starter Log4J2
        • Devtools
    • Spring Security 5.6.4
      • Secutiry Oauth2 Autoconfigure 2.1.5
      • Security Test
    • Springfox (Swagger) 3.0.0
    • Lombok - Help setting up lombok
    • Database
      • Postgres:13 (Relational Database)
      • Starter Data Redis (Token Store)
      • h2database (Profile Test)
  • Application.properties - Default
    • Port: 5000
    • Profile: dev
    • Base path: /api
    • Encrypt: bcrypt
    • Hibernate DDL: update
  • ExceptionHandler
    • ProjectException:
      • Status: 400
      • Description: Exception provoked, n reasons, but mainly business rule
    • AuthorizationException:
      • Status: 403
      • Description: Access Denied with RuntimeException

It is not necessary to have Java, Maven, Postgres or redis installed, all services will run in Docker containers.

Pre requirements

  • Docker

    • Install docker here
  • File .env

    • Example:
    DB_AUTHSERVICE_DATABASE_NAME=authservice_database
    DB_AUTHSERVICE_USERNAME=postgres
    DB_AUTHSERVICE_PASSWORD=pg_postgres
    
    CLIENT_ID=52da334b25d96304a09901705846663fef41ce8f
    CLIENT_SECRET=99214c1e0dd20c56e76d4b2716b39e63a38e8d9b
    

Getting Started

  1. Git clone project
git clone https://github.com/andresinho20049/spring-authservice-with-docker.git
  1. Go into the project folder
cd spring-authservice-with-docker
  1. Create a file .env in root folder project
tee -a ./.env <<EOF
DB_AUTHSERVICE_DATABASE_NAME=authservice_database
DB_AUTHSERVICE_USERNAME=postgres
DB_AUTHSERVICE_PASSWORD=pg_postgres

CLIENT_ID=52da334b25d96304a09901705846663fef41ce8f
CLIENT_SECRET=99214c1e0dd20c56e76d4b2716b39e63a38e8d9b

To exit type EOF, then press ENTER

  1. Run docker compose with env file parameters
docker compose --env-file=./.env up --build

Authentication

It is configured by default to start in the Dev profile, in this profile a support user is created when starting spring

The default is:
Username: admin@email.com
Password: strongPassword@1234

With the project running, visit the Swagger page to test the endpoints

  • In the browser type the path: /api/swagger-ui/index.html
  • or If you prefer click here

also visit redis console, runing port 8001

Preview

Model

User

{
  "id": 1,
  "name": "Admin",
  "email": "admin@email.com",
  "updatePassword": true,
  "active": true,
  "roles": [
    {
      "name": "ROLE_ADMIN"
    }
  ]
}

Role

[
  {
    "name": "ROLE_ADMIN"
  },
  {
    "name": "ROLE_VIEW_USER"
  },
  {
    "name": "ROLE_CREATE_USER"
  },
  {
    "name": "ROLE_UPDATE_USER"
  },
  {
    "name": "ROLE_DISABLE_USER"
  }
]

JWT Payload Example

{
  "aud": ["restservice"],
  "updatePassword": true,
  "user_name": "admin@email.com",
  "scope": ["all"],
  "name": "Admin",
  "exp": 1655404387,
  "authorities": ["ROLE_ADMIN"],
  "jti": "7d12042b-856b-41a7-b1d9-6acb220840a7",
  "client_id": "52da334b25d96304a09901705846663fef41ce8f"
}

Considerations

The main objective behind the development of this project was to create a reliable and secure authentication service that could be utilized by other applications. It was designed to provide a seamless experience for users, while ensuring their information is protected. Additionally, this project serves as a boilerplate, enabling developers to quickly build and deploy new applications with the same level of security and authentication functionality.

Project: Spring boot with Redis and Postgres using Docker
By: André Carlos (andresinho20049)

Recommended projects