core-model-components.adoc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. [[core-model-components]]
  2. = Core Model / Components
  3. [[registered-client]]
  4. == RegisteredClient
  5. A `RegisteredClient` is a representation of a client that is https://datatracker.ietf.org/doc/html/rfc6749#section-2[registered] with the authorization server.
  6. A client must be registered with the authorization server before it can initiate an authorization grant flow, such as `authorization_code` or `client_credentials`.
  7. During client registration, the client is assigned a unique https://datatracker.ietf.org/doc/html/rfc6749#section-2.2[client identifier], (optionally) a client secret (depending on https://datatracker.ietf.org/doc/html/rfc6749#section-2.1[client type]), and metadata associated with its unique client identifier.
  8. The client's metadata can range from human-facing display strings (such as client name) to items specific to a protocol flow (such as the list of valid redirect URIs).
  9. [TIP]
  10. The corresponding client registration model in Spring Security's OAuth2 Client support is {spring-security-reference-base-url}/servlet/oauth2/client/core.html#oauth2Client-client-registration[ClientRegistration].
  11. The primary purpose of a client is to request access to protected resources.
  12. The client first requests an access token by authenticating with the authorization server and presenting the authorization grant.
  13. The authorization server authenticates the client and authorization grant, and, if they are valid, issues an access token.
  14. The client can now request the protected resource from the resource server by presenting the access token.
  15. The following example shows how to configure a `RegisteredClient` that is allowed to perform the https://datatracker.ietf.org/doc/html/rfc6749#section-4.1[authorization_code grant] flow to request an access token:
  16. [source,java]
  17. ----
  18. RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
  19. .clientId("client-a")
  20. .clientSecret("{noop}secret") <1>
  21. .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
  22. .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
  23. .redirectUri("http://127.0.0.1:8080/authorized")
  24. .scope("scope-a")
  25. .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
  26. .build();
  27. ----
  28. <1> `\{noop\}` represents the `PasswordEncoder` id for Spring Security's {spring-security-reference-base-url}/features/authentication/password-storage.html#authentication-password-storage-dpe[NoOpPasswordEncoder].
  29. The corresponding configuration in Spring Security's {spring-security-reference-base-url}/servlet/oauth2/client/index.html[OAuth2 Client support] is:
  30. [source,yaml]
  31. ----
  32. spring:
  33. security:
  34. oauth2:
  35. client:
  36. registration:
  37. client-a:
  38. provider: spring
  39. client-id: client-a
  40. client-secret: secret
  41. authorization-grant-type: authorization_code
  42. redirect-uri: "http://127.0.0.1:8080/authorized"
  43. scope: scope-a
  44. provider:
  45. spring:
  46. issuer-uri: http://localhost:9000
  47. ----
  48. A `RegisteredClient` has metadata (attributes) associated with its unique Client Identifier and is defined as follows:
  49. [source,java]
  50. ----
  51. public class RegisteredClient implements Serializable {
  52. private String id; <1>
  53. private String clientId; <2>
  54. private Instant clientIdIssuedAt; <3>
  55. private String clientSecret; <4>
  56. private Instant clientSecretExpiresAt; <5>
  57. private String clientName; <6>
  58. private Set<ClientAuthenticationMethod> clientAuthenticationMethods; <7>
  59. private Set<AuthorizationGrantType> authorizationGrantTypes; <8>
  60. private Set<String> redirectUris; <9>
  61. private Set<String> postLogoutRedirectUris; <10>
  62. private Set<String> scopes; <11>
  63. private ClientSettings clientSettings; <12>
  64. private TokenSettings tokenSettings; <13>
  65. ...
  66. }
  67. ----
  68. <1> `id`: The ID that uniquely identifies the `RegisteredClient`.
  69. <2> `clientId`: The client identifier.
  70. <3> `clientIdIssuedAt`: The time at which the client identifier was issued.
  71. <4> `clientSecret`: The client's secret. The value should be encoded using Spring Security's {spring-security-reference-base-url}/features/authentication/password-storage.html#authentication-password-storage-dpe[PasswordEncoder].
  72. <5> `clientSecretExpiresAt`: The time at which the client secret expires.
  73. <6> `clientName`: A descriptive name used for the client. The name may be used in certain scenarios, such as when displaying the client name in the consent page.
  74. <7> `clientAuthenticationMethods`: The authentication method(s) that the client may use. The supported values are `client_secret_basic`, `client_secret_post`, https://datatracker.ietf.org/doc/html/rfc7523[`private_key_jwt`], `client_secret_jwt`, and `none` https://datatracker.ietf.org/doc/html/rfc7636[(public clients)].
  75. <8> `authorizationGrantTypes`: The https://datatracker.ietf.org/doc/html/rfc6749#section-1.3[authorization grant type(s)] that the client can use. The supported values are `authorization_code`, `client_credentials`, `refresh_token`, and `urn:ietf:params:oauth:grant-type:device_code`.
  76. <9> `redirectUris`: The registered https://datatracker.ietf.org/doc/html/rfc6749#section-3.1.2[redirect URI(s)] that the client may use in redirect-based flows – for example, `authorization_code` grant.
  77. <10> `postLogoutRedirectUris`: The post logout redirect URI(s) that the client may use for logout.
  78. <11> `scopes`: The scope(s) that the client is allowed to request.
  79. <12> `clientSettings`: The custom settings for the client – for example, require https://datatracker.ietf.org/doc/html/rfc7636[PKCE], require authorization consent, and others.
  80. <13> `tokenSettings`: The custom settings for the OAuth2 tokens issued to the client – for example, access/refresh token time-to-live, reuse refresh tokens, and others.
  81. [[registered-client-repository]]
  82. == RegisteredClientRepository
  83. The `RegisteredClientRepository` is the central component where new clients can be registered and existing clients can be queried.
  84. It is used by other components when following a specific protocol flow, such as client authentication, authorization grant processing, token introspection, dynamic client registration, and others.
  85. The provided implementations of `RegisteredClientRepository` are `InMemoryRegisteredClientRepository` and `JdbcRegisteredClientRepository`.
  86. The `InMemoryRegisteredClientRepository` implementation stores `RegisteredClient` instances in-memory and is recommended *ONLY* to be used during development and testing.
  87. `JdbcRegisteredClientRepository` is a JDBC implementation that persists `RegisteredClient` instances by using `JdbcOperations`.
  88. [NOTE]
  89. The `RegisteredClientRepository` is a *REQUIRED* component.
  90. The following example shows how to register a `RegisteredClientRepository` `@Bean`:
  91. [source,java]
  92. ----
  93. @Bean
  94. public RegisteredClientRepository registeredClientRepository() {
  95. List<RegisteredClient> registrations = ...
  96. return new InMemoryRegisteredClientRepository(registrations);
  97. }
  98. ----
  99. Alternatively, you can configure the `RegisteredClientRepository` through the xref:configuration-model.adoc#customizing-the-configuration[`OAuth2AuthorizationServerConfigurer`]:
  100. [source,java]
  101. ----
  102. @Bean
  103. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  104. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  105. new OAuth2AuthorizationServerConfigurer();
  106. http.apply(authorizationServerConfigurer);
  107. authorizationServerConfigurer
  108. .registeredClientRepository(registeredClientRepository);
  109. ...
  110. return http.build();
  111. }
  112. ----
  113. [NOTE]
  114. The `OAuth2AuthorizationServerConfigurer` is useful when applying multiple configuration options simultaneously.
  115. [[oauth2-authorization]]
  116. == OAuth2Authorization
  117. An `OAuth2Authorization` is a representation of an OAuth2 authorization, which holds state related to the authorization granted to a xref:core-model-components.adoc#registered-client[client], by the resource owner or itself in the case of the `client_credentials` authorization grant type.
  118. [TIP]
  119. The corresponding authorization model in Spring Security's OAuth2 Client support is {spring-security-reference-base-url}/servlet/oauth2/client/core.html#oauth2Client-authorized-client[OAuth2AuthorizedClient].
  120. After the successful completion of an authorization grant flow, an `OAuth2Authorization` is created and associates an {spring-security-api-base-url}/org/springframework/security/oauth2/core/OAuth2AccessToken.html[`OAuth2AccessToken`], an (optional) {spring-security-api-base-url}/org/springframework/security/oauth2/core/OAuth2RefreshToken.html[`OAuth2RefreshToken`], and additional state specific to the executed authorization grant type.
  121. The {spring-security-api-base-url}/org/springframework/security/oauth2/core/OAuth2Token.html[`OAuth2Token`] instances associated with an `OAuth2Authorization` vary, depending on the authorization grant type.
  122. For the OAuth2 https://datatracker.ietf.org/doc/html/rfc6749#section-4.1[authorization_code grant], an `OAuth2AuthorizationCode`, an `OAuth2AccessToken`, and an (optional) `OAuth2RefreshToken` are associated.
  123. For the OpenID Connect 1.0 https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth[authorization_code grant], an `OAuth2AuthorizationCode`, an {spring-security-api-base-url}/org/springframework/security/oauth2/core/oidc/OidcIdToken.html[`OidcIdToken`], an `OAuth2AccessToken`, and an (optional) `OAuth2RefreshToken` are associated.
  124. For the OAuth2 https://datatracker.ietf.org/doc/html/rfc6749#section-4.4[client_credentials grant], only an `OAuth2AccessToken` is associated.
  125. `OAuth2Authorization` and its attributes are defined as follows:
  126. [source,java]
  127. ----
  128. public class OAuth2Authorization implements Serializable {
  129. private String id; <1>
  130. private String registeredClientId; <2>
  131. private String principalName; <3>
  132. private AuthorizationGrantType authorizationGrantType; <4>
  133. private Set<String> authorizedScopes; <5>
  134. private Map<Class<? extends OAuth2Token>, Token<?>> tokens; <6>
  135. private Map<String, Object> attributes; <7>
  136. ...
  137. }
  138. ----
  139. <1> `id`: The ID that uniquely identifies the `OAuth2Authorization`.
  140. <2> `registeredClientId`: The ID that uniquely identifies the xref:core-model-components.adoc#registered-client[RegisteredClient].
  141. <3> `principalName`: The principal name of the resource owner (or client).
  142. <4> `authorizationGrantType`: The `AuthorizationGrantType` used.
  143. <5> `authorizedScopes`: The `Set` of scope(s) authorized for the client.
  144. <6> `tokens`: The `OAuth2Token` instances (and associated metadata) specific to the executed authorization grant type.
  145. <7> `attributes`: The additional attributes specific to the executed authorization grant type – for example, the authenticated `Principal`, `OAuth2AuthorizationRequest`, and others.
  146. `OAuth2Authorization` and its associated `OAuth2Token` instances have a set lifespan.
  147. A newly issued `OAuth2Token` is active and becomes inactive when it either expires or is invalidated (revoked).
  148. The `OAuth2Authorization` is (implicitly) inactive when all associated `OAuth2Token` instances are inactive.
  149. Each `OAuth2Token` is held in an `OAuth2Authorization.Token`, which provides accessors for `isExpired()`, `isInvalidated()`, and `isActive()`.
  150. `OAuth2Authorization.Token` also provides `getClaims()`, which returns the claims (if any) associated with the `OAuth2Token`.
  151. [[oauth2-authorization-service]]
  152. == OAuth2AuthorizationService
  153. The `OAuth2AuthorizationService` is the central component where new authorizations are stored and existing authorizations are queried.
  154. It is used by other components when following a specific protocol flow – for example, client authentication, authorization grant processing, token introspection, token revocation, dynamic client registration, and others.
  155. The provided implementations of `OAuth2AuthorizationService` are `InMemoryOAuth2AuthorizationService` and `JdbcOAuth2AuthorizationService`.
  156. The `InMemoryOAuth2AuthorizationService` implementation stores `OAuth2Authorization` instances in-memory and is recommended *ONLY* to be used during development and testing.
  157. `JdbcOAuth2AuthorizationService` is a JDBC implementation that persists `OAuth2Authorization` instances by using `JdbcOperations`.
  158. [NOTE]
  159. The `OAuth2AuthorizationService` is an *OPTIONAL* component and defaults to `InMemoryOAuth2AuthorizationService`.
  160. The following example shows how to register an `OAuth2AuthorizationService` `@Bean`:
  161. [source,java]
  162. ----
  163. @Bean
  164. public OAuth2AuthorizationService authorizationService() {
  165. return new InMemoryOAuth2AuthorizationService();
  166. }
  167. ----
  168. Alternatively, you can configure the `OAuth2AuthorizationService` through the xref:configuration-model.adoc#customizing-the-configuration[`OAuth2AuthorizationServerConfigurer`]:
  169. [source,java]
  170. ----
  171. @Bean
  172. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  173. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  174. new OAuth2AuthorizationServerConfigurer();
  175. http.apply(authorizationServerConfigurer);
  176. authorizationServerConfigurer
  177. .authorizationService(authorizationService);
  178. ...
  179. return http.build();
  180. }
  181. ----
  182. [NOTE]
  183. The `OAuth2AuthorizationServerConfigurer` is useful when applying multiple configuration options simultaneously.
  184. [[oauth2-authorization-consent]]
  185. == OAuth2AuthorizationConsent
  186. An `OAuth2AuthorizationConsent` is a representation of an authorization "consent" (decision) from an https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.1[OAuth2 authorization request flow] – for example, the `authorization_code` grant, which holds the authorities granted to a xref:core-model-components.adoc#registered-client[client] by the resource owner.
  187. When authorizing access to a client, the resource owner may grant only a subset of the authorities requested by the client.
  188. The typical use case is the `authorization_code` grant flow, in which the client requests scope(s) and the resource owner grants (or denies) access to the requested scope(s).
  189. After the completion of an OAuth2 authorization request flow, an `OAuth2AuthorizationConsent` is created (or updated) and associates the granted authorities with the client and resource owner.
  190. `OAuth2AuthorizationConsent` and its attributes are defined as follows:
  191. [source,java]
  192. ----
  193. public final class OAuth2AuthorizationConsent implements Serializable {
  194. private final String registeredClientId; <1>
  195. private final String principalName; <2>
  196. private final Set<GrantedAuthority> authorities; <3>
  197. ...
  198. }
  199. ----
  200. <1> `registeredClientId`: The ID that uniquely identifies the xref:core-model-components.adoc#registered-client[RegisteredClient].
  201. <2> `principalName`: The principal name of the resource owner.
  202. <3> `authorities`: The authorities granted to the client by the resource owner. An authority can represent a scope, a claim, a permission, a role, and others.
  203. [[oauth2-authorization-consent-service]]
  204. == OAuth2AuthorizationConsentService
  205. The `OAuth2AuthorizationConsentService` is the central component where new authorization consents are stored and existing authorization consents are queried.
  206. It is primarily used by components that implement an OAuth2 authorization request flow – for example, the `authorization_code` grant.
  207. The provided implementations of `OAuth2AuthorizationConsentService` are `InMemoryOAuth2AuthorizationConsentService` and `JdbcOAuth2AuthorizationConsentService`.
  208. The `InMemoryOAuth2AuthorizationConsentService` implementation stores `OAuth2AuthorizationConsent` instances in-memory and is recommended *ONLY* for development and testing.
  209. `JdbcOAuth2AuthorizationConsentService` is a JDBC implementation that persists `OAuth2AuthorizationConsent` instances by using `JdbcOperations`.
  210. [NOTE]
  211. The `OAuth2AuthorizationConsentService` is an *OPTIONAL* component and defaults to `InMemoryOAuth2AuthorizationConsentService`.
  212. The following example shows how to register an `OAuth2AuthorizationConsentService` `@Bean`:
  213. [source,java]
  214. ----
  215. @Bean
  216. public OAuth2AuthorizationConsentService authorizationConsentService() {
  217. return new InMemoryOAuth2AuthorizationConsentService();
  218. }
  219. ----
  220. Alternatively, you can configure the `OAuth2AuthorizationConsentService` through the xref:configuration-model.adoc#customizing-the-configuration[`OAuth2AuthorizationServerConfigurer`]:
  221. [source,java]
  222. ----
  223. @Bean
  224. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  225. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  226. new OAuth2AuthorizationServerConfigurer();
  227. http.apply(authorizationServerConfigurer);
  228. authorizationServerConfigurer
  229. .authorizationConsentService(authorizationConsentService);
  230. ...
  231. return http.build();
  232. }
  233. ----
  234. [NOTE]
  235. The `OAuth2AuthorizationServerConfigurer` is useful when applying multiple configuration options simultaneously.
  236. [[oauth2-token-context]]
  237. == OAuth2TokenContext
  238. An `OAuth2TokenContext` is a context object that holds information associated with an `OAuth2Token` and is used by an xref:core-model-components.adoc#oauth2-token-generator[OAuth2TokenGenerator] and xref:core-model-components.adoc#oauth2-token-customizer[OAuth2TokenCustomizer].
  239. `OAuth2TokenContext` provides the following accessors:
  240. [source,java]
  241. ----
  242. public interface OAuth2TokenContext extends Context {
  243. default RegisteredClient getRegisteredClient() ... <1>
  244. default <T extends Authentication> T getPrincipal() ... <2>
  245. default AuthorizationServerContext getAuthorizationServerContext() ... <3>
  246. @Nullable
  247. default OAuth2Authorization getAuthorization() ... <4>
  248. default Set<String> getAuthorizedScopes() ... <5>
  249. default OAuth2TokenType getTokenType() ... <6>
  250. default AuthorizationGrantType getAuthorizationGrantType() ... <7>
  251. default <T extends Authentication> T getAuthorizationGrant() ... <8>
  252. ...
  253. }
  254. ----
  255. <1> `getRegisteredClient()`: The xref:core-model-components.adoc#registered-client[RegisteredClient] associated with the authorization grant.
  256. <2> `getPrincipal()`: The `Authentication` instance of the resource owner (or client).
  257. <3> `getAuthorizationServerContext()`: The xref:configuration-model.adoc#configuring-authorization-server-settings[`AuthorizationServerContext`] object that holds information of the Authorization Server runtime environment.
  258. <4> `getAuthorization()`: The xref:core-model-components.adoc#oauth2-authorization[OAuth2Authorization] associated with the authorization grant.
  259. <5> `getAuthorizedScopes()`: The scope(s) authorized for the client.
  260. <6> `getTokenType()`: The `OAuth2TokenType` to generate. The supported values are `code`, `access_token`, `refresh_token`, and `id_token`.
  261. <7> `getAuthorizationGrantType()`: The `AuthorizationGrantType` associated with the authorization grant.
  262. <8> `getAuthorizationGrant()`: The `Authentication` instance used by the `AuthenticationProvider` that processes the authorization grant.
  263. [[oauth2-token-generator]]
  264. == OAuth2TokenGenerator
  265. An `OAuth2TokenGenerator` is responsible for generating an `OAuth2Token` from the information contained in the provided xref:core-model-components.adoc#oauth2-token-context[OAuth2TokenContext].
  266. The `OAuth2Token` generated primarily depends on the type of `OAuth2TokenType` specified in the `OAuth2TokenContext`.
  267. For example, when the `value` for `OAuth2TokenType` is:
  268. * `code`, then `OAuth2AuthorizationCode` is generated.
  269. * `access_token`, then `OAuth2AccessToken` is generated.
  270. * `refresh_token`, then `OAuth2RefreshToken` is generated.
  271. * `id_token`, then `OidcIdToken` is generated.
  272. Furthermore, the format of the generated `OAuth2AccessToken` varies, depending on the `TokenSettings.getAccessTokenFormat()` configured for the xref:core-model-components.adoc#registered-client[RegisteredClient].
  273. If the format is `OAuth2TokenFormat.SELF_CONTAINED` (the default), then a `Jwt` is generated.
  274. If the format is `OAuth2TokenFormat.REFERENCE`, then an "opaque" token is generated.
  275. Finally, if the generated `OAuth2Token` has a set of claims and implements `ClaimAccessor`, the claims are made accessible from xref:core-model-components.adoc#oauth2-authorization[OAuth2Authorization.Token.getClaims()].
  276. The `OAuth2TokenGenerator` is primarily used by components that implement authorization grant processing – for example, `authorization_code`, `client_credentials`, and `refresh_token`.
  277. The provided implementations are `OAuth2AccessTokenGenerator`, `OAuth2RefreshTokenGenerator`, and `JwtGenerator`.
  278. The `OAuth2AccessTokenGenerator` generates an "opaque" (`OAuth2TokenFormat.REFERENCE`) access token, and the `JwtGenerator` generates a `Jwt` (`OAuth2TokenFormat.SELF_CONTAINED`).
  279. [NOTE]
  280. The `OAuth2TokenGenerator` is an *OPTIONAL* component and defaults to a `DelegatingOAuth2TokenGenerator` composed of an `OAuth2AccessTokenGenerator` and `OAuth2RefreshTokenGenerator`.
  281. [NOTE]
  282. If a `JwtEncoder` `@Bean` or `JWKSource<SecurityContext>` `@Bean` is registered, then a `JwtGenerator` is additionally composed in the `DelegatingOAuth2TokenGenerator`.
  283. The `OAuth2TokenGenerator` provides great flexibility, as it can support any custom token format for `access_token` and `refresh_token`.
  284. The following example shows how to register an `OAuth2TokenGenerator` `@Bean`:
  285. [source,java]
  286. ----
  287. @Bean
  288. public OAuth2TokenGenerator<?> tokenGenerator() {
  289. JwtEncoder jwtEncoder = ...
  290. JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
  291. OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
  292. OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
  293. return new DelegatingOAuth2TokenGenerator(
  294. jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
  295. }
  296. ----
  297. Alternatively, you can configure the `OAuth2TokenGenerator` through the xref:configuration-model.adoc#customizing-the-configuration[`OAuth2AuthorizationServerConfigurer`]:
  298. [source,java]
  299. ----
  300. @Bean
  301. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  302. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  303. new OAuth2AuthorizationServerConfigurer();
  304. http.apply(authorizationServerConfigurer);
  305. authorizationServerConfigurer
  306. .tokenGenerator(tokenGenerator);
  307. ...
  308. return http.build();
  309. }
  310. ----
  311. [NOTE]
  312. The `OAuth2AuthorizationServerConfigurer` is useful when applying multiple configuration options simultaneously.
  313. [[oauth2-token-customizer]]
  314. == OAuth2TokenCustomizer
  315. An `OAuth2TokenCustomizer` provides the ability to customize the attributes of an `OAuth2Token`, which are accessible in the provided xref:core-model-components.adoc#oauth2-token-context[OAuth2TokenContext].
  316. It is used by an xref:core-model-components.adoc#oauth2-token-generator[OAuth2TokenGenerator] to let it customize the attributes of the `OAuth2Token` before it is generated.
  317. An `OAuth2TokenCustomizer<OAuth2TokenClaimsContext>` declared with a generic type of `OAuth2TokenClaimsContext` (`implements OAuth2TokenContext`) provides the ability to customize the claims of an "opaque" `OAuth2AccessToken`.
  318. `OAuth2TokenClaimsContext.getClaims()` provides access to the `OAuth2TokenClaimsSet.Builder`, allowing the ability to add, replace, and remove claims.
  319. The following example shows how to implement an `OAuth2TokenCustomizer<OAuth2TokenClaimsContext>` and configure it with an `OAuth2AccessTokenGenerator`:
  320. [source,java]
  321. ----
  322. @Bean
  323. public OAuth2TokenGenerator<?> tokenGenerator() {
  324. JwtEncoder jwtEncoder = ...
  325. JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
  326. OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
  327. accessTokenGenerator.setAccessTokenCustomizer(accessTokenCustomizer());
  328. OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
  329. return new DelegatingOAuth2TokenGenerator(
  330. jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
  331. }
  332. @Bean
  333. public OAuth2TokenCustomizer<OAuth2TokenClaimsContext> accessTokenCustomizer() {
  334. return context -> {
  335. OAuth2TokenClaimsSet.Builder claims = context.getClaims();
  336. // Customize claims
  337. };
  338. }
  339. ----
  340. [NOTE]
  341. If the `OAuth2TokenGenerator` is not provided as a `@Bean` or is not configured through the `OAuth2AuthorizationServerConfigurer`, an `OAuth2TokenCustomizer<OAuth2TokenClaimsContext>` `@Bean` will automatically be configured with an `OAuth2AccessTokenGenerator`.
  342. An `OAuth2TokenCustomizer<JwtEncodingContext>` declared with a generic type of `JwtEncodingContext` (`implements OAuth2TokenContext`) provides the ability to customize the headers and claims of a `Jwt`.
  343. `JwtEncodingContext.getJwsHeader()` provides access to the `JwsHeader.Builder`, allowing the ability to add, replace, and remove headers.
  344. `JwtEncodingContext.getClaims()` provides access to the `JwtClaimsSet.Builder`, allowing the ability to add, replace, and remove claims.
  345. The following example shows how to implement an `OAuth2TokenCustomizer<JwtEncodingContext>` and configure it with a `JwtGenerator`:
  346. [source,java]
  347. ----
  348. @Bean
  349. public OAuth2TokenGenerator<?> tokenGenerator() {
  350. JwtEncoder jwtEncoder = ...
  351. JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
  352. jwtGenerator.setJwtCustomizer(jwtCustomizer());
  353. OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
  354. OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
  355. return new DelegatingOAuth2TokenGenerator(
  356. jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
  357. }
  358. @Bean
  359. public OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() {
  360. return context -> {
  361. JwsHeader.Builder headers = context.getJwsHeader();
  362. JwtClaimsSet.Builder claims = context.getClaims();
  363. if (context.getTokenType().equals(OAuth2TokenType.ACCESS_TOKEN)) {
  364. // Customize headers/claims for access_token
  365. } else if (context.getTokenType().getValue().equals(OidcParameterNames.ID_TOKEN)) {
  366. // Customize headers/claims for id_token
  367. }
  368. };
  369. }
  370. ----
  371. [NOTE]
  372. If the `OAuth2TokenGenerator` is not provided as a `@Bean` or is not configured through the `OAuth2AuthorizationServerConfigurer`, an `OAuth2TokenCustomizer<JwtEncodingContext>` `@Bean` will automatically be configured with a `JwtGenerator`.
  373. [TIP]
  374. For an example showing how you can xref:guides/how-to-userinfo.adoc#customize-id-token[customize the ID token], see the guide xref:guides/how-to-userinfo.adoc[How-to: Customize the OpenID Connect 1.0 UserInfo response].
  375. [[session-registry]]
  376. == SessionRegistry
  377. If OpenID Connect 1.0 is enabled, a `SessionRegistry` instance is used to track authenticated sessions.
  378. The `SessionRegistry` is used by the default implementation of `SessionAuthenticationStrategy` associated to the xref:protocol-endpoints.adoc#oauth2-authorization-endpoint[OAuth2 Authorization Endpoint] for registering new authenticated sessions.
  379. [NOTE]
  380. If a `SessionRegistry` `@Bean` is not registered, the default implementation `SessionRegistryImpl` will be used.
  381. [IMPORTANT]
  382. If a `SessionRegistry` `@Bean` is registered and is an instance of `SessionRegistryImpl`, a `HttpSessionEventPublisher` `@Bean` *SHOULD* also be registered as it's responsible for notifying `SessionRegistryImpl` of session lifecycle events, for example, `SessionDestroyedEvent`, to provide the ability to remove the `SessionInformation` instance.
  383. When a logout is requested by an End-User, the xref:protocol-endpoints.adoc#oidc-logout-endpoint[OpenID Connect 1.0 Logout Endpoint] uses the `SessionRegistry` to lookup the `SessionInformation` associated to the authenticated End-User to perform the logout.
  384. If Spring Security's {spring-security-reference-base-url}/servlet/authentication/session-management.html#ns-concurrent-sessions[Concurrent Session Control] feature is being used, it is *RECOMMENDED* to register a `SessionRegistry` `@Bean` to ensure it's shared between Spring Security's Concurrent Session Control and Spring Authorization Server's Logout feature.
  385. The following example shows how to register a `SessionRegistry` `@Bean` and `HttpSessionEventPublisher` `@Bean` (required by `SessionRegistryImpl`):
  386. [source,java]
  387. ----
  388. @Bean
  389. public SessionRegistry sessionRegistry() {
  390. return new SessionRegistryImpl();
  391. }
  392. @Bean
  393. public HttpSessionEventPublisher httpSessionEventPublisher() {
  394. return new HttpSessionEventPublisher();
  395. }
  396. ----