how-to-multitenancy.adoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. [[how-to-multitenancy]]
  2. = How-to: Implement Multitenancy
  3. :index-link: ../how-to.html
  4. :docs-dir: ..
  5. This guide shows how to customize Spring Authorization Server to support multiple issuers per host in a multi-tenant hosting configuration.
  6. The purpose of this guide is to demonstrate a general pattern for building multi-tenant capable components for Spring Authorization Server, which can also be applied to other components to suit your needs.
  7. * xref:guides/how-to-multitenancy.adoc#multi-tenant-define-tenant-identifier[Define the tenant identifier]
  8. * xref:guides/how-to-multitenancy.adoc#multi-tenant-enable-multiple-issuers[Enable multiple issuers]
  9. * xref:guides/how-to-multitenancy.adoc#multi-tenant-create-component-registry[Create a component registry]
  10. * xref:guides/how-to-multitenancy.adoc#multi-tenant-create-components[Create multi-tenant components]
  11. * xref:guides/how-to-multitenancy.adoc#multi-tenant-add-tenants-dynamically[Add tenants dynamically]
  12. [[multi-tenant-define-tenant-identifier]]
  13. == Define the tenant identifier
  14. The xref:protocol-endpoints.adoc#oidc-provider-configuration-endpoint[OpenID Connect 1.0 Provider Configuration Endpoint] and xref:protocol-endpoints.adoc#oauth2-authorization-server-metadata-endpoint[OAuth2 Authorization Server Metadata Endpoint] allow for path components in the issuer identifier value, which effectively enables supporting multiple issuers per host.
  15. For example, an https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest[OpenID Provider Configuration Request] "http://localhost:9000/issuer1/.well-known/openid-configuration" or an https://datatracker.ietf.org/doc/html/rfc8414#section-3.1[Authorization Server Metadata Request] "http://localhost:9000/.well-known/oauth-authorization-server/issuer1" would return the following configuration metadata:
  16. [source,json]
  17. ----
  18. {
  19. "issuer": "http://localhost:9000/issuer1",
  20. "authorization_endpoint": "http://localhost:9000/issuer1/oauth2/authorize",
  21. "token_endpoint": "http://localhost:9000/issuer1/oauth2/token",
  22. "jwks_uri": "http://localhost:9000/issuer1/oauth2/jwks",
  23. "revocation_endpoint": "http://localhost:9000/issuer1/oauth2/revoke",
  24. "introspection_endpoint": "http://localhost:9000/issuer1/oauth2/introspect",
  25. ...
  26. }
  27. ----
  28. NOTE: The base URL of the xref:protocol-endpoints.adoc[Protocol Endpoints] is the issuer identifier value.
  29. Essentially, an issuer identifier with a path component represents the _"tenant identifier"_.
  30. [[multi-tenant-enable-multiple-issuers]]
  31. == Enable multiple issuers
  32. Support for using multiple issuers per host is disabled by default.
  33. To enable, add the following configuration:
  34. .AuthorizationServerSettingsConfig
  35. [source,java]
  36. ----
  37. include::{examples-dir}/main/java/sample/multitenancy/AuthorizationServerSettingsConfig.java[]
  38. ----
  39. <1> Set to `true` to allow usage of multiple issuers per host.
  40. [[multi-tenant-create-component-registry]]
  41. == Create a component registry
  42. We start by building a simple registry for managing the concrete components for each tenant.
  43. The registry contains the logic for retrieving a concrete implementation of a particular class using the issuer identifier value.
  44. We will use the following class in each of the delegating implementations below:
  45. .TenantPerIssuerComponentRegistry
  46. [source,java]
  47. ----
  48. include::{examples-dir}/main/java/sample/multitenancy/TenantPerIssuerComponentRegistry.java[]
  49. ----
  50. TIP: This registry is designed to allow components to be easily registered at startup to support adding tenants statically, but also supports xref:guides/how-to-multitenancy.adoc#multi-tenant-add-tenants-dynamically[adding tenants dynamically] at runtime.
  51. [[multi-tenant-create-components]]
  52. == Create multi-tenant components
  53. The components that require multi-tenant capability are:
  54. * xref:guides/how-to-multitenancy.adoc#multi-tenant-registered-client-repository[`RegisteredClientRepository`]
  55. * xref:guides/how-to-multitenancy.adoc#multi-tenant-oauth2-authorization-service[`OAuth2AuthorizationService`]
  56. * xref:guides/how-to-multitenancy.adoc#multi-tenant-oauth2-authorization-consent-service[`OAuth2AuthorizationConsentService`]
  57. * xref:guides/how-to-multitenancy.adoc#multi-tenant-jwk-source[`JWKSource<SecurityContext>`]
  58. For each of these components, an implementation of a composite can be provided that delegates to the concrete component associated to the _"requested"_ issuer identifier.
  59. Let's step through a scenario of how to customize Spring Authorization Server to support 2x tenants for each multi-tenant capable component.
  60. [[multi-tenant-registered-client-repository]]
  61. === Multi-tenant RegisteredClientRepository
  62. The following example shows a sample implementation of a xref:core-model-components.adoc#registered-client-repository[`RegisteredClientRepository`] that is composed of 2x `JdbcRegisteredClientRepository` instances, where each instance is mapped to an issuer identifier:
  63. .RegisteredClientRepositoryConfig
  64. [source,java]
  65. ----
  66. include::{examples-dir}/main/java/sample/multitenancy/RegisteredClientRepositoryConfig.java[]
  67. ----
  68. TIP: Click on the "Expand folded text" icon in the code sample above to display the full example.
  69. <1> A `JdbcRegisteredClientRepository` instance mapped to issuer identifier `issuer1` and using a dedicated `DataSource`.
  70. <2> A `JdbcRegisteredClientRepository` instance mapped to issuer identifier `issuer2` and using a dedicated `DataSource`.
  71. <3> A composite implementation of a `RegisteredClientRepository` that delegates to a `JdbcRegisteredClientRepository` mapped to the _"requested"_ issuer identifier.
  72. <4> Obtain the `JdbcRegisteredClientRepository` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  73. IMPORTANT: Explicitly configuring the issuer identifier via `AuthorizationServerSettings.builder().issuer("http://localhost:9000")` forces to a single-tenant configuration. Avoid explicitly configuring the issuer identifier when using a multi-tenant hosting configuration.
  74. In the preceding example, each of the `JdbcRegisteredClientRepository` instances are configured with a `JdbcTemplate` and associated `DataSource`.
  75. This is important in a multi-tenant configuration as a primary requirement is to have the ability to isolate the data from each tenant.
  76. Configuring a dedicated `DataSource` for each component instance provides the flexibility to isolate the data in its own schema within the same database instance or alternatively isolate the data in a separate database instance altogether.
  77. The following example shows a sample configuration of 2x `DataSource` `@Bean` (one for each tenant) that are used by the multi-tenant capable components:
  78. .DataSourceConfig
  79. [source,java]
  80. ----
  81. include::{examples-dir}/main/java/sample/multitenancy/DataSourceConfig.java[]
  82. ----
  83. <1> Use a separate H2 database instance using `issuer1-db` as the name.
  84. <2> Use a separate H2 database instance using `issuer2-db` as the name.
  85. [[multi-tenant-oauth2-authorization-service]]
  86. === Multi-tenant OAuth2AuthorizationService
  87. The following example shows a sample implementation of an xref:core-model-components.adoc#oauth2-authorization-service[`OAuth2AuthorizationService`] that is composed of 2x `JdbcOAuth2AuthorizationService` instances, where each instance is mapped to an issuer identifier:
  88. .OAuth2AuthorizationServiceConfig
  89. [source,java]
  90. ----
  91. include::{examples-dir}/main/java/sample/multitenancy/OAuth2AuthorizationServiceConfig.java[]
  92. ----
  93. <1> A `JdbcOAuth2AuthorizationService` instance mapped to issuer identifier `issuer1` and using a dedicated `DataSource`.
  94. <2> A `JdbcOAuth2AuthorizationService` instance mapped to issuer identifier `issuer2` and using a dedicated `DataSource`.
  95. <3> A composite implementation of an `OAuth2AuthorizationService` that delegates to a `JdbcOAuth2AuthorizationService` mapped to the _"requested"_ issuer identifier.
  96. <4> Obtain the `JdbcOAuth2AuthorizationService` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  97. [[multi-tenant-oauth2-authorization-consent-service]]
  98. === Multi-tenant OAuth2AuthorizationConsentService
  99. The following example shows a sample implementation of an xref:core-model-components.adoc#oauth2-authorization-consent-service[`OAuth2AuthorizationConsentService`] that is composed of 2x `JdbcOAuth2AuthorizationConsentService` instances, where each instance is mapped to an issuer identifier:
  100. .OAuth2AuthorizationConsentServiceConfig
  101. [source,java]
  102. ----
  103. include::{examples-dir}/main/java/sample/multitenancy/OAuth2AuthorizationConsentServiceConfig.java[]
  104. ----
  105. <1> A `JdbcOAuth2AuthorizationConsentService` instance mapped to issuer identifier `issuer1` and using a dedicated `DataSource`.
  106. <2> A `JdbcOAuth2AuthorizationConsentService` instance mapped to issuer identifier `issuer2` and using a dedicated `DataSource`.
  107. <3> A composite implementation of an `OAuth2AuthorizationConsentService` that delegates to a `JdbcOAuth2AuthorizationConsentService` mapped to the _"requested"_ issuer identifier.
  108. <4> Obtain the `JdbcOAuth2AuthorizationConsentService` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  109. [[multi-tenant-jwk-source]]
  110. === Multi-tenant JWKSource
  111. And finally, the following example shows a sample implementation of a `JWKSource<SecurityContext>` that is composed of 2x `JWKSet` instances, where each instance is mapped to an issuer identifier:
  112. .JWKSourceConfig
  113. [source,java]
  114. ----
  115. include::{examples-dir}/main/java/sample/multitenancy/JWKSourceConfig.java[]
  116. ----
  117. <1> A `JWKSet` instance mapped to issuer identifier `issuer1`.
  118. <2> A `JWKSet` instance mapped to issuer identifier `issuer2`.
  119. <3> A composite implementation of an `JWKSource<SecurityContext>` that uses the `JWKSet` mapped to the _"requested"_ issuer identifier.
  120. <4> Obtain the `JWKSet` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  121. [[multi-tenant-add-tenants-dynamically]]
  122. == Add Tenants Dynamically
  123. If the number of tenants is dynamic and can change at runtime, defining each `DataSource` as a `@Bean` may not be feasible.
  124. In this case, the `DataSource` and corresponding components can be registered through other means at application startup and/or runtime.
  125. The following example shows a Spring `@Service` capable of adding tenants dynamically:
  126. .TenantService
  127. [source,java]
  128. ----
  129. include::{examples-dir}/main/java/sample/multitenancy/TenantService.java[]
  130. ----