Skip to content

Commit 3cc89f9

Browse files
author
Steve Riesenberg
committed
Polish "How-to: Implement core services with JPA"
Issue gh-690 Issue gh-714
1 parent 38e37ae commit 3cc89f9

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

docs/src/docs/asciidoc/guides/how-to-jpa.adoc

+32-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
[[how-to-jpa]]
22
= How-to: Implement core services with JPA
3-
:toc: left
4-
:toclevels: 3
53
:index-link: ../how-to.html
64
:docs-dir: ..
75
:examples-dir: ../examples
86

97
[[jpa-getting-started]]
108
== Getting Started
119

12-
In this guide, we will demonstrate how to implement the xref:{docs-dir}/core-model-components.adoc#core-model-components[core services] of xref:{docs-dir}/index.adoc#top[Spring Authorization Server] with JPA. The purpose of this guide is to provide a starting point for implementing these services yourself, with the intention that you will make modifications as necessary to suit your needs.
10+
This guide shows how to implement the xref:{docs-dir}/core-model-components.adoc#core-model-components[core services] of xref:{docs-dir}/index.adoc#top[Spring Authorization Server] with JPA.
11+
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.
1312

1413
[[jpa-define-data-model]]
1514
== Define the data model
1615

17-
This guide seeks to provide a starting point for the data model and uses the simplest possible structure and data types. To come up with the initial schema, we begin by reviewing the xref:{docs-dir}/core-model-components.adoc#core-model-components[domain objects] used by the core services. Please note:
16+
This guide provides a starting point for the data model and uses the simplest possible structure and data types.
17+
To come up with the initial schema, we begin by reviewing the xref:{docs-dir}/core-model-components.adoc#core-model-components[domain objects] used by the core services.
1818

19-
NOTE: Except for token, state, metadata, settings and claims values, we will use the JPA default column length of 255 for all columns. In reality, the length and even type of columns you use may need to be customized. Your mileage may vary and you are encouraged to experiment and test before deploying to production.
19+
NOTE: Except for token, state, metadata, settings, and claims values, we use the JPA default column length of 255 for all columns.
20+
In reality, the length and even type of columns you use may need to be customized.
21+
You are encouraged to experiment and test before deploying to production.
2022

2123
[[jpa-client-schema]]
2224
=== Client Schema
2325

24-
The xref:{docs-dir}/core-model-components.adoc#registered-client[`RegisteredClient`] domain object contains a few multi-valued fields and some settings fields that require storing arbitrary key/value data. The following is an example that we will use to create a JPA entity.
26+
The xref:{docs-dir}/core-model-components.adoc#registered-client[`RegisteredClient`] domain object contains a few multi-valued fields and some settings fields that require storing arbitrary key/value data.
27+
The following listing shows the `client` schema.
2528

2629
.Client Schema
2730
[source,sql]
@@ -32,11 +35,13 @@ include::{examples-dir}/src/main/resources/oauth2-registered-client-schema.sql[]
3235
[[jpa-authorization-schema]]
3336
=== Authorization Schema
3437

35-
The xref:{docs-dir}/core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object is more complex, and contains several multi-valued fields as well as numerous arbitrarily long token values, metadata, settings and claims values. The built-in JDBC implementation utilizes a flattened structure that prefers performance over normalization, which we adopt here as well.
38+
The xref:{docs-dir}/core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object is more complex and contains several multi-valued fields as well as numerous arbitrarily long token values, metadata, settings and claims values.
39+
The built-in JDBC implementation utilizes a flattened structure that prefers performance over normalization, which we adopt here as well.
3640

37-
CAUTION: It has been difficult to find a flattened database schema that works well in all cases and with all database vendors. You may need to normalize or heavily alter the following schema for your needs.
41+
CAUTION: It has been difficult to find a flattened database schema that works well in all cases and with all database vendors.
42+
You may need to normalize or heavily alter the following schema for your needs.
3843

39-
The following is an example that we will use to create a JPA entity.
44+
The following listing shows the `authorization` schema.
4045

4146
.Authorization Schema
4247
[source,sql]
@@ -47,7 +52,8 @@ include::{examples-dir}/src/main/resources/oauth2-authorization-schema.sql[]
4752
[[jpa-authorization-consent-schema]]
4853
=== Authorization Consent Schema
4954

