This repository demonstrates how to implement row-level multitenancy in a Spring Boot application. Each object in the application contains a tenantId
field to segregate data by tenants. An advanced implementation combining JWT (JSON Web Token) authentication with multitenancy is available in the multitenancy-JWT-Combination
branch.
Multitenancy ensures that multiple tenants (e.g., organizations or users) can share the same application instance while maintaining strict data isolation. This repository provides:
- A basic implementation of row-level multitenancy, where queries filter data by the
tenantId
field. - An advanced implementation integrating JWT authentication, where the
tenantId
is embedded in the JWT and used to scope all operations securely.
main
: Demonstrates the basic implementation of row-level multitenancy.multitenancy-JWT-Combination
: An advanced implementation combining row-level multitenancy with JWT authentication.
src/main/java
:com.rlabs.multitenant
: Main application code.config
: Configuration files for multitenancy and JWT integration.controllers
: REST API controllers for testing and demonstration.services
: Service layer implementing the core logic.entities
: JPA entities withtenantId
fields.repositories
: JPA repositories for database interactions.
application.properties
: Configuration file for database and other properties.README.md
: This file.
- Java: Programming language
- Spring Boot: Framework for building the application
- Spring Security for authentication
- Spring Data JPA for database interaction
- JWT: Token-based authentication
- H2/PostgreSQL/MySQL: Database options (configurable)
- Maven: Dependency management
- Java 17 or later installed.
- Maven installed.
- A database (H2 in-memory by default; others configurable in
application.yml
).