This application is using discriminator column pattern to support multi-tenancy.
Instead of adding filters on the application code, this project uses database level policies to enforce security on the tenant's data.
This is done using Postgres Row Level Security.
Java 8
Spring Boot
Postgres
Main Class: src/main/java/com/ankit/rls/RowLevelSecurityApplication.java
- Create a new Postgres database named 'rls'
- Create a new database user with name 'application_user' and password as 'password'
- Run the application. The application uses flyway for the database migration. The flyway will create a new table 'users' and 'tenants' in the database.
- The project contains script to insert testdata. The tenants and users table will be filled with some testdata.
- The project contains sql script to enable row level security on the users table.
The users APIs require x-tenant-id header to be sent with the request. The 'x-tenant-id' header is used to identify the tenant.