50-
The xref:{docs-dir}/core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object is the simplest to model, and only contains a single multi-valued field in addition to a composite key. The following is an example that we will use to create a JPA entity.
55+
The xref:{docs-dir}/core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object is the simplest to model and contains only a single multi-valued field in addition to a composite key.
56+
The following listing shows the `authorizationConsent` schema.
5157

5258
.Authorization Consent Schema
5359
[source,sql]
@@ -60,12 +66,13 @@ include::{examples-dir}/src/main/resources/oauth2-authorization-consent-schema.s
6066

6167
The preceding schema examples provide a reference for the structure of the entities we need to create.
6268

63-
NOTE: The following entities are minimally annotated and are just examples. They allow the schema to be created dynamically and therefore do not require the above sql scripts to be executed manually.
69+
NOTE: The following entities are minimally annotated and are just examples.
70+
They allow the schema to be created dynamically and therefore do not require the above sql scripts to be executed manually.
6471

6572
[[jpa-client-entity]]
6673
=== Client Entity
6774

68-
The following is an example of the `Client` entity which is used to persist information mapped from the xref:{docs-dir}/core-model-components.adoc#registered-client[`RegisteredClient`] domain object.
75+
The following listing shows the `Client` entity, which is used to persist information mapped from the xref:{docs-dir}/core-model-components.adoc#registered-client[`RegisteredClient`] domain object.
6976

7077
.Client Entity
7178
[source,java]
@@ -76,7 +83,7 @@ include::{examples-dir}/src/main/java/sample/jpa/Client.java[tag=class]
7683
[[jpa-authorization-entity]]
7784
=== Authorization Entity
7885

79-
The following is an example of the `Authorization` entity which is used to persist information mapped from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object.
86+
The following listing shows the `Authorization` entity, which is used to persist information mapped from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object.
8087

8188
.Authorization Entity
8289
[source,java]
@@ -87,7 +94,7 @@ include::{examples-dir}/src/main/java/sample/jpa/Authorization.java[tag=class]
8794
[[jpa-authorization-consent-entity]]
8895
=== Authorization Consent Entity
8996

90-
The following is an example of the `AuthorizationConsent` entity which is used to persist information mapped from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object.
97+
The following listing shows the `AuthorizationConsent` entity, which is used to persist information mapped from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object.
9198

