protocol-endpoints.adoc 54 KB

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