A simple multi-tenant application using spring boot with database per tenant approach:
- Each user of the application has a tenant associated with him.
- Each tenant connection properties is stored in the database along with the users.
The tenant selection is not made by taking by HTTP header ("X-Tenant-ID" for example) but instead uses the current logged user to do this:
- At login the application connects to default schema (see below), retrieve the user from database (and the associed tenant), generate the JWT token with tenant information into token claims and returns this token.
- The next calls to API then pass the JWT token (Authorization Bearer ...), the application validate this token, extract the tenant information from claims and sets the current tenant.
- The operation (selects/inserts/updates) is then done in the correct tenant for that user.
This application has 3 databases:
- multi_tenant_db_core, used by application to store the users, roles and tenant properties.
- multi_tenant_db_tenant1, used by user1.
- multi_tenant_db_tenant1, used by user2.
The migration is done by Liquibase, running in a multi tenant mode (MultiTenantDatabaseLiquibase extended from MultiTenantSpringLiquibase and including a MultiTenantDatabaseProvider which in turn is extended from AbstractDataSourceBasedMultiTenantConnectionProviderImpl).