9299
.Authorization Consent Entity
93100
[source,java]
@@ -103,7 +110,7 @@ By closely examining the interfaces of each core service and reviewing the `Jdbc
103110
[[jpa-client-repository]]
104111
=== Client Repository
105112

106-
The following is an example of the `ClientRepository` capable of finding a <<jpa-client-entity,`Client`>> by the `id` and `clientId` fields.
113+
The following listing shows the `ClientRepository`, which is able to find a <<jpa-client-entity,`Client`>> by the `id` and `clientId` fields.
107114

108115
.Client Repository
109116
[source,java]
@@ -114,7 +121,8 @@ include::{examples-dir}/src/main/java/sample/jpa/ClientRepository.java[tag=class
114121
[[jpa-authorization-repository]]
115122
=== Authorization Repository
116123

117-
The following is an example of the `AuthorizationRepository` capable of finding an <<jpa-authorization-entity,`Authorization`>> by the `id` field as well as the `state`, `authorizationCodeValue`, `accessTokenValue` and `refreshTokenValue` token fields. It also allows querying a combination of token fields.
124+
The following listing shows the `AuthorizationRepository`, which is able to find an <<jpa-authorization-entity,`Authorization`>> by the `id` field as well as the `state`, `authorizationCodeValue`, `accessTokenValue` and `refreshTokenValue` token fields.
125+
It also allows querying a combination of token fields.
118126

119127
.Authorization Repository
120128
[source,java]
@@ -125,7 +133,7 @@ include::{examples-dir}/src/main/java/sample/jpa/AuthorizationRepository.java[ta
125133
[[jpa-authorization-consent-repository]]
126134
=== Authorization Consent Repository
127135

128-
The following is an example of the `AuthorizationConsentRepository` capable of finding and deleting an <<jpa-authorization-consent-entity,`AuthorizationConsent`>> by the `registeredClientId` and `principalName` fields, which form a composite primary key.
136+
The following listing shows the `AuthorizationConsentRepository`, which is able to find and delete an <<jpa-authorization-consent-entity,`AuthorizationConsent`>> by the `registeredClientId` and `principalName` fields that form a composite primary key.
129137

130138
.Authorization Consent Repository
131139
[source,java]
@@ -136,14 +144,16 @@ include::{examples-dir}/src/main/java/sample/jpa/AuthorizationConsentRepository.
136144
[[jpa-implement-core-services]]
137145
== Implement core services
138146

139-
With the above <<jpa-create-jpa-entities,entities>> and <<jpa-create-spring-data-repositories,repositories>>, we can begin implementing the core services. By reviewing the `Jdbc` implementations, we can derive a minimal set of internal utilities for converting to/from string values for enumerations and reading/writing JSON data for attributes, settings, metadata and claims fields.
147+
With the above <<jpa-create-jpa-entities,entities>> and <<jpa-create-spring-data-repositories,repositories>>, we can begin implementing the core services.
148+
By reviewing the `Jdbc` implementations, we can derive a minimal set of internal utilities for converting to and from string values for enumerations and reading and writing JSON data for attributes, settings, metadata and claims fields.
140149

141-
CAUTION: Keep in mind that writing JSON data to text columns with a fixed length has proven problematic with the `Jdbc` implementations. While these examples continue to do so, you may need to split these fields out into a separate table or data store that supports arbitrarily long data values.
150+
CAUTION: Keep in mind that writing JSON data to text columns with a fixed length has proven problematic with the `Jdbc` implementations.
151+
While these examples continue to do so, you may need to split these fields out into a separate table or data store that supports arbitrarily long data values.
142152

143153
[[jpa-registered-client-repository]]
144154
=== Registered Client Repository
145155

146-
The following is an example of the `JpaRegisteredClientRepository` which uses a <<jpa-client-repository,`ClientRepository`>> for persisting a <<jpa-client-entity,`Client`>>, and maps to/from the xref:{docs-dir}/core-model-components.adoc#registered-client[`RegisteredClient`] domain object.
156+
The following listing shows the `JpaRegisteredClientRepository`, which uses a <<jpa-client-repository,`ClientRepository`>> for persisting a <<jpa-client-entity,`Client`>> and maps to and from the xref:{docs-dir}/core-model-components.adoc#registered-client[`RegisteredClient`] domain object.
147157

148158
.`RegisteredClientRepository` Implementation
149159
[source,java]
@@ -154,7 +164,7 @@ include::{examples-dir}/src/main/java/sample/jpa/JpaRegisteredClientRepository.j
154164
[[jpa-authorization-service]]
155165
=== Authorization Service
156166

157-
The following is an example of the `JpaOAuth2AuthorizationService` which uses an <<jpa-authorization-repository,`AuthorizationRepository`>> for persisting an <<jpa-authorization-entity,`Authorization`>>, and maps to/from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object.
167+
The following listing shows the `JpaOAuth2AuthorizationService`, which uses an <<jpa-authorization-repository,`AuthorizationRepository`>> for persisting an <<jpa-authorization-entity,`Authorization`>> and maps to and from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`] domain object.
158168

159169
.`OAuth2AuthorizationService` Implementation
160170
[source,java]
@@ -165,7 +175,7 @@ include::{examples-dir}/src/main/java/sample/jpa/JpaOAuth2AuthorizationService.j
165175
[[jpa-authorization-consent-service]]
166176
=== Authorization Consent Service
167177

168-
The following is an example of the `JpaOAuth2AuthorizationConsentService` which uses an <<jpa-authorization-consent-repository,`AuthorizationConsentRepository`>> for persisting an <<jpa-authorization-consent-entity,`AuthorizationConsent`>>, and maps to/from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object.
178+
The following listing shows the `JpaOAuth2AuthorizationConsentService`, which uses an <<jpa-authorization-consent-repository,`AuthorizationConsentRepository`>> for persisting an <<jpa-authorization-consent-entity,`AuthorizationConsent`>> and maps to and from the xref:{docs-dir}/core-model-components.adoc#oauth2-authorization-consent[`OAuth2AuthorizationConsent`] domain object.
169179

170180
.`OAuth2AuthorizationConsentService` Implementation
171181
[source,java]

0 commit comments

Comments
 (0)