Skip to content

Commit 560e443

Browse files
committed
Add TenantPerIssuerComponentRegistry and TenantService to how-to guide
Issue gh-663
1 parent 1cda1ca commit 560e443

File tree

8 files changed

+333
-96
lines changed

8 files changed

+333
-96
lines changed

docs/modules/ROOT/pages/guides/how-to-multitenancy.adoc

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
:docs-dir: ..
66

77
This guide shows how to customize Spring Authorization Server to support multiple issuers per host in a multi-tenant hosting configuration.
8+
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.
9+
10+
* xref:guides/how-to-multitenancy.adoc#multi-tenant-define-tenant-identifier[Define the tenant identifier]
11+
* xref:guides/how-to-multitenancy.adoc#multi-tenant-create-component-registry[Create a component registry]
12+
* xref:guides/how-to-multitenancy.adoc#multi-tenant-create-components[Create multi-tenant components]
13+
* xref:guides/how-to-multitenancy.adoc#multi-tenant-add-tenants-dynamically[Add tenants dynamically]
14+
15+
[[multi-tenant-define-tenant-identifier]]
16+
== Define the tenant identifier
817

918
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.
1019

@@ -27,6 +36,25 @@ NOTE: The base URL of the xref:protocol-endpoints.adoc[Protocol Endpoints] is th
2736

2837
Essentially, an issuer identifier with a path component represents the _"tenant identifier"_.
2938

39+
[[multi-tenant-create-component-registry]]
40+
== Create a component registry
41+
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+
45+
We will use the following class in each of the delegating implementations below:
46+
47+
.TenantPerIssuerComponentRegistry
48+
[source,java]
49+
----
50+
include::{examples-dir}/main/java/sample/multitenancy/TenantPerIssuerComponentRegistry.java[]
51+
----
52+
53+
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.
54+
55+
[[multi-tenant-create-components]]
56+
== Create multi-tenant components
57+
3058
The components that require multi-tenant capability are:
3159

3260
* xref:guides/how-to-multitenancy.adoc#multi-tenant-registered-client-repository[`RegisteredClientRepository`]
@@ -39,7 +67,7 @@ For each of these components, an implementation of a composite can be provided t
3967
Let's step through a scenario of how to customize Spring Authorization Server to support 2x tenants for each multi-tenant capable component.
4068

4169
[[multi-tenant-registered-client-repository]]
42-
== Multi-tenant RegisteredClientRepository
70+
=== Multi-tenant RegisteredClientRepository
4371

4472
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:
4573

@@ -75,7 +103,7 @@ include::{examples-dir}/main/java/sample/multitenancy/DataSourceConfig.java[]
75103
<2> Use a separate H2 database instance using `issuer2-db` as the name.
76104

77105
[[multi-tenant-oauth2-authorization-service]]
78-
== Multi-tenant OAuth2AuthorizationService
106+
=== Multi-tenant OAuth2AuthorizationService
79107

80108
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:
81109

@@ -91,7 +119,7 @@ include::{examples-dir}/main/java/sample/multitenancy/OAuth2AuthorizationService
91119
<4> Obtain the `JdbcOAuth2AuthorizationService` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
92120

93121
[[multi-tenant-oauth2-authorization-consent-service]]
94-
== Multi-tenant OAuth2AuthorizationConsentService
122+
=== Multi-tenant OAuth2AuthorizationConsentService
95123

96124
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:
97125

@@ -107,7 +135,7 @@ include::{examples-dir}/main/java/sample/multitenancy/OAuth2AuthorizationConsent
107135
<4> Obtain the `JdbcOAuth2AuthorizationConsentService` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
108136

109137
[[multi-tenant-jwk-source]]
110-
== Multi-tenant JWKSource
138+
=== Multi-tenant JWKSource
111139

112140
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:
113141

@@ -121,3 +149,17 @@ include::{examples-dir}/main/java/sample/multitenancy/JWKSourceConfig.java[]
121149
<2> A `JWKSet` instance mapped to issuer identifier `issuer2`.
122150
<3> A composite implementation of an `JWKSource<SecurityContext>` that uses the `JWKSet` mapped to the _"requested"_ issuer identifier.
123151
<4> Obtain the `JWKSet` that is mapped to the _"requested"_ issuer identifier indicated by `AuthorizationServerContext.getIssuer()`.
152+
153+
[[multi-tenant-add-tenants-dynamically]]
154+
== Add Tenants Dynamically
155+
156+
If the number of tenants is dynamic and can change at runtime, defining each `DataSource` as a `@Bean` may not be feasible.
157+
In this case, the `DataSource` and corresponding components can be registered through other means at application startup and/or runtime.
158+
159+
The following example shows a Spring `@Service` capable of adding tenants dynamically:
160+
161+
.TenantService
162+
[source,java]
163+
----
164+
include::{examples-dir}/main/java/sample/multitenancy/TenantService.java[]
165+
----

docs/src/main/java/sample/multitenancy/JWKSourceConfig.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import java.security.interfaces.RSAPrivateKey;
2121
import java.security.interfaces.RSAPublicKey;
2222
import java.util.Collections;
23-
import java.util.HashMap;
2423
import java.util.List;
25-
import java.util.Map;
2624
import java.util.UUID;
2725

2826
import com.nimbusds.jose.KeySourceException;
@@ -35,18 +33,16 @@
3533

3634
import org.springframework.context.annotation.Bean;
3735
import org.springframework.context.annotation.Configuration;
38-
import org.springframework.security.oauth2.server.authorization.context.AuthorizationServerContextHolder;
3936

4037
@Configuration(proxyBeanMethods = false)
4138
public class JWKSourceConfig {
4239

4340
@Bean
44-
public JWKSource<SecurityContext> jwkSource() {
45-
Map<String, JWKSet> jwkSetMap = new HashMap<>();
46-
jwkSetMap.put("issuer1", new JWKSet(generateRSAJwk())); // <1>
47-
jwkSetMap.put("issuer2", new JWKSet(generateRSAJwk())); // <2>
41+
public JWKSource<SecurityContext> jwkSource(TenantPerIssuerComponentRegistry componentRegistry) {
42+
componentRegistry.register("issuer1", JWKSet.class, new JWKSet(generateRSAJwk())); // <1>
43+
componentRegistry.register("issuer2", JWKSet.class, new JWKSet(generateRSAJwk())); // <2>
4844

49-
return new DelegatingJWKSource(jwkSetMap);
45+
return new DelegatingJWKSource(componentRegistry);
5046
}
5147

5248
// @fold:on
@@ -72,10 +68,11 @@ private static RSAKey generateRSAJwk() {
7268
// @fold:off
7369

7470
private static class DelegatingJWKSource implements JWKSource<SecurityContext> { // <3>
75-
private final Map<String, JWKSet> jwkSetMap;
7671

77-
private DelegatingJWKSource(Map<String, JWKSet> jwkSetMap) {
78-
this.jwkSetMap = jwkSetMap;
72+
private final TenantPerIssuerComponentRegistry componentRegistry;
73+
74+
private DelegatingJWKSource(TenantPerIssuerComponentRegistry componentRegistry) {
75+
this.componentRegistry = componentRegistry;
7976
}
8077

8178
@Override
@@ -85,17 +82,7 @@ public List<JWK> get(JWKSelector jwkSelector, SecurityContext context) throws Ke
8582
}
8683

8784
private JWKSet getJwkSet() {
88-
if (AuthorizationServerContextHolder.getContext() == null ||
89-
AuthorizationServerContextHolder.getContext().getIssuer() == null) {
90-
return null;
91-
}
92-
String issuer = AuthorizationServerContextHolder.getContext().getIssuer(); // <4>
93-
for (Map.Entry<String, JWKSet> entry : this.jwkSetMap.entrySet()) {
94-
if (issuer.endsWith(entry.getKey())) {
95-
return entry.getValue();
96-
}
97-
}
98-
return null;
85+
return this.componentRegistry.get(JWKSet.class); // <4>
9986
}
10087

10188
}

docs/src/main/java/sample/multitenancy/OAuth2AuthorizationConsentServiceConfig.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package sample.multitenancy;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
20-
2118
import javax.sql.DataSource;
2219

2320
import org.springframework.beans.factory.annotation.Qualifier;
@@ -28,7 +25,6 @@
2825
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent;
2926
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
3027
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
31-
import org.springframework.security.oauth2.server.authorization.context.AuthorizationServerContextHolder;
3228

3329
@Configuration(proxyBeanMethods = false)
3430
public class OAuth2AuthorizationConsentServiceConfig {
@@ -37,22 +33,25 @@ public class OAuth2AuthorizationConsentServiceConfig {
3733
public OAuth2AuthorizationConsentService authorizationConsentService(
3834
@Qualifier("issuer1-data-source") DataSource issuer1DataSource,
3935
@Qualifier("issuer2-data-source") DataSource issuer2DataSource,
36+
TenantPerIssuerComponentRegistry componentRegistry,
4037
RegisteredClientRepository registeredClientRepository) {
4138

42-
Map<String, OAuth2AuthorizationConsentService> authorizationConsentServiceMap = new HashMap<>();
43-
authorizationConsentServiceMap.put("issuer1", new JdbcOAuth2AuthorizationConsentService( // <1>
44-
new JdbcTemplate(issuer1DataSource), registeredClientRepository));
45-
authorizationConsentServiceMap.put("issuer2", new JdbcOAuth2AuthorizationConsentService( // <2>
46-
new JdbcTemplate(issuer2DataSource), registeredClientRepository));
39+
componentRegistry.register("issuer1", OAuth2AuthorizationConsentService.class,
40+
new JdbcOAuth2AuthorizationConsentService( // <1>
41+
new JdbcTemplate(issuer1DataSource), registeredClientRepository));
42+
componentRegistry.register("issuer2", OAuth2AuthorizationConsentService.class,
43+
new JdbcOAuth2AuthorizationConsentService( // <2>
44+
new JdbcTemplate(issuer2DataSource), registeredClientRepository));
4745

48-
return new DelegatingOAuth2AuthorizationConsentService(authorizationConsentServiceMap);
46+
return new DelegatingOAuth2AuthorizationConsentService(componentRegistry);
4947
}
5048

5149
private static class DelegatingOAuth2AuthorizationConsentService implements OAuth2AuthorizationConsentService { // <3>
52-
private final Map<String, OAuth2AuthorizationConsentService> authorizationConsentServiceMap;
5350

54-
private DelegatingOAuth2AuthorizationConsentService(Map<String, OAuth2AuthorizationConsentService> authorizationConsentServiceMap) {
55-
this.authorizationConsentServiceMap = authorizationConsentServiceMap;
51+
private final TenantPerIssuerComponentRegistry componentRegistry;
52+
53+
private DelegatingOAuth2AuthorizationConsentService(TenantPerIssuerComponentRegistry componentRegistry) {
54+
this.componentRegistry = componentRegistry;
5655
}
5756

5857
@Override
@@ -80,17 +79,7 @@ public OAuth2AuthorizationConsent findById(String registeredClientId, String pri
8079
}
8180

8281
private OAuth2AuthorizationConsentService getAuthorizationConsentService() {
83-
if (AuthorizationServerContextHolder.getContext() == null ||
84-
AuthorizationServerContextHolder.getContext().getIssuer() == null) {
85-
return null;
86-
}
87-
String issuer = AuthorizationServerContextHolder.getContext().getIssuer(); // <4>
88-
for (Map.Entry<String, OAuth2AuthorizationConsentService> entry : this.authorizationConsentServiceMap.entrySet()) {
89-
if (issuer.endsWith(entry.getKey())) {
90-
return entry.getValue();
91-
}
92-
}
93-
return null;
82+
return this.componentRegistry.get(OAuth2AuthorizationConsentService.class); // <4>
9483
}
9584

9685
}

docs/src/main/java/sample/multitenancy/OAuth2AuthorizationServiceConfig.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package sample.multitenancy;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
20-
2118
import javax.sql.DataSource;
2219

2320
import org.springframework.beans.factory.annotation.Qualifier;
@@ -29,7 +26,6 @@
2926
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
3027
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
3128
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
32-
import org.springframework.security.oauth2.server.authorization.context.AuthorizationServerContextHolder;
3329

3430
@Configuration(proxyBeanMethods = false)
3531
public class OAuth2AuthorizationServiceConfig {
@@ -38,22 +34,25 @@ public class OAuth2AuthorizationServiceConfig {
3834
public OAuth2AuthorizationService authorizationService(
3935
@Qualifier("issuer1-data-source") DataSource issuer1DataSource,
4036
@Qualifier("issuer2-data-source") DataSource issuer2DataSource,
37+
TenantPerIssuerComponentRegistry componentRegistry,
4138
RegisteredClientRepository registeredClientRepository) {
4239

43-
Map<String, OAuth2AuthorizationService> authorizationServiceMap = new HashMap<>();
44-
authorizationServiceMap.put("issuer1", new JdbcOAuth2AuthorizationService( // <1>
45-
new JdbcTemplate(issuer1DataSource), registeredClientRepository));
46-
authorizationServiceMap.put("issuer2", new JdbcOAuth2AuthorizationService( // <2>
47-
new JdbcTemplate(issuer2DataSource), registeredClientRepository));
40+
componentRegistry.register("issuer1", OAuth2AuthorizationService.class,
41+
new JdbcOAuth2AuthorizationService( // <1>
42+
new JdbcTemplate(issuer1DataSource), registeredClientRepository));
43+
componentRegistry.register("issuer2", OAuth2AuthorizationService.class,
44+
new JdbcOAuth2AuthorizationService( // <2>
45+
new JdbcTemplate(issuer2DataSource), registeredClientRepository));
4846

49-
return new DelegatingOAuth2AuthorizationService(authorizationServiceMap);
47+
return new DelegatingOAuth2AuthorizationService(componentRegistry);
5048
}
5149

5250
private static class DelegatingOAuth2AuthorizationService implements OAuth2AuthorizationService { // <3>
53-
private final Map<String, OAuth2AuthorizationService> authorizationServiceMap;
5451

55-
private DelegatingOAuth2AuthorizationService(Map<String, OAuth2AuthorizationService> authorizationServiceMap) {
56-
this.authorizationServiceMap = authorizationServiceMap;
52+
private final TenantPerIssuerComponentRegistry componentRegistry;
53+
54+
private DelegatingOAuth2AuthorizationService(TenantPerIssuerComponentRegistry componentRegistry) {
55+
this.componentRegistry = componentRegistry;
5756
}
5857

5958
@Override
@@ -89,17 +88,7 @@ public OAuth2Authorization findByToken(String token, OAuth2TokenType tokenType)
8988
}
9089

9190
private OAuth2AuthorizationService getAuthorizationService() {
92-
if (AuthorizationServerContextHolder.getContext() == null ||
93-
AuthorizationServerContextHolder.getContext().getIssuer() == null) {
94-
return null;
95-
}
96-
String issuer = AuthorizationServerContextHolder.getContext().getIssuer(); // <4>
97-
for (Map.Entry<String, OAuth2AuthorizationService> entry : this.authorizationServiceMap.entrySet()) {
98-
if (issuer.endsWith(entry.getKey())) {
99-
return entry.getValue();
100-
}
101-
}
102-
return null;
91+
return this.componentRegistry.get(OAuth2AuthorizationService.class); // <4>
10392
}
10493

10594
}

docs/src/main/java/sample/multitenancy/RegisteredClientRepositoryConfig.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package sample.multitenancy;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
2018
import java.util.UUID;
2119

2220
import javax.sql.DataSource;
@@ -30,15 +28,15 @@
3028
import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository;
3129
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
3230
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
33-
import org.springframework.security.oauth2.server.authorization.context.AuthorizationServerContextHolder;
3431

3532
@Configuration(proxyBeanMethods = false)
3633
public class RegisteredClientRepositoryConfig {
3734

3835
@Bean
3936
public RegisteredClientRepository registeredClientRepository(
4037
@Qualifier("issuer1-data-source") DataSource issuer1DataSource,
41-
@Qualifier("issuer2-data-source") DataSource issuer2DataSource) {
38+
@Qualifier("issuer2-data-source") DataSource issuer2DataSource,
39+
TenantPerIssuerComponentRegistry componentRegistry) {
4240

4341
JdbcRegisteredClientRepository issuer1RegisteredClientRepository =
4442
new JdbcRegisteredClientRepository(new JdbcTemplate(issuer1DataSource)); // <1>
@@ -74,18 +72,18 @@ public RegisteredClientRepository registeredClientRepository(
7472
// @formatter:on
7573
// @fold:off
7674

77-
Map<String, RegisteredClientRepository> registeredClientRepositoryMap = new HashMap<>();
78-
registeredClientRepositoryMap.put("issuer1", issuer1RegisteredClientRepository);
79-
registeredClientRepositoryMap.put("issuer2", issuer2RegisteredClientRepository);
75+
componentRegistry.register("issuer1", RegisteredClientRepository.class, issuer1RegisteredClientRepository);
76+
componentRegistry.register("issuer2", RegisteredClientRepository.class, issuer2RegisteredClientRepository);
8077

81-
return new DelegatingRegisteredClientRepository(registeredClientRepositoryMap);
78+
return new DelegatingRegisteredClientRepository(componentRegistry);
8279
}
8380

8481
private static class DelegatingRegisteredClientRepository implements RegisteredClientRepository { // <3>
85-
private final Map<String, RegisteredClientRepository> registeredClientRepositoryMap;
8682

87-
private DelegatingRegisteredClientRepository(Map<String, RegisteredClientRepository> registeredClientRepositoryMap) {
88-
this.registeredClientRepositoryMap = registeredClientRepositoryMap;
83+
private final TenantPerIssuerComponentRegistry componentRegistry;
84+
85+
private DelegatingRegisteredClientRepository(TenantPerIssuerComponentRegistry componentRegistry) {
86+
this.componentRegistry = componentRegistry;
8987
}
9088

9189
@Override
@@ -113,17 +111,7 @@ public RegisteredClient findByClientId(String clientId) {
113111
}
114112

115113
private RegisteredClientRepository getRegisteredClientRepository() {
116-
if (AuthorizationServerContextHolder.getContext() == null ||
117-
AuthorizationServerContextHolder.getContext().getIssuer() == null) {
118-
return null;
119-
}
120-
String issuer = AuthorizationServerContextHolder.getContext().getIssuer(); // <4>
121-
for (Map.Entry<String, RegisteredClientRepository> entry : this.registeredClientRepositoryMap.entrySet()) {
122-
if (issuer.endsWith(entry.getKey())) {
123-
return entry.getValue();
124-
}
125-
}
126-
return null;
114+
return this.componentRegistry.get(RegisteredClientRepository.class); // <4>
127115
}
128116

129117
}

0 commit comments

Comments
 (0)