multitenancy.adoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. = OAuth 2.0 Resource Server Multi-tenancy
  2. [[webflux-oauth2resourceserver-multitenancy]]
  3. == Multi-tenancy
  4. A resource server is considered multi-tenant when there are multiple strategies for verifying a bearer token, keyed by some tenant identifier.
  5. For example, your resource server can accept bearer tokens from two different authorization servers.
  6. Alternately, your authorization server can represent a multiplicity of issuers.
  7. In each case, two things need to be done and trade-offs are associated with how you choose to do them:
  8. . Resolve the tenant.
  9. . Propagate the tenant.
  10. === Resolving the Tenant By Claim
  11. One way to differentiate tenants is by the issuer claim. Since the issuer claim accompanies signed JWTs, you can do so with the `JwtIssuerReactiveAuthenticationManagerResolver`:
  12. [tabs]
  13. ======
  14. Java::
  15. +
  16. [source,java,role="primary"]
  17. ----
  18. JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver
  19. .fromTrustedIssuers("https://idp.example.org/issuerOne", "https://idp.example.org/issuerTwo");
  20. http
  21. .authorizeExchange((authorize) -> authorize
  22. .anyExchange().authenticated()
  23. )
  24. .oauth2ResourceServer((oauth2) -> oauth2
  25. .authenticationManagerResolver(authenticationManagerResolver)
  26. );
  27. ----
  28. Kotlin::
  29. +
  30. [source,kotlin,role="secondary"]
  31. ----
  32. val customAuthenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver
  33. .fromTrustedIssuers("https://idp.example.org/issuerOne", "https://idp.example.org/issuerTwo")
  34. return http {
  35. authorizeExchange {
  36. authorize(anyExchange, authenticated)
  37. }
  38. oauth2ResourceServer {
  39. authenticationManagerResolver = customAuthenticationManagerResolver
  40. }
  41. }
  42. ----
  43. ======
  44. This is nice because the issuer endpoints are loaded lazily.
  45. In fact, the corresponding `JwtReactiveAuthenticationManager` is instantiated only when the first request with the corresponding issuer is sent.
  46. This allows for an application startup that is independent from those authorization servers being up and available.
  47. ==== Dynamic Tenants
  48. You may not want to restart the application each time a new tenant is added.
  49. In this case, you can configure the `JwtIssuerReactiveAuthenticationManagerResolver` with a repository of `ReactiveAuthenticationManager` instances, which you can edit at runtime:
  50. [tabs]
  51. ======
  52. Java::
  53. +
  54. [source,java,role="primary"]
  55. ----
  56. private Mono<ReactiveAuthenticationManager> addManager(
  57. Map<String, ReactiveAuthenticationManager> authenticationManagers, String issuer) {
  58. return Mono.fromCallable(() -> ReactiveJwtDecoders.fromIssuerLocation(issuer))
  59. .subscribeOn(Schedulers.boundedElastic())
  60. .map(JwtReactiveAuthenticationManager::new)
  61. .doOnNext((authenticationManager) -> authenticationManager.put(issuer, authenticationManager));
  62. }
  63. // ...
  64. JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver =
  65. new JwtIssuerReactiveAuthenticationManagerResolver(authenticationManagers::get);
  66. http
  67. .authorizeExchange((authorize) -> authorize
  68. .anyExchange().authenticated()
  69. )
  70. .oauth2ResourceServer((oauth2) -> oauth2
  71. .authenticationManagerResolver(authenticationManagerResolver)
  72. );
  73. ----
  74. Kotlin::
  75. +
  76. [source,kotlin,role="secondary"]
  77. ----
  78. private fun addManager(
  79. authenticationManagers: MutableMap<String, ReactiveAuthenticationManager>, issuer: String): Mono<JwtReactiveAuthenticationManager> {
  80. return Mono.fromCallable { ReactiveJwtDecoders.fromIssuerLocation(issuer) }
  81. .subscribeOn(Schedulers.boundedElastic())
  82. .map { jwtDecoder: ReactiveJwtDecoder -> JwtReactiveAuthenticationManager(jwtDecoder) }
  83. .doOnNext { authenticationManager: JwtReactiveAuthenticationManager -> authenticationManagers[issuer] = authenticationManager }
  84. }
  85. // ...
  86. var customAuthenticationManagerResolver = JwtIssuerReactiveAuthenticationManagerResolver(authenticationManagers::get)
  87. return http {
  88. authorizeExchange {
  89. authorize(anyExchange, authenticated)
  90. }
  91. oauth2ResourceServer {
  92. authenticationManagerResolver = customAuthenticationManagerResolver
  93. }
  94. }
  95. ----
  96. ======
  97. In this case, you construct `JwtIssuerReactiveAuthenticationManagerResolver` with a strategy for obtaining the `ReactiveAuthenticationManager` given to the issuer.
  98. This approach lets us add and remove elements from the repository (shown as a `Map` in the preceding snippet) at runtime.
  99. [NOTE]
  100. ====
  101. It would be unsafe to simply take any issuer and construct an `ReactiveAuthenticationManager` from it.
  102. The issuer should be one that the code can verify from a trusted source, such as an allowed list of issuers.
  103. ====