protocol-endpoints.adoc 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. [[protocol-endpoints]]
  2. = Protocol Endpoints
  3. [[oauth2-authorization-endpoint]]
  4. == OAuth2 Authorization Endpoint
  5. `OAuth2AuthorizationEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc6749#section-3.1[OAuth2 Authorization endpoint].
  6. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.1[OAuth2 authorization requests].
  7. `OAuth2AuthorizationEndpointConfigurer` provides the following configuration options:
  8. [source,java]
  9. ----
  10. @Bean
  11. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  12. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  13. new OAuth2AuthorizationServerConfigurer();
  14. http.apply(authorizationServerConfigurer);
  15. authorizationServerConfigurer
  16. .authorizationEndpoint(authorizationEndpoint ->
  17. authorizationEndpoint
  18. .authorizationRequestConverter(authorizationRequestConverter) <1>
  19. .authorizationRequestConverters(authorizationRequestConvertersConsumer) <2>
  20. .authenticationProvider(authenticationProvider) <3>
  21. .authenticationProviders(authenticationProvidersConsumer) <4>
  22. .authorizationResponseHandler(authorizationResponseHandler) <5>
  23. .errorResponseHandler(errorResponseHandler) <6>
  24. .consentPage("/oauth2/v1/authorize") <7>
  25. );
  26. return http.build();
  27. }
  28. ----
  29. <1> `authorizationRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.1[OAuth2 authorization request] (or consent) from `HttpServletRequest` to an instance of `OAuth2AuthorizationCodeRequestAuthenticationToken` or `OAuth2AuthorizationConsentAuthenticationToken`.
  30. <2> `authorizationRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  31. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OAuth2AuthorizationCodeRequestAuthenticationToken` or `OAuth2AuthorizationConsentAuthenticationToken`.
  32. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  33. <5> `authorizationResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OAuth2AuthorizationCodeRequestAuthenticationToken` and returning the https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2[OAuth2AuthorizationResponse].
  34. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthorizationCodeRequestAuthenticationException` and returning the https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1[OAuth2Error response].
  35. <7> `consentPage()`: The `URI` of the custom consent page to redirect resource owners to if consent is required during the authorization request flow.
  36. `OAuth2AuthorizationEndpointConfigurer` configures the `OAuth2AuthorizationEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  37. `OAuth2AuthorizationEndpointFilter` is the `Filter` that processes OAuth2 authorization requests (and consents).
  38. `OAuth2AuthorizationEndpointFilter` is configured with the following defaults:
  39. * `*AuthenticationConverter*` -- A `DelegatingAuthenticationConverter` composed of `OAuth2AuthorizationCodeRequestAuthenticationConverter` and `OAuth2AuthorizationConsentAuthenticationConverter`.
  40. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OAuth2AuthorizationCodeRequestAuthenticationProvider` and `OAuth2AuthorizationConsentAuthenticationProvider`.
  41. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OAuth2AuthorizationCodeRequestAuthenticationToken` and returns the `OAuth2AuthorizationResponse`.
  42. * `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthorizationCodeRequestAuthenticationException` and returns the `OAuth2Error` response.
  43. [[oauth2-authorization-endpoint-customizing-authorization-request-validation]]
  44. === Customizing Authorization Request Validation
  45. `OAuth2AuthorizationCodeRequestAuthenticationValidator` is the default validator used for validating specific OAuth2 authorization request parameters used in the Authorization Code Grant.
  46. The default implementation validates the `redirect_uri` and `scope` parameters.
  47. If validation fails, an `OAuth2AuthorizationCodeRequestAuthenticationException` is thrown.
  48. `OAuth2AuthorizationCodeRequestAuthenticationProvider` provides the ability to override the default authorization request validation by supplying a custom authentication validator of type `Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext>` to `setAuthenticationValidator()`.
  49. [TIP]
  50. `OAuth2AuthorizationCodeRequestAuthenticationContext` holds the `OAuth2AuthorizationCodeRequestAuthenticationToken`, which contains the OAuth2 authorization request parameters.
  51. [IMPORTANT]
  52. If validation fails, the authentication validator *MUST* throw `OAuth2AuthorizationCodeRequestAuthenticationException`.
  53. A common use case during the development life cycle phase is to allow for `localhost` in the `redirect_uri` parameter.
  54. The following example shows how to configure `OAuth2AuthorizationCodeRequestAuthenticationProvider` with a custom authentication validator that allows for `localhost` in the `redirect_uri` parameter:
  55. [source,java]
  56. ----
  57. @Bean
  58. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  59. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  60. new OAuth2AuthorizationServerConfigurer();
  61. http.apply(authorizationServerConfigurer);
  62. authorizationServerConfigurer
  63. .authorizationEndpoint(authorizationEndpoint ->
  64. authorizationEndpoint
  65. .authenticationProviders(configureAuthenticationValidator())
  66. );
  67. return http.build();
  68. }
  69. private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
  70. return (authenticationProviders) ->
  71. authenticationProviders.forEach((authenticationProvider) -> {
  72. if (authenticationProvider instanceof OAuth2AuthorizationCodeRequestAuthenticationProvider) {
  73. Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator =
  74. // Override default redirect_uri validator
  75. new CustomRedirectUriValidator()
  76. // Reuse default scope validator
  77. .andThen(OAuth2AuthorizationCodeRequestAuthenticationValidator.DEFAULT_SCOPE_VALIDATOR);
  78. ((OAuth2AuthorizationCodeRequestAuthenticationProvider) authenticationProvider)
  79. .setAuthenticationValidator(authenticationValidator);
  80. }
  81. });
  82. }
  83. static class CustomRedirectUriValidator implements Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> {
  84. @Override
  85. public void accept(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
  86. OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
  87. authenticationContext.getAuthentication();
  88. RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
  89. String requestedRedirectUri = authorizationCodeRequestAuthentication.getRedirectUri();
  90. // Use exact string matching when comparing client redirect URIs against pre-registered URIs
  91. if (!registeredClient.getRedirectUris().contains(requestedRedirectUri)) {
  92. OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST);
  93. throw new OAuth2AuthorizationCodeRequestAuthenticationException(error, null);
  94. }
  95. }
  96. }
  97. ----
  98. [[oauth2-device-authorization-endpoint]]
  99. == OAuth2 Device Authorization Endpoint
  100. `OAuth2DeviceAuthorizationEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc8628#section-3.1[OAuth2 Device Authorization endpoint].
  101. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for OAuth2 device authorization requests.
  102. `OAuth2DeviceAuthorizationEndpointConfigurer` provides the following configuration options:
  103. [source,java]
  104. ----
  105. @Bean
  106. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  107. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  108. new OAuth2AuthorizationServerConfigurer();
  109. http.apply(authorizationServerConfigurer);
  110. authorizationServerConfigurer
  111. .deviceAuthorizationEndpoint(deviceAuthorizationEndpoint ->
  112. deviceAuthorizationEndpoint
  113. .deviceAuthorizationRequestConverter(deviceAuthorizationRequestConverter) <1>
  114. .deviceAuthorizationRequestConverters(deviceAuthorizationRequestConvertersConsumer) <2>
  115. .authenticationProvider(authenticationProvider) <3>
  116. .authenticationProviders(authenticationProvidersConsumer) <4>
  117. .deviceAuthorizationResponseHandler(deviceAuthorizationResponseHandler) <5>
  118. .errorResponseHandler(errorResponseHandler) <6>
  119. .verificationUri("/oauth2/v1/device_verification") <7>
  120. );
  121. return http.build();
  122. }
  123. ----
  124. <1> `deviceAuthorizationRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://datatracker.ietf.org/doc/html/rfc8628#section-3.1[OAuth2 device authorization request] from `HttpServletRequest` to an instance of `OAuth2DeviceAuthorizationRequestAuthenticationToken`.
  125. <2> `deviceAuthorizationRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  126. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OAuth2DeviceAuthorizationRequestAuthenticationToken`.
  127. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  128. <5> `deviceAuthorizationResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OAuth2DeviceAuthorizationRequestAuthenticationToken` and returning the https://datatracker.ietf.org/doc/html/rfc8628#section-3.2[OAuth2DeviceAuthorizationResponse].
  129. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the https://datatracker.ietf.org/doc/html/rfc6749#section-5.2[OAuth2Error response].
  130. <7> `verificationUri()`: The `URI` of the custom end-user verification page to direct resource owners to on a secondary device.
  131. `OAuth2DeviceAuthorizationEndpointConfigurer` configures the `OAuth2DeviceAuthorizationEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  132. `OAuth2DeviceAuthorizationEndpointFilter` is the `Filter` that processes OAuth2 device authorization requests.
  133. `OAuth2DeviceAuthorizationEndpointFilter` is configured with the following defaults:
  134. * `*AuthenticationConverter*` -- An `OAuth2DeviceAuthorizationRequestAuthenticationConverter`.
  135. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OAuth2DeviceAuthorizationRequestAuthenticationProvider`.
  136. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OAuth2DeviceAuthorizationRequestAuthenticationToken` and returns the `OAuth2DeviceAuthorizationResponse`.
  137. * `*AuthenticationFailureHandler*` -- An `OAuth2ErrorAuthenticationFailureHandler`.
  138. [[oauth2-device-verification-endpoint]]
  139. == OAuth2 Device Verification Endpoint
  140. `OAuth2DeviceVerificationEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc8628#section-3.3[OAuth2 Device Verification endpoint] (or "User Interaction").
  141. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for OAuth2 device verification requests.
  142. `OAuth2DeviceVerificationEndpointConfigurer` provides the following configuration options:
  143. [source,java]
  144. ----
  145. @Bean
  146. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  147. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  148. new OAuth2AuthorizationServerConfigurer();
  149. http.apply(authorizationServerConfigurer);
  150. authorizationServerConfigurer
  151. .deviceVerificationEndpoint(deviceVerificationEndpoint ->
  152. deviceVerificationEndpoint
  153. .deviceVerificationRequestConverter(deviceVerificationRequestConverter) <1>
  154. .deviceVerificationRequestConverters(deviceVerificationRequestConvertersConsumer) <2>
  155. .authenticationProvider(authenticationProvider) <3>
  156. .authenticationProviders(authenticationProvidersConsumer) <4>
  157. .deviceVerificationResponseHandler(deviceVerificationResponseHandler) <5>
  158. .errorResponseHandler(errorResponseHandler) <6>
  159. .consentPage("/oauth2/v1/consent") <7>
  160. );
  161. return http.build();
  162. }
  163. ----
  164. <1> `deviceVerificationRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://datatracker.ietf.org/doc/html/rfc8628#section-3.3[OAuth2 device verification request] (or consent) from `HttpServletRequest` to an instance of `OAuth2DeviceVerificationAuthenticationToken` or `OAuth2DeviceAuthorizationConsentAuthenticationToken`.
  165. <2> `deviceVerificationRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  166. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OAuth2DeviceVerificationAuthenticationToken` or `OAuth2DeviceAuthorizationConsentAuthenticationToken`.
  167. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  168. <5> `deviceVerificationResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OAuth2DeviceVerificationAuthenticationToken` and directing the resource owner to return to their device.
  169. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the error response.
  170. <7> `consentPage()`: The `URI` of the custom consent page to redirect resource owners to if consent is required during the device verification request flow.
  171. `OAuth2DeviceVerificationEndpointConfigurer` configures the `OAuth2DeviceVerificationEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  172. `OAuth2DeviceVerificationEndpointFilter` is the `Filter` that processes OAuth2 device verification requests (and consents).
  173. `OAuth2DeviceVerificationEndpointFilter` is configured with the following defaults:
  174. * `*AuthenticationConverter*` -- A `DelegatingAuthenticationConverter` composed of `OAuth2DeviceVerificationAuthenticationConverter` and `OAuth2DeviceAuthorizationConsentAuthenticationConverter`.
  175. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OAuth2DeviceVerificationAuthenticationProvider` and `OAuth2DeviceAuthorizationConsentAuthenticationProvider`.
  176. * `*AuthenticationSuccessHandler*` -- A `SimpleUrlAuthenticationSuccessHandler` that handles an "`authenticated`" `OAuth2DeviceVerificationAuthenticationToken` and redirects the user to a success page (`/?success`).
  177. * `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthenticationException` and returns the `OAuth2Error` response.
  178. [[oauth2-token-endpoint]]
  179. == OAuth2 Token Endpoint
  180. `OAuth2TokenEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc6749#section-3.2[OAuth2 Token endpoint].
  181. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3[OAuth2 access token requests].
  182. `OAuth2TokenEndpointConfigurer` provides the following configuration options:
  183. [source,java]
  184. ----
  185. @Bean
  186. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  187. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  188. new OAuth2AuthorizationServerConfigurer();
  189. http.apply(authorizationServerConfigurer);
  190. authorizationServerConfigurer
  191. .tokenEndpoint(tokenEndpoint ->
  192. tokenEndpoint
  193. .accessTokenRequestConverter(accessTokenRequestConverter) <1>
  194. .accessTokenRequestConverters(accessTokenRequestConvertersConsumer) <2>
  195. .authenticationProvider(authenticationProvider) <3>
  196. .authenticationProviders(authenticationProvidersConsumer) <4>
  197. .accessTokenResponseHandler(accessTokenResponseHandler) <5>
  198. .errorResponseHandler(errorResponseHandler) <6>
  199. );
  200. return http.build();
  201. }
  202. ----
  203. <1> `accessTokenRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3[OAuth2 access token request] from `HttpServletRequest` to an instance of `OAuth2AuthorizationGrantAuthenticationToken`.
  204. <2> `accessTokenRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  205. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OAuth2AuthorizationGrantAuthenticationToken`.
  206. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  207. <5> `accessTokenResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an `OAuth2AccessTokenAuthenticationToken` and returning the https://datatracker.ietf.org/doc/html/rfc6749#section-5.1[`OAuth2AccessTokenResponse`].
  208. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the https://datatracker.ietf.org/doc/html/rfc6749#section-5.2[OAuth2Error response].
  209. `OAuth2TokenEndpointConfigurer` configures the `OAuth2TokenEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  210. `OAuth2TokenEndpointFilter` is the `Filter` that processes OAuth2 access token requests.
  211. The supported https://datatracker.ietf.org/doc/html/rfc6749#section-1.3[authorization grant types] are `authorization_code`, `refresh_token`, `client_credentials`, and `urn:ietf:params:oauth:grant-type:device_code`.
  212. `OAuth2TokenEndpointFilter` is configured with the following defaults:
  213. * `*AuthenticationConverter*` -- A `DelegatingAuthenticationConverter` composed of `OAuth2AuthorizationCodeAuthenticationConverter`, `OAuth2RefreshTokenAuthenticationConverter`, `OAuth2ClientCredentialsAuthenticationConverter`, and `OAuth2DeviceCodeAuthenticationConverter`.
  214. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OAuth2AuthorizationCodeAuthenticationProvider`, `OAuth2RefreshTokenAuthenticationProvider`, `OAuth2ClientCredentialsAuthenticationProvider`, and `OAuth2DeviceCodeAuthenticationProvider`.
  215. * `*AuthenticationSuccessHandler*` -- An `OAuth2AccessTokenResponseAuthenticationSuccessHandler`.
  216. * `*AuthenticationFailureHandler*` -- An `OAuth2ErrorAuthenticationFailureHandler`.
  217. [[oauth2-token-endpoint-customizing-client-credentials-grant-request-validation]]
  218. === Customizing Client Credentials Grant Request Validation
  219. `OAuth2ClientCredentialsAuthenticationValidator` is the default validator used for validating specific OAuth2 Client Credentials Grant request parameters.
  220. The default implementation validates the `scope` parameter.
  221. If validation fails, an `OAuth2AuthenticationException` is thrown.
  222. `OAuth2ClientCredentialsAuthenticationProvider` provides the ability to override the default request validation by supplying a custom authentication validator of type `Consumer<OAuth2ClientCredentialsAuthenticationContext>` to `setAuthenticationValidator()`.
  223. [TIP]
  224. `OAuth2ClientCredentialsAuthenticationContext` holds the `OAuth2ClientCredentialsAuthenticationToken`, which contains the OAuth2 Client Credentials Grant request parameters.
  225. [IMPORTANT]
  226. If validation fails, the authentication validator *MUST* throw `OAuth2AuthenticationException`.
  227. The following example shows how to configure `OAuth2ClientCredentialsAuthenticationProvider` with a custom authentication validator that overrides the default `scope` validation:
  228. [source,java]
  229. ----
  230. @Bean
  231. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  232. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  233. new OAuth2AuthorizationServerConfigurer();
  234. http.apply(authorizationServerConfigurer);
  235. authorizationServerConfigurer
  236. .tokenEndpoint(tokenEndpoint ->
  237. tokenEndpoint
  238. .authenticationProviders(configureAuthenticationValidator())
  239. );
  240. return http.build();
  241. }
  242. private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
  243. return (authenticationProviders) ->
  244. authenticationProviders.forEach((authenticationProvider) -> {
  245. if (authenticationProvider instanceof OAuth2ClientCredentialsAuthenticationProvider) {
  246. Consumer<OAuth2ClientCredentialsAuthenticationContext> authenticationValidator =
  247. new CustomScopeValidator();
  248. // Override default scope validation
  249. ((OAuth2ClientCredentialsAuthenticationProvider) authenticationProvider)
  250. .setAuthenticationValidator(authenticationValidator);
  251. }
  252. });
  253. }
  254. static class CustomScopeValidator implements Consumer<OAuth2ClientCredentialsAuthenticationContext> {
  255. @Override
  256. public void accept(OAuth2ClientCredentialsAuthenticationContext authenticationContext) {
  257. OAuth2ClientCredentialsAuthenticationToken clientCredentialsAuthentication =
  258. authenticationContext.getAuthentication();
  259. Set<String> requestedScopes = clientCredentialsAuthentication.getScopes();
  260. RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
  261. Set<String> allowedScopes = registeredClient.getScopes();
  262. // TODO Implement scope validation
  263. }
  264. }
  265. ----
  266. [[oauth2-token-introspection-endpoint]]
  267. == OAuth2 Token Introspection Endpoint
  268. `OAuth2TokenIntrospectionEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc7662#section-2[OAuth2 Token Introspection endpoint].
  269. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for https://datatracker.ietf.org/doc/html/rfc7662#section-2.1[OAuth2 introspection requests].
  270. `OAuth2TokenIntrospectionEndpointConfigurer` provides the following configuration options:
  271. [source,java]
  272. ----
  273. @Bean
  274. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  275. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  276. new OAuth2AuthorizationServerConfigurer();
  277. http.apply(authorizationServerConfigurer);
  278. authorizationServerConfigurer
  279. .tokenIntrospectionEndpoint(tokenIntrospectionEndpoint ->
  280. tokenIntrospectionEndpoint
  281. .introspectionRequestConverter(introspectionRequestConverter) <1>
  282. .introspectionRequestConverters(introspectionRequestConvertersConsumer) <2>
  283. .authenticationProvider(authenticationProvider) <3>
  284. .authenticationProviders(authenticationProvidersConsumer) <4>
  285. .introspectionResponseHandler(introspectionResponseHandler) <5>
  286. .errorResponseHandler(errorResponseHandler) <6>
  287. );
  288. return http.build();
  289. }
  290. ----
  291. <1> `introspectionRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://datatracker.ietf.org/doc/html/rfc7662#section-2.1[OAuth2 introspection request] from `HttpServletRequest` to an instance of `OAuth2TokenIntrospectionAuthenticationToken`.
  292. <2> `introspectionRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  293. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OAuth2TokenIntrospectionAuthenticationToken`.
  294. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  295. <5> `introspectionResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OAuth2TokenIntrospectionAuthenticationToken` and returning the https://datatracker.ietf.org/doc/html/rfc7662#section-2.2[OAuth2TokenIntrospection response].
  296. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the https://datatracker.ietf.org/doc/html/rfc7662#section-2.3[OAuth2Error response].
  297. `OAuth2TokenIntrospectionEndpointConfigurer` configures the `OAuth2TokenIntrospectionEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  298. `OAuth2TokenIntrospectionEndpointFilter` is the `Filter` that processes OAuth2 introspection requests.
  299. `OAuth2TokenIntrospectionEndpointFilter` is configured with the following defaults:
  300. * `*AuthenticationConverter*` -- An `OAuth2TokenIntrospectionAuthenticationConverter`.
  301. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OAuth2TokenIntrospectionAuthenticationProvider`.
  302. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OAuth2TokenIntrospectionAuthenticationToken` and returns the `OAuth2TokenIntrospection` response.
  303. * `*AuthenticationFailureHandler*` -- An `OAuth2ErrorAuthenticationFailureHandler`.
  304. [[oauth2-token-revocation-endpoint]]
  305. == OAuth2 Token Revocation Endpoint
  306. `OAuth2TokenRevocationEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc7009#section-2[OAuth2 Token Revocation endpoint].
  307. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for https://datatracker.ietf.org/doc/html/rfc7009#section-2.1[OAuth2 revocation requests].
  308. `OAuth2TokenRevocationEndpointConfigurer` provides the following configuration options:
  309. [source,java]
  310. ----
  311. @Bean
  312. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  313. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  314. new OAuth2AuthorizationServerConfigurer();
  315. http.apply(authorizationServerConfigurer);
  316. authorizationServerConfigurer
  317. .tokenRevocationEndpoint(tokenRevocationEndpoint ->
  318. tokenRevocationEndpoint
  319. .revocationRequestConverter(revocationRequestConverter) <1>
  320. .revocationRequestConverters(revocationRequestConvertersConsumer) <2>
  321. .authenticationProvider(authenticationProvider) <3>
  322. .authenticationProviders(authenticationProvidersConsumer) <4>
  323. .revocationResponseHandler(revocationResponseHandler) <5>
  324. .errorResponseHandler(errorResponseHandler) <6>
  325. );
  326. return http.build();
  327. }
  328. ----
  329. <1> `revocationRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://datatracker.ietf.org/doc/html/rfc7009#section-2.1[OAuth2 revocation request] from `HttpServletRequest` to an instance of `OAuth2TokenRevocationAuthenticationToken`.
  330. <2> `revocationRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  331. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OAuth2TokenRevocationAuthenticationToken`.
  332. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  333. <5> `revocationResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OAuth2TokenRevocationAuthenticationToken` and returning the https://datatracker.ietf.org/doc/html/rfc7009#section-2.2[OAuth2 revocation response].
  334. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the https://datatracker.ietf.org/doc/html/rfc7009#section-2.2.1[OAuth2Error response].
  335. `OAuth2TokenRevocationEndpointConfigurer` configures the `OAuth2TokenRevocationEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  336. `OAuth2TokenRevocationEndpointFilter` is the `Filter` that processes OAuth2 revocation requests.
  337. `OAuth2TokenRevocationEndpointFilter` is configured with the following defaults:
  338. * `*AuthenticationConverter*` -- An `OAuth2TokenRevocationAuthenticationConverter`.
  339. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OAuth2TokenRevocationAuthenticationProvider`.
  340. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OAuth2TokenRevocationAuthenticationToken` and returns the OAuth2 revocation response.
  341. * `*AuthenticationFailureHandler*` -- An `OAuth2ErrorAuthenticationFailureHandler`.
  342. [[oauth2-authorization-server-metadata-endpoint]]
  343. == OAuth2 Authorization Server Metadata Endpoint
  344. `OAuth2AuthorizationServerMetadataEndpointConfigurer` provides the ability to customize the https://datatracker.ietf.org/doc/html/rfc8414#section-3[OAuth2 Authorization Server Metadata endpoint].
  345. It defines an extension point that lets you customize the https://datatracker.ietf.org/doc/html/rfc8414#section-3.2[OAuth2 Authorization Server Metadata response].
  346. `OAuth2AuthorizationServerMetadataEndpointConfigurer` provides the following configuration option:
  347. [source,java]
  348. ----
  349. @Bean
  350. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  351. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  352. new OAuth2AuthorizationServerConfigurer();
  353. http.apply(authorizationServerConfigurer);
  354. authorizationServerConfigurer
  355. .authorizationServerMetadataEndpoint(authorizationServerMetadataEndpoint ->
  356. authorizationServerMetadataEndpoint
  357. .authorizationServerMetadataCustomizer(authorizationServerMetadataCustomizer)); <1>
  358. return http.build();
  359. }
  360. ----
  361. <1> `authorizationServerMetadataCustomizer()`: The `Consumer` providing access to the `OAuth2AuthorizationServerMetadata.Builder` allowing the ability to customize the claims of the Authorization Server's configuration.
  362. `OAuth2AuthorizationServerMetadataEndpointConfigurer` configures the `OAuth2AuthorizationServerMetadataEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  363. `OAuth2AuthorizationServerMetadataEndpointFilter` is the `Filter` that returns the https://datatracker.ietf.org/doc/html/rfc8414#section-3.2[OAuth2AuthorizationServerMetadata response].
  364. [[jwk-set-endpoint]]
  365. == JWK Set Endpoint
  366. `OAuth2AuthorizationServerConfigurer` provides support for the https://datatracker.ietf.org/doc/html/rfc7517[JWK Set endpoint].
  367. `OAuth2AuthorizationServerConfigurer` configures the `NimbusJwkSetEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  368. `NimbusJwkSetEndpointFilter` is the `Filter` that returns the https://datatracker.ietf.org/doc/html/rfc7517#section-5[JWK Set].
  369. [NOTE]
  370. The JWK Set endpoint is configured *only* if a `JWKSource<SecurityContext>` `@Bean` is registered.
  371. [[oidc-provider-configuration-endpoint]]
  372. == OpenID Connect 1.0 Provider Configuration Endpoint
  373. `OidcProviderConfigurationEndpointConfigurer` provides the ability to customize the https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig[OpenID Connect 1.0 Provider Configuration endpoint].
  374. It defines an extension point that lets you customize the https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse[OpenID Provider Configuration response].
  375. `OidcProviderConfigurationEndpointConfigurer` provides the following configuration option:
  376. [source,java]
  377. ----
  378. @Bean
  379. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  380. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  381. new OAuth2AuthorizationServerConfigurer();
  382. http.apply(authorizationServerConfigurer);
  383. authorizationServerConfigurer
  384. .oidc(oidc ->
  385. oidc
  386. .providerConfigurationEndpoint(providerConfigurationEndpoint ->
  387. providerConfigurationEndpoint
  388. .providerConfigurationCustomizer(providerConfigurationCustomizer) <1>
  389. )
  390. );
  391. return http.build();
  392. }
  393. ----
  394. <1> `providerConfigurationCustomizer()`: The `Consumer` providing access to the `OidcProviderConfiguration.Builder` allowing the ability to customize the claims of the OpenID Provider's configuration.
  395. `OidcProviderConfigurationEndpointConfigurer` configures the `OidcProviderConfigurationEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  396. `OidcProviderConfigurationEndpointFilter` is the `Filter` that returns the https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse[OidcProviderConfiguration response].
  397. [[oidc-logout-endpoint]]
  398. == OpenID Connect 1.0 Logout Endpoint
  399. `OidcLogoutEndpointConfigurer` provides the ability to customize the https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RPLogout[OpenID Connect 1.0 Logout endpoint].
  400. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for RP-Initiated Logout requests.
  401. `OidcLogoutEndpointConfigurer` provides the following configuration options:
  402. [source,java]
  403. ----
  404. @Bean
  405. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  406. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  407. new OAuth2AuthorizationServerConfigurer();
  408. http.apply(authorizationServerConfigurer);
  409. authorizationServerConfigurer
  410. .oidc(oidc ->
  411. oidc
  412. .logoutEndpoint(logoutEndpoint ->
  413. logoutEndpoint
  414. .logoutRequestConverter(logoutRequestConverter) <1>
  415. .logoutRequestConverters(logoutRequestConvertersConsumer) <2>
  416. .authenticationProvider(authenticationProvider) <3>
  417. .authenticationProviders(authenticationProvidersConsumer) <4>
  418. .logoutResponseHandler(logoutResponseHandler) <5>
  419. .errorResponseHandler(errorResponseHandler) <6>
  420. )
  421. );
  422. return http.build();
  423. }
  424. ----
  425. <1> `logoutRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract a https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RPLogout[Logout request] from `HttpServletRequest` to an instance of `OidcLogoutAuthenticationToken`.
  426. <2> `logoutRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  427. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OidcLogoutAuthenticationToken`.
  428. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  429. <5> `logoutResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OidcLogoutAuthenticationToken` and performing the logout.
  430. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the error response.
  431. `OidcLogoutEndpointConfigurer` configures the `OidcLogoutEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  432. `OidcLogoutEndpointFilter` is the `Filter` that processes https://openid.net/specs/openid-connect-rpinitiated-1_0.html#RPLogout[RP-Initiated Logout requests] and performs the logout of the End-User.
  433. `OidcLogoutEndpointFilter` is configured with the following defaults:
  434. * `*AuthenticationConverter*` -- An `OidcLogoutAuthenticationConverter`.
  435. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OidcLogoutAuthenticationProvider`.
  436. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OidcLogoutAuthenticationToken` and performs the logout.
  437. * `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthenticationException` and returns the `OAuth2Error` response.
  438. [NOTE]
  439. `OidcLogoutAuthenticationProvider` uses a xref:core-model-components.adoc#session-registry[`SessionRegistry`] to look up the `SessionInformation` instance associated to the End-User requesting to be logged out.
  440. [TIP]
  441. `OidcClientInitiatedLogoutSuccessHandler` is the corresponding configuration in Spring Security’s OAuth2 Client support for configuring {spring-security-reference-base-url}/servlet/oauth2/login/advanced.html#oauth2login-advanced-oidc-logout[OpenID Connect 1.0 RP-Initiated Logout].
  442. [[oidc-user-info-endpoint]]
  443. == OpenID Connect 1.0 UserInfo Endpoint
  444. `OidcUserInfoEndpointConfigurer` provides the ability to customize the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[OpenID Connect 1.0 UserInfo endpoint].
  445. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for https://openid.net/specs/openid-connect-core-1_0.html#UserInfoRequest[UserInfo requests].
  446. `OidcUserInfoEndpointConfigurer` provides the following configuration options:
  447. [source,java]
  448. ----
  449. @Bean
  450. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  451. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  452. new OAuth2AuthorizationServerConfigurer();
  453. http.apply(authorizationServerConfigurer);
  454. authorizationServerConfigurer
  455. .oidc(oidc ->
  456. oidc
  457. .userInfoEndpoint(userInfoEndpoint ->
  458. userInfoEndpoint
  459. .userInfoRequestConverter(userInfoRequestConverter) <1>
  460. .userInfoRequestConverters(userInfoRequestConvertersConsumer) <2>
  461. .authenticationProvider(authenticationProvider) <3>
  462. .authenticationProviders(authenticationProvidersConsumer) <4>
  463. .userInfoResponseHandler(userInfoResponseHandler) <5>
  464. .errorResponseHandler(errorResponseHandler) <6>
  465. .userInfoMapper(userInfoMapper) <7>
  466. )
  467. );
  468. return http.build();
  469. }
  470. ----
  471. <1> `userInfoRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract an https://openid.net/specs/openid-connect-core-1_0.html#UserInfoRequest[UserInfo request] from `HttpServletRequest` to an instance of `OidcUserInfoAuthenticationToken`.
  472. <2> `userInfoRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  473. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OidcUserInfoAuthenticationToken`.
  474. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  475. <5> `userInfoResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OidcUserInfoAuthenticationToken` and returning the https://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse[UserInfo response].
  476. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the https://openid.net/specs/openid-connect-core-1_0.html#UserInfoError[UserInfo Error response].
  477. <7> `userInfoMapper()`: The `Function` used to extract claims from `OidcUserInfoAuthenticationContext` to an instance of `OidcUserInfo`.
  478. `OidcUserInfoEndpointConfigurer` configures the `OidcUserInfoEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  479. `OidcUserInfoEndpointFilter` is the `Filter` that processes https://openid.net/specs/openid-connect-core-1_0.html#UserInfoRequest[UserInfo requests] and returns the https://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse[OidcUserInfo response].
  480. `OidcUserInfoEndpointFilter` is configured with the following defaults:
  481. * `*AuthenticationConverter*` -- An internal implementation that obtains the `Authentication` from the `SecurityContext` and creates an `OidcUserInfoAuthenticationToken` with the principal.
  482. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OidcUserInfoAuthenticationProvider`, which is associated with an internal implementation of `userInfoMapper` that extracts https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims[standard claims] from the https://openid.net/specs/openid-connect-core-1_0.html#IDToken[ID Token] based on the https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims[scopes requested] during authorization.
  483. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OidcUserInfoAuthenticationToken` and returns the `OidcUserInfo` response.
  484. * `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthenticationException` and returns the `OAuth2Error` response.
  485. [TIP]
  486. You can customize the ID Token by providing an xref:core-model-components.adoc#oauth2-token-customizer[`OAuth2TokenCustomizer<JwtEncodingContext>`] `@Bean`.
  487. The OpenID Connect 1.0 UserInfo endpoint is an OAuth2 protected resource, which *REQUIRES* an access token to be sent as a bearer token in the https://openid.net/specs/openid-connect-core-1_0.html#UserInfoRequest[UserInfo request].
  488. The following example shows how to enable the OAuth2 resource server configuration:
  489. [source,java]
  490. ----
  491. @Bean
  492. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  493. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  494. new OAuth2AuthorizationServerConfigurer();
  495. http.apply(authorizationServerConfigurer);
  496. ...
  497. http.oauth2ResourceServer(resourceServer -> resourceServer.jwt(Customizer.withDefaults()));
  498. return http.build();
  499. }
  500. @Bean
  501. public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
  502. return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
  503. }
  504. ----
  505. [NOTE]
  506. A `JwtDecoder` `@Bean` is *REQUIRED* for the OpenID Connect 1.0 UserInfo endpoint.
  507. [TIP]
  508. The guide xref:guides/how-to-userinfo.adoc[How-to: Customize the OpenID Connect 1.0 UserInfo response] contains examples of customizing the UserInfo endpoint.
  509. [[oidc-client-registration-endpoint]]
  510. == OpenID Connect 1.0 Client Registration Endpoint
  511. `OidcClientRegistrationEndpointConfigurer` provides the ability to customize the https://openid.net/specs/openid-connect-registration-1_0.html#ClientRegistration[OpenID Connect 1.0 Client Registration endpoint].
  512. It defines extension points that let you customize the pre-processing, main processing, and post-processing logic for https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration requests] or https://openid.net/specs/openid-connect-registration-1_0.html#ReadRequest[Client Read requests].
  513. `OidcClientRegistrationEndpointConfigurer` provides the following configuration options:
  514. [source,java]
  515. ----
  516. @Bean
  517. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  518. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  519. new OAuth2AuthorizationServerConfigurer();
  520. http.apply(authorizationServerConfigurer);
  521. authorizationServerConfigurer
  522. .oidc(oidc ->
  523. oidc
  524. .clientRegistrationEndpoint(clientRegistrationEndpoint ->
  525. clientRegistrationEndpoint
  526. .clientRegistrationRequestConverter(clientRegistrationRequestConverter) <1>
  527. .clientRegistrationRequestConverters(clientRegistrationRequestConvertersConsumers) <2>
  528. .authenticationProvider(authenticationProvider) <3>
  529. .authenticationProviders(authenticationProvidersConsumer) <4>
  530. .clientRegistrationResponseHandler(clientRegistrationResponseHandler) <5>
  531. .errorResponseHandler(errorResponseHandler) <6>
  532. )
  533. );
  534. return http.build();
  535. }
  536. ----
  537. <1> `clientRegistrationRequestConverter()`: Adds an `AuthenticationConverter` (_pre-processor_) used when attempting to extract a https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration request] or https://openid.net/specs/openid-connect-registration-1_0.html#ReadRequest[Client Read request] from `HttpServletRequest` to an instance of `OidcClientRegistrationAuthenticationToken`.
  538. <2> `clientRegistrationRequestConverters()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationConverter``'s allowing the ability to add, remove, or customize a specific `AuthenticationConverter`.
  539. <3> `authenticationProvider()`: Adds an `AuthenticationProvider` (_main processor_) used for authenticating the `OidcClientRegistrationAuthenticationToken`.
  540. <4> `authenticationProviders()`: Sets the `Consumer` providing access to the `List` of default and (optionally) added ``AuthenticationProvider``'s allowing the ability to add, remove, or customize a specific `AuthenticationProvider`.
  541. <5> `clientRegistrationResponseHandler()`: The `AuthenticationSuccessHandler` (_post-processor_) used for handling an "`authenticated`" `OidcClientRegistrationAuthenticationToken` and returning the https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationResponse[Client Registration response] or https://openid.net/specs/openid-connect-registration-1_0.html#ReadResponse[Client Read response].
  542. <6> `errorResponseHandler()`: The `AuthenticationFailureHandler` (_post-processor_) used for handling an `OAuth2AuthenticationException` and returning the https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationError[Client Registration Error response] or https://openid.net/specs/openid-connect-registration-1_0.html#ReadError[Client Read Error response].
  543. [NOTE]
  544. The OpenID Connect 1.0 Client Registration endpoint is disabled by default because many deployments do not require dynamic client registration.
  545. `OidcClientRegistrationEndpointConfigurer` configures the `OidcClientRegistrationEndpointFilter` and registers it with the OAuth2 authorization server `SecurityFilterChain` `@Bean`.
  546. `OidcClientRegistrationEndpointFilter` is the `Filter` that processes https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationRequest[Client Registration requests] and returns the https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationResponse[OidcClientRegistration response].
  547. [TIP]
  548. `OidcClientRegistrationEndpointFilter` also processes https://openid.net/specs/openid-connect-registration-1_0.html#ReadRequest[Client Read requests] and returns the https://openid.net/specs/openid-connect-registration-1_0.html#ReadResponse[OidcClientRegistration response].
  549. `OidcClientRegistrationEndpointFilter` is configured with the following defaults:
  550. * `*AuthenticationConverter*` -- An `OidcClientRegistrationAuthenticationConverter`.
  551. * `*AuthenticationManager*` -- An `AuthenticationManager` composed of `OidcClientRegistrationAuthenticationProvider` and `OidcClientConfigurationAuthenticationProvider`.
  552. * `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OidcClientRegistrationAuthenticationToken` and returns the `OidcClientRegistration` response.
  553. * `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthenticationException` and returns the `OAuth2Error` response.
  554. The OpenID Connect 1.0 Client Registration endpoint is an https://openid.net/specs/openid-connect-registration-1_0.html#ClientRegistration[OAuth2 protected resource], which *REQUIRES* an access token to be sent as a bearer token in the Client Registration (or Client Read) request.
  555. [IMPORTANT]
  556. The access token in a Client Registration request *REQUIRES* the OAuth2 scope `client.create`.
  557. [IMPORTANT]
  558. The access token in a Client Read request *REQUIRES* the OAuth2 scope `client.read`.
  559. The following example shows how to enable the OAuth2 resource server configuration:
  560. [source,java]
  561. ----
  562. @Bean
  563. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  564. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  565. new OAuth2AuthorizationServerConfigurer();
  566. http.apply(authorizationServerConfigurer);
  567. ...
  568. http.oauth2ResourceServer(resourceServer -> resourceServer.jwt(Customizer.withDefaults()));
  569. return http.build();
  570. }
  571. @Bean
  572. public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
  573. return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
  574. }
  575. ----
  576. [NOTE]
  577. A `JwtDecoder` `@Bean` is *REQUIRED* for the OpenID Connect 1.0 Client Registration endpoint.