how-to-userinfo.adoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [[how-to-userinfo]]
  2. = How-to: Customize the OpenID Connect 1.0 UserInfo response
  3. :index-link: ../how-to.html
  4. :docs-dir: ..
  5. This guide shows how to customize the xref:protocol-endpoints.adoc#oidc-user-info-endpoint[UserInfo endpoint] of the xref:index.adoc[Spring Authorization Server].
  6. The purpose of this guide is to demonstrate how to enable the endpoint and use the available customization options to produce a custom response.
  7. * xref:guides/how-to-userinfo.adoc#enable-user-info[Enable the User Info Endpoint]
  8. * xref:guides/how-to-userinfo.adoc#customize-user-info[Customize the User Info response]
  9. [[enable-user-info]]
  10. == Enable the User Info Endpoint
  11. The xref:protocol-endpoints.adoc#oidc-user-info-endpoint[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].
  12. > The Access Token obtained from an OpenID Connect Authentication Request MUST be sent as a Bearer Token, per Section 2 of https://openid.net/specs/openid-connect-core-1_0.html#RFC6750[OAuth 2.0 Bearer Token Usage] [RFC6750].
  13. Before customizing the response, you need to enable the UserInfo endpoint.
  14. The following listing shows how to enable the {spring-security-reference-base-url}/servlet/oauth2/resource-server/jwt.html[OAuth2 resource server configuration].
  15. [[sample.userinfo]]
  16. [source,java]
  17. ----
  18. include::{examples-dir}/main/java/sample/userinfo/EnableUserInfoSecurityConfig.java[]
  19. ----
  20. TIP: Click on the "Expand folded text" icon in the code sample above to display the full example.
  21. This configuration provides the following:
  22. <1> A Spring Security filter chain for the xref:{docs-dir}/protocol-endpoints.adoc[Protocol Endpoints].
  23. <2> Resource server support that allows User Info requests to be authenticated with access tokens.
  24. <3> An instance of `JwtDecoder` used to validate access tokens.
  25. [[customize-user-info]]
  26. == Customize the User Info response
  27. The following sections describe some options for customizing the user info response.
  28. * xref:guides/how-to-userinfo.adoc#customize-id-token[Customize the ID Token]
  29. * xref:guides/how-to-userinfo.adoc#customize-user-info-mapper[Customize the User Info Mapper]
  30. [[customize-id-token]]
  31. === Customize the ID Token
  32. By default, the user info response is generated by using claims from the `id_token` that are returned with the xref:protocol-endpoints.adoc#oauth2-token-endpoint[token response].
  33. Using the default strategy, https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims[standard claims] are returned only with the user info response based on the https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims[requested scopes] during authorization.
  34. The preferred way to customize the user info response is to add standard claims to the `id_token`.
  35. The following listing shows how to add claims to the `id_token`.
  36. [[sample.userinfo.idtoken]]
  37. [source,java]
  38. ----
  39. include::{examples-dir}/main/java/sample/userinfo/idtoken/IdTokenCustomizerConfig.java[]
  40. ----
  41. This configuration provides the following:
  42. <1> An instance of xref:core-model-components.adoc#oauth2-token-customizer[`OAuth2TokenCustomizer`] for customizing the `id_token`.
  43. <2> A custom service used to obtain user info in a domain-specific way.
  44. The following listing shows a custom service for looking up user info in a domain-specific way:
  45. [source,java]
  46. ----
  47. include::{examples-dir}/main/java/sample/userinfo/idtoken/OidcUserInfoService.java[]
  48. ----
  49. [[customize-user-info-mapper]]
  50. === Customize the User Info Mapper
  51. To fully customize the user info response, you can provide a custom user info mapper capable of generating the object used to render the response, which is an instance of the `OidcUserInfo` class from Spring Security.
  52. The mapper implementation receives an instance of `OidcUserInfoAuthenticationContext` with information about the current request, including the xref:core-model-components.adoc#oauth2-authorization[`OAuth2Authorization`].
  53. The following listing shows how to use the customization option that is available while working directly with the `OAuth2AuthorizationServerConfigurer`.
  54. [[sample.userinfo.jwt]]
  55. [source,java]
  56. ----
  57. include::{examples-dir}/main/java/sample/userinfo/jwt/JwtUserInfoMapperSecurityConfig.java[]
  58. ----
  59. This configuration maps claims from the access token (which is a JWT when using the xref:getting-started.adoc#sample.gettingStarted[Getting Started config]) to populate the user info response and provides the following:
  60. <1> A Spring Security filter chain for the xref:{docs-dir}/protocol-endpoints.adoc[Protocol Endpoints].
  61. <2> A user info mapper that maps claims in a domain-specific way.
  62. <3> An example showing the configuration option for customizing the user info mapper.
  63. <4> Resource server support that allows User Info requests to be authenticated with access tokens.
  64. <5> An example showing how to apply the `OAuth2AuthorizationServerConfigurer` to the Spring Security configuration.
  65. The user info mapper is not limited to mapping claims from a JWT, but this is a simple example that demonstrates the customization option.
  66. Similar to the xref:guides/how-to-userinfo.adoc#customize-id-token[example shown earlier] where we customize claims of the ID token, you can customize claims of the access token itself ahead of time, as in the following example:
  67. [source,java]
  68. ----
  69. include::{examples-dir}/main/java/sample/userinfo/jwt/JwtTokenCustomizerConfig.java[]
  70. ----
  71. Whether you customize the user info response directly or use this example and customize the access token, you can look up information in a database, perform an LDAP query, make a request to another service, or use any other means of obtaining the information you want to be presented in the user info response.