how-to-redis.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. [[how-to-redis]]
  2. = How-to: Implement core services with Redis
  3. :index-link: ../how-to.html
  4. :docs-dir: ..
  5. This guide shows how to implement the xref:core-model-components.adoc[core services] of xref:index.adoc[Spring Authorization Server] with https://redis.io/[Redis].
  6. The purpose of this guide is to provide a starting point for implementing these services yourself, with the intention that you can make modifications to suit your needs.
  7. * xref:guides/how-to-redis.adoc#define-entity-model[Define the entity model]
  8. * xref:guides/how-to-redis.adoc#create-spring-data-repositories[Create Spring Data repositories]
  9. * xref:guides/how-to-redis.adoc#implement-core-services[Implement core services]
  10. * xref:guides/how-to-redis.adoc#configure-core-services[Configure core services]
  11. TIP: The code samples provided in this guide are located in the https://github.com/spring-projects/spring-authorization-server/tree/main/docs/src/main/java/sample[documentation samples] directory under the *_redis_* subdirectory.
  12. [[define-entity-model]]
  13. == Define the entity model
  14. The following defines the entity model representation for the `RegisteredClient`, `OAuth2Authorization` and `OAuth2AuthorizationConsent` domain classes.
  15. * xref:guides/how-to-redis.adoc#registered-client-entity[Registered Client Entity]
  16. * xref:guides/how-to-redis.adoc#authorization-grant-entity[Authorization Grant _Base_ Entity]
  17. * xref:guides/how-to-redis.adoc#oauth2-authorization-code-grant-entity[Authorization Code Grant Entity (OAuth 2.0)]
  18. * xref:guides/how-to-redis.adoc#oidc-authorization-code-grant-entity[Authorization Code Grant Entity (OpenID Connect 1.0)]
  19. * xref:guides/how-to-redis.adoc#client-credentials-grant-entity[Client Credentials Grant Entity]
  20. * xref:guides/how-to-redis.adoc#device-code-grant-entity[Device Code Grant Entity]
  21. * xref:guides/how-to-redis.adoc#token-exchange-grant-entity[Token Exchange Grant Entity]
  22. * xref:guides/how-to-redis.adoc#authorization-consent-entity[Authorization Consent Entity]
  23. [[registered-client-entity]]
  24. === Registered Client Entity
  25. The following listing shows the `OAuth2RegisteredClient` entity, which is used to persist information mapped from the xref:core-model-components.adoc#registered-client[`RegisteredClient`] domain class.
  26. .OAuth2RegisteredClient Entity
  27. [source,java]
  28. ----
  29. include::{examples-dir}/main/java/sample/redis/entity/OAuth2RegisteredClient.java[]
  30. ----
  31. TIP: Click on the "Expand folded text" icon in the code sample above to display the full example.
  32. [[authorization-grant-entity]]
  33. === Authorization Grant _Base_ Entity
  34. The entity model for the xref:core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain class is designed with a class hierarchy based on authorization grant type.
  35. The following listing shows the `OAuth2AuthorizationGrantAuthorization` _base_ entity, which defines common attributes for each authorization grant type.
  36. .OAuth2AuthorizationGrantAuthorization _Base_ Entity
  37. [source,java]
  38. ----
  39. include::{examples-dir}/main/java/sample/redis/entity/OAuth2AuthorizationGrantAuthorization.java[]
  40. ----
  41. [[oauth2-authorization-code-grant-entity]]
  42. === Authorization Code Grant Entity (OAuth 2.0)
  43. The following listing shows the `OAuth2AuthorizationCodeGrantAuthorization` entity, which extends `OAuth2AuthorizationGrantAuthorization`, and defines additional attributes for the OAuth 2.0 `authorization_code` grant type.
  44. .OAuth2AuthorizationCodeGrantAuthorization Entity
  45. [source,java]
  46. ----
  47. include::{examples-dir}/main/java/sample/redis/entity/OAuth2AuthorizationCodeGrantAuthorization.java[]
  48. ----
  49. [[oidc-authorization-code-grant-entity]]
  50. === Authorization Code Grant Entity (OpenID Connect 1.0)
  51. The following listing shows the `OidcAuthorizationCodeGrantAuthorization` entity, which extends `OAuth2AuthorizationCodeGrantAuthorization`, and defines additional attributes for the OpenID Connect 1.0 `authorization_code` grant type.
  52. .OidcAuthorizationCodeGrantAuthorization Entity
  53. [source,java]
  54. ----
  55. include::{examples-dir}/main/java/sample/redis/entity/OidcAuthorizationCodeGrantAuthorization.java[]
  56. ----
  57. [[client-credentials-grant-entity]]
  58. === Client Credentials Grant Entity
  59. The following listing shows the `OAuth2ClientCredentialsGrantAuthorization` entity, which extends `OAuth2AuthorizationGrantAuthorization`, for the `client_credentials` grant type.
  60. .OAuth2ClientCredentialsGrantAuthorization Entity
  61. [source,java]
  62. ----
  63. include::{examples-dir}/main/java/sample/redis/entity/OAuth2ClientCredentialsGrantAuthorization.java[]
  64. ----
  65. [[device-code-grant-entity]]
  66. === Device Code Grant Entity
  67. The following listing shows the `OAuth2DeviceCodeGrantAuthorization` entity, which extends `OAuth2AuthorizationGrantAuthorization`, and defines additional attributes for the `urn:ietf:params:oauth:grant-type:device_code` grant type.
  68. .OAuth2DeviceCodeGrantAuthorization Entity
  69. [source,java]
  70. ----
  71. include::{examples-dir}/main/java/sample/redis/entity/OAuth2DeviceCodeGrantAuthorization.java[]
  72. ----
  73. [[token-exchange-grant-entity]]
  74. === Token Exchange Grant Entity
  75. The following listing shows the `OAuth2TokenExchangeGrantAuthorization` entity, which extends `OAuth2AuthorizationGrantAuthorization`, for the `urn:ietf:params:oauth:grant-type:token-exchange` grant type.
  76. .OAuth2TokenExchangeGrantAuthorization Entity
  77. [source,java]
  78. ----
  79. include::{examples-dir}/main/java/sample/redis/entity/OAuth2TokenExchangeGrantAuthorization.java[]
  80. ----
  81. [[authorization-consent-entity]]
  82. === Authorization Consent Entity
  83. The following listing shows the `OAuth2UserConsent` entity, which is used to persist information mapped from the xref:core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain class.
  84. .OAuth2UserConsent Entity
  85. [source,java]
  86. ----
  87. include::{examples-dir}/main/java/sample/redis/entity/OAuth2UserConsent.java[]
  88. ----
  89. [[create-spring-data-repositories]]
  90. == Create Spring Data repositories
  91. By closely examining the interfaces of each core service and reviewing the `Jdbc` implementations, we can derive a minimal set of queries needed for supporting a Redis version of each interface.
  92. * xref:guides/how-to-redis.adoc#registered-client-repository[Registered Client Repository]
  93. * xref:guides/how-to-redis.adoc#authorization-grant-repository[Authorization Grant Repository]
  94. * xref:guides/how-to-redis.adoc#authorization-consent-repository[Authorization Consent Repository]
  95. [[registered-client-repository]]
  96. === Registered Client Repository
  97. The following listing shows the `OAuth2RegisteredClientRepository`, which is able to find a xref:guides/how-to-redis.adoc#registered-client-entity[`OAuth2RegisteredClient`] by the `id` and `clientId` fields.
  98. .OAuth2RegisteredClientRepository
  99. [source,java]
  100. ----
  101. include::{examples-dir}/main/java/sample/redis/repository/OAuth2RegisteredClientRepository.java[]
  102. ----
  103. [[authorization-grant-repository]]
  104. === Authorization Grant Repository
  105. The following listing shows the `OAuth2AuthorizationGrantAuthorizationRepository`, which is able to find an xref:guides/how-to-redis.adoc#authorization-grant-entity[`OAuth2AuthorizationGrantAuthorization`] by the `id` field as well as by `state`, `authorizationCode`, `accessToken`, `refreshToken`, `idToken`, `deviceState`, `userCode` and `deviceCode` values.
  106. .OAuth2AuthorizationGrantAuthorizationRepository
  107. [source,java]
  108. ----
  109. include::{examples-dir}/main/java/sample/redis/repository/OAuth2AuthorizationGrantAuthorizationRepository.java[]
  110. ----
  111. [[authorization-consent-repository]]
  112. === Authorization Consent Repository
  113. The following listing shows the `OAuth2UserConsentRepository`, which is able to find and delete an xref:guides/how-to-redis.adoc#authorization-consent-entity[`OAuth2UserConsent`] by the `registeredClientId` and `principalName` fields that form the composite primary key.
  114. .OAuth2UserConsentRepository
  115. [source,java]
  116. ----
  117. include::{examples-dir}/main/java/sample/redis/repository/OAuth2UserConsentRepository.java[]
  118. ----
  119. [[implement-core-services]]
  120. == Implement core services
  121. With the above xref:guides/how-to-redis.adoc#define-entity-model[entities] and xref:guides/how-to-redis.adoc#create-spring-data-repositories[repositories], we can begin implementing the core services.
  122. TIP: The core services make use of the `ModelMapper` utility class for converting to and from the domain object (e.g. `RegisteredClient`) to the entity model representation (e.g. `OAuth2RegisteredClient`).
  123. * xref:guides/how-to-redis.adoc#redis-registered-client-repository[Registered Client Repository]
  124. * xref:guides/how-to-redis.adoc#redis-authorization-service[Authorization Service]
  125. * xref:guides/how-to-redis.adoc#redis-authorization-consent-service[Authorization Consent Service]
  126. [[redis-registered-client-repository]]
  127. === Registered Client Repository
  128. The following listing shows the `RedisRegisteredClientRepository`, which uses an xref:guides/how-to-redis.adoc#registered-client-repository[`OAuth2RegisteredClientRepository`] for persisting an xref:guides/how-to-redis.adoc#registered-client-entity[`OAuth2RegisteredClient`] and maps to and from the xref:core-model-components.adoc#registered-client[`RegisteredClient`] domain object, using the `ModelMapper` utility class.
  129. .RedisRegisteredClientRepository
  130. [source,java]
  131. ----
  132. include::{examples-dir}/main/java/sample/redis/service/RedisRegisteredClientRepository.java[]
  133. ----
  134. [[redis-authorization-service]]
  135. === Authorization Service
  136. The following listing shows the `RedisOAuth2AuthorizationService`, which uses an xref:guides/how-to-redis.adoc#authorization-grant-repository[`OAuth2AuthorizationGrantAuthorizationRepository`] for persisting an xref:guides/how-to-redis.adoc#authorization-grant-entity[`OAuth2AuthorizationGrantAuthorization`] and maps to and from the xref:core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object, using the `ModelMapper` utility class.
  137. .RedisOAuth2AuthorizationService
  138. [source,java]
  139. ----
  140. include::{examples-dir}/main/java/sample/redis/service/RedisOAuth2AuthorizationService.java[]
  141. ----
  142. [[redis-authorization-consent-service]]
  143. === Authorization Consent Service
  144. The following listing shows the `RedisOAuth2AuthorizationConsentService`, which uses an xref:guides/how-to-redis.adoc#authorization-consent-repository[`OAuth2UserConsentRepository`] for persisting an xref:guides/how-to-redis.adoc#authorization-consent-entity[`OAuth2UserConsent`] and maps to and from the xref:core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object, using the `ModelMapper` utility class.
  145. .RedisOAuth2AuthorizationConsentService
  146. [source,java]
  147. ----
  148. include::{examples-dir}/main/java/sample/redis/service/RedisOAuth2AuthorizationConsentService.java[]
  149. ----
  150. [[configure-core-services]]
  151. == Configure core services
  152. The following example shows how to configure the core services:
  153. .RedisConfig
  154. [source,java]
  155. ----
  156. include::{examples-dir}/main/java/sample/redis/config/RedisConfig.java[]
  157. ----
  158. <1> Activate the Spring Data Redis repositories under the `sample.redis.repository` base package.
  159. <2> Use the https://docs.spring.io/spring-data/redis/reference/redis/drivers.html#redis:connectors:jedis[Jedis] Connector.
  160. <3> Register the custom ``Converter``'s that perform the Object-to-Hash conversion before persisting to Redis.
  161. <4> Register the `RedisRegisteredClientRepository` with the activated `OAuth2RegisteredClientRepository`.
  162. <5> Register the `RedisOAuth2AuthorizationService` with the activated `OAuth2AuthorizationGrantAuthorizationRepository`.
  163. <6> Register the `RedisOAuth2AuthorizationConsentService` with the activated `OAuth2UserConsentRepository`.