client-authentication.adoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. [[oauth2-client-authentication]]
  2. = [[oauth2Client-client-auth-support]]Client Authentication Support
  3. [[oauth2-client-authentication-client-credentials]]
  4. == [[oauth2Client-client-credentials-auth]]Client Credentials
  5. [[oauth2-client-authentication-client-credentials-client-secret-basic]]
  6. === Authenticate using `client_secret_basic`
  7. Client Authentication with HTTP Basic is supported out of the box and no customization is necessary to enable it.
  8. The default implementation is provided by `DefaultOAuth2TokenRequestHeadersConverter`.
  9. Given the following Spring Boot properties for an OAuth 2.0 client registration:
  10. [source,yaml]
  11. ----
  12. spring:
  13. security:
  14. oauth2:
  15. client:
  16. registration:
  17. okta:
  18. client-id: client-id
  19. client-secret: client-secret
  20. client-authentication-method: client_secret_basic
  21. authorization-grant-type: authorization_code
  22. ...
  23. ----
  24. The following example shows how to configure `WebClientReactiveAuthorizationCodeTokenResponseClient` to disable URL encoding of the client credentials:
  25. [tabs]
  26. ======
  27. Java::
  28. +
  29. [source,java,role="primary"]
  30. ----
  31. DefaultOAuth2TokenRequestHeadersConverter<OAuth2AuthorizationCodeGrantRequest> headersConverter =
  32. new DefaultOAuth2TokenRequestHeadersConverter<>();
  33. headersConverter.setEncodeClientCredentials(false);
  34. WebClientReactiveAuthorizationCodeTokenResponseClient tokenResponseClient =
  35. new WebClientReactiveAuthorizationCodeTokenResponseClient();
  36. tokenResponseClient.setHeadersConverter(headersConverter);
  37. ----
  38. Kotlin::
  39. +
  40. [source,kotlin,role="secondary"]
  41. ----
  42. val headersConverter = DefaultOAuth2TokenRequestHeadersConverter<OAuth2AuthorizationCodeGrantRequest>()
  43. headersConverter.setEncodeClientCredentials(false)
  44. val tokenResponseClient = WebClientReactiveAuthorizationCodeTokenResponseClient()
  45. tokenResponseClient.setHeadersConverter(headersConverter)
  46. ----
  47. ======
  48. [[oauth2-client-authentication-client-credentials-client-secret-post]]
  49. === Authenticate using `client_secret_post`
  50. Client Authentication with client credentials included in the request-body is supported out of the box and no customization is necessary to enable it.
  51. The following Spring Boot properties for an OAuth 2.0 client registration demonstrate the configuration:
  52. [source,yaml]
  53. ----
  54. spring:
  55. security:
  56. oauth2:
  57. client:
  58. registration:
  59. okta:
  60. client-id: client-id
  61. client-secret: client-secret
  62. client-authentication-method: client_secret_post
  63. authorization-grant-type: authorization_code
  64. ...
  65. ----
  66. [[oauth2-client-authentication-jwt-bearer]]
  67. == [[oauth2Client-jwt-bearer-auth]]JWT Bearer
  68. [NOTE]
  69. Please refer to JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants for further details on https://datatracker.ietf.org/doc/html/rfc7523#section-2.2[JWT Bearer] Client Authentication.
  70. The default implementation for JWT Bearer Client Authentication is `NimbusJwtClientAuthenticationParametersConverter`,
  71. which is a `Converter` that customizes the Token Request parameters by adding
  72. a signed JSON Web Token (JWS) in the `client_assertion` parameter.
  73. The `java.security.PrivateKey` or `javax.crypto.SecretKey` used for signing the JWS
  74. is supplied by the `com.nimbusds.jose.jwk.JWK` resolver associated with `NimbusJwtClientAuthenticationParametersConverter`.
  75. [[oauth2-client-authentication-jwt-bearer-private-key-jwt]]
  76. === Authenticate using `private_key_jwt`
  77. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  78. [source,yaml]
  79. ----
  80. spring:
  81. security:
  82. oauth2:
  83. client:
  84. registration:
  85. okta:
  86. client-id: okta-client-id
  87. client-authentication-method: private_key_jwt
  88. authorization-grant-type: authorization_code
  89. ...
  90. ----
  91. The following example shows how to configure `WebClientReactiveAuthorizationCodeTokenResponseClient`:
  92. [tabs]
  93. ======
  94. Java::
  95. +
  96. [source,java,role="primary"]
  97. ----
  98. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  99. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  100. // Assuming RSA key type
  101. RSAPublicKey publicKey = ...
  102. RSAPrivateKey privateKey = ...
  103. return new RSAKey.Builder(publicKey)
  104. .privateKey(privateKey)
  105. .keyID(UUID.randomUUID().toString())
  106. .build();
  107. }
  108. return null;
  109. };
  110. WebClientReactiveAuthorizationCodeTokenResponseClient tokenResponseClient =
  111. new WebClientReactiveAuthorizationCodeTokenResponseClient();
  112. tokenResponseClient.addParametersConverter(
  113. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  114. ----
  115. Kotlin::
  116. +
  117. [source,kotlin,role="secondary"]
  118. ----
  119. val jwkResolver: Function<ClientRegistration, JWK> =
  120. Function<ClientRegistration, JWK> { clientRegistration ->
  121. if (clientRegistration.clientAuthenticationMethod.equals(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) {
  122. // Assuming RSA key type
  123. var publicKey: RSAPublicKey = ...
  124. var privateKey: RSAPrivateKey = ...
  125. RSAKey.Builder(publicKey)
  126. .privateKey(privateKey)
  127. .keyID(UUID.randomUUID().toString())
  128. .build()
  129. }
  130. null
  131. }
  132. val tokenResponseClient = WebClientReactiveAuthorizationCodeTokenResponseClient()
  133. tokenResponseClient.addParametersConverter(
  134. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  135. )
  136. ----
  137. ======
  138. [[oauth2-client-authentication-jwt-bearer-client-secret-jwt]]
  139. === Authenticate using `client_secret_jwt`
  140. Given the following Spring Boot properties for an OAuth 2.0 Client registration:
  141. [source,yaml]
  142. ----
  143. spring:
  144. security:
  145. oauth2:
  146. client:
  147. registration:
  148. okta:
  149. client-id: okta-client-id
  150. client-secret: okta-client-secret
  151. client-authentication-method: client_secret_jwt
  152. authorization-grant-type: client_credentials
  153. ...
  154. ----
  155. The following example shows how to configure `WebClientReactiveClientCredentialsTokenResponseClient`:
  156. [tabs]
  157. ======
  158. Java::
  159. +
  160. [source,java,role="primary"]
  161. ----
  162. Function<ClientRegistration, JWK> jwkResolver = (clientRegistration) -> {
  163. if (clientRegistration.getClientAuthenticationMethod().equals(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) {
  164. SecretKeySpec secretKey = new SecretKeySpec(
  165. clientRegistration.getClientSecret().getBytes(StandardCharsets.UTF_8),
  166. "HmacSHA256");
  167. return new OctetSequenceKey.Builder(secretKey)
  168. .keyID(UUID.randomUUID().toString())
  169. .build();
  170. }
  171. return null;
  172. };
  173. WebClientReactiveClientCredentialsTokenResponseClient tokenResponseClient =
  174. new WebClientReactiveClientCredentialsTokenResponseClient();
  175. tokenResponseClient.addParametersConverter(
  176. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver));
  177. ----
  178. Kotlin::
  179. +
  180. [source,kotlin,role="secondary"]
  181. ----
  182. val jwkResolver = Function<ClientRegistration, JWK?> { clientRegistration: ClientRegistration ->
  183. if (clientRegistration.clientAuthenticationMethod == ClientAuthenticationMethod.CLIENT_SECRET_JWT) {
  184. val secretKey = SecretKeySpec(
  185. clientRegistration.clientSecret.toByteArray(StandardCharsets.UTF_8),
  186. "HmacSHA256"
  187. )
  188. OctetSequenceKey.Builder(secretKey)
  189. .keyID(UUID.randomUUID().toString())
  190. .build()
  191. }
  192. null
  193. }
  194. val tokenResponseClient = WebClientReactiveClientCredentialsTokenResponseClient()
  195. tokenResponseClient.addParametersConverter(
  196. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  197. )
  198. ----
  199. ======
  200. [[oauth2-client-authentication-jwt-bearer-assertion]]
  201. === Customizing the JWT assertion
  202. The JWT produced by `NimbusJwtClientAuthenticationParametersConverter` contains the `iss`, `sub`, `aud`, `jti`, `iat` and `exp` claims by default. You can customize the headers and/or claims by providing a `Consumer<NimbusJwtClientAuthenticationParametersConverter.JwtClientAuthenticationContext<T>>` to `setJwtClientAssertionCustomizer()`. The following example shows how to customize claims of the JWT:
  203. [tabs]
  204. ======
  205. Java::
  206. +
  207. [source,java,role="primary"]
  208. ----
  209. Function<ClientRegistration, JWK> jwkResolver = ...
  210. NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> converter =
  211. new NimbusJwtClientAuthenticationParametersConverter<>(jwkResolver);
  212. converter.setJwtClientAssertionCustomizer((context) -> {
  213. context.getHeaders().header("custom-header", "header-value");
  214. context.getClaims().claim("custom-claim", "claim-value");
  215. });
  216. ----
  217. Kotlin::
  218. +
  219. [source,kotlin,role="secondary"]
  220. ----
  221. val jwkResolver = ...
  222. val converter: NimbusJwtClientAuthenticationParametersConverter<OAuth2ClientCredentialsGrantRequest> =
  223. NimbusJwtClientAuthenticationParametersConverter(jwkResolver)
  224. converter.setJwtClientAssertionCustomizer { context ->
  225. context.headers.header("custom-header", "header-value")
  226. context.claims.claim("custom-claim", "claim-value")
  227. }
  228. ----
  229. ======
  230. [[oauth2-client-authentication-public]]
  231. == [[oauth2Client-public-auth]]Public Authentication
  232. Public Client Authentication is supported out of the box and no customization is necessary to enable it.
  233. The following Spring Boot properties for an OAuth 2.0 client registration demonstrate the configuration:
  234. [source,yaml]
  235. ----
  236. spring:
  237. security:
  238. oauth2:
  239. client:
  240. registration:
  241. okta:
  242. client-id: client-id
  243. client-authentication-method: none
  244. authorization-grant-type: authorization_code
  245. ...
  246. ----
  247. [NOTE]
  248. Public Clients are supported using https://tools.ietf.org/html/rfc7636[Proof Key for Code Exchange] (PKCE).
  249. PKCE will automatically be used when `client-authentication-method` is set to "none" (`ClientAuthenticationMethod.NONE`).