client-authentication.adoc 9.1 KB

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