how-to-multitenancy.adoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 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.
  7. 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:
  8. [source,json]
  9. ----
  10. {
  11. "issuer": "http://localhost:9000/issuer1",
  12. "authorization_endpoint": "http://localhost:9000/issuer1/oauth2/authorize",
  13. "token_endpoint": "http://localhost:9000/issuer1/oauth2/token",
  14. "jwks_uri": "http://localhost:9000/issuer1/oauth2/jwks",
  15. "revocation_endpoint": "http://localhost:9000/issuer1/oauth2/revoke",
  16. "introspection_endpoint": "http://localhost:9000/issuer1/oauth2/introspect",
  17. ...
  18. }
  19. ----
  20. NOTE: The base URL of the xref:protocol-endpoints.adoc[Protocol Endpoints] is the issuer identifier value.
  21. Essentially, an issuer identifier with a path component represents the _"tenant identifier"_.
  22. The components that require multi-tenant capability are:
  23. * xref:guides/how-to-multitenancy.adoc#multi-tenant-registered-client-repository[`RegisteredClientRepository`]
  24. * xref:guides/how-to-multitenancy.adoc#multi-tenant-oauth2-authorization-service[`OAuth2AuthorizationService`]
  25. * xref:guides/how-to-multitenancy.adoc#multi-tenant-oauth2-authorization-consent-service[`OAuth2AuthorizationConsentService`]
  26. * xref:guides/how-to-multitenancy.adoc#multi-tenant-jwk-source[`JWKSource<SecurityContext>`]
  27. 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.
  28. Let's step through a scenario of how to customize Spring Authorization Server to support 2x tenants for each multi-tenant capable component.
  29. [[multi-tenant-registered-client-repository]]
  30. == Multi-tenant RegisteredClientRepository
  31. 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:
  32. .RegisteredClientRepositoryConfig
  33. [source,java]
  34. ----
  35. include::{examples-dir}/main/java/sample/multitenancy/RegisteredClientRepositoryConfig.java[]
  36. ----
  37. TIP: Click on the "Expand folded text" icon in the code sample above to display the full example.
  38. <1> A `JdbcRegisteredClientRepository` instance mapped to issuer identifier `issuer1` and using a dedicated `DataSource`.
  39. <2> A `JdbcRegisteredClientRepository` instance mapped to issuer identifier `issuer2` and using a dedicated `DataSource`.
  40. <3> A composite implementation of a `RegisteredClientRepository` that delegates to a `JdbcRegisteredClientRepository` mapped to the _"requested"_ issuer identifier.
  41. <4> Obtain the `JdbcRegisteredClientRepository` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  42. 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.
  43. In the preceding example, each of the `JdbcRegisteredClientRepository` instances are configured with a `JdbcTemplate` and associated `DataSource`.
  44. This is important in a multi-tenant configuration as a primary requirement is to have the ability to isolate the data from each tenant.
  45. 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.
  46. The following example shows a sample configuration of 2x `DataSource` `@Bean` (one for each tenant) that are used by the multi-tenant capable components:
  47. .DataSourceConfig
  48. [source,java]
  49. ----
  50. include::{examples-dir}/main/java/sample/multitenancy/DataSourceConfig.java[]
  51. ----
  52. <1> Use a separate H2 database instance using `issuer1-db` as the name.
  53. <2> Use a separate H2 database instance using `issuer2-db` as the name.
  54. [[multi-tenant-oauth2-authorization-service]]
  55. == Multi-tenant OAuth2AuthorizationService
  56. 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:
  57. .OAuth2AuthorizationServiceConfig
  58. [source,java]
  59. ----
  60. include::{examples-dir}/main/java/sample/multitenancy/OAuth2AuthorizationServiceConfig.java[]
  61. ----
  62. <1> A `JdbcOAuth2AuthorizationService` instance mapped to issuer identifier `issuer1` and using a dedicated `DataSource`.
  63. <2> A `JdbcOAuth2AuthorizationService` instance mapped to issuer identifier `issuer2` and using a dedicated `DataSource`.
  64. <3> A composite implementation of an `OAuth2AuthorizationService` that delegates to a `JdbcOAuth2AuthorizationService` mapped to the _"requested"_ issuer identifier.
  65. <4> Obtain the `JdbcOAuth2AuthorizationService` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  66. [[multi-tenant-oauth2-authorization-consent-service]]
  67. == Multi-tenant OAuth2AuthorizationConsentService
  68. 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:
  69. .OAuth2AuthorizationConsentServiceConfig
  70. [source,java]
  71. ----
  72. include::{examples-dir}/main/java/sample/multitenancy/OAuth2AuthorizationConsentServiceConfig.java[]
  73. ----
  74. <1> A `JdbcOAuth2AuthorizationConsentService` instance mapped to issuer identifier `issuer1` and using a dedicated `DataSource`.
  75. <2> A `JdbcOAuth2AuthorizationConsentService` instance mapped to issuer identifier `issuer2` and using a dedicated `DataSource`.
  76. <3> A composite implementation of an `OAuth2AuthorizationConsentService` that delegates to a `JdbcOAuth2AuthorizationConsentService` mapped to the _"requested"_ issuer identifier.
  77. <4> Obtain the `JdbcOAuth2AuthorizationConsentService` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
  78. [[multi-tenant-jwk-source]]
  79. == Multi-tenant JWKSource
  80. 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:
  81. .JWKSourceConfig
  82. [source,java]
  83. ----
  84. include::{examples-dir}/main/java/sample/multitenancy/JWKSourceConfig.java[]
  85. ----
  86. <1> A `JWKSet` instance mapped to issuer identifier `issuer1`.
  87. <2> A `JWKSet` instance mapped to issuer identifier `issuer2`.
  88. <3> A composite implementation of an `JWKSource<SecurityContext>` that uses the `JWKSet` mapped to the _"requested"_ issuer identifier.
  89. <4> Obtain the `JWKSet` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.