login.adoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. [[webflux-oauth2-login]]
  2. = OAuth 2.0 Login
  3. The OAuth 2.0 Login feature provides an application with the capability to have users log in to the application by using their existing account at an OAuth 2.0 Provider (e.g.
  4. GitHub) or OpenID Connect 1.0 Provider (such as Google).
  5. OAuth 2.0 Login implements the use cases: "Login with Google" or "Login with GitHub".
  6. NOTE: OAuth 2.0 Login is implemented by using the *Authorization Code Grant*, as specified in the https://tools.ietf.org/html/rfc6749#section-4.1[OAuth 2.0 Authorization Framework] and https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth[OpenID Connect Core 1.0].
  7. [[webflux-oauth2-login-sample]]
  8. == Spring Boot 2.0 Sample
  9. Spring Boot 2.0 brings full auto-configuration capabilities for OAuth 2.0 Login.
  10. This section shows how to configure the {gh-samples-url}/reactive/webflux/java/oauth2/login[*OAuth 2.0 Login WebFlux sample*] using _Google_ as the _Authentication Provider_ and covers the following topics:
  11. * <<webflux-oauth2-login-sample-setup,Initial setup>>
  12. * <<webflux-oauth2-login-sample-redirect,Setting the redirect URI>>
  13. * <<webflux-oauth2-login-sample-config,Configure `application.yml`>>
  14. * <<webflux-oauth2-login-sample-start,Boot up the application>>
  15. [[webflux-oauth2-login-sample-setup]]
  16. === Initial setup
  17. To use Google's OAuth 2.0 authentication system for login, you must set up a project in the Google API Console to obtain OAuth 2.0 credentials.
  18. NOTE: https://developers.google.com/identity/protocols/OpenIDConnect[Google's OAuth 2.0 implementation] for authentication conforms to the https://openid.net/connect/[OpenID Connect 1.0] specification and is https://openid.net/certification/[OpenID Certified].
  19. Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page, starting in the section, "Setting up OAuth 2.0".
  20. After completing the "Obtain OAuth 2.0 credentials" instructions, you should have a new OAuth Client with credentials consisting of a Client ID and a Client Secret.
  21. [[webflux-oauth2-login-sample-redirect]]
  22. === Setting the redirect URI
  23. The redirect URI is the path in the application that the end-user's user-agent is redirected back to after they have authenticated with Google and have granted access to the OAuth Client _(<<webflux-oauth2-login-sample-setup,created in the previous step>>)_ on the Consent page.
  24. In the "Set a redirect URI" sub-section, ensure that the *Authorized redirect URIs* field is set to `http://localhost:8080/login/oauth2/code/google`.
  25. TIP: The default redirect URI template is `+{baseUrl}/login/oauth2/code/{registrationId}+`.
  26. The *_registrationId_* is a unique identifier for the xref:servlet/oauth2/oauth2-client.adoc#oauth2Client-client-registration[ClientRegistration].
  27. For our example, the `registrationId` is `google`.
  28. IMPORTANT: If the OAuth Client is running behind a proxy server, it is recommended to check xref:features/exploits/http.adoc#http-proxy-server[Proxy Server Configuration] to ensure the application is correctly configured.
  29. Also, see the supported xref:servlet/oauth2/oauth2-client.adoc#oauth2Client-auth-code-redirect-uri[ `URI` template variables] for `redirect-uri`.
  30. [[webflux-oauth2-login-sample-config]]
  31. === Configure `application.yml`
  32. Now that you have a new OAuth Client with Google, you need to configure the application to use the OAuth Client for the _authentication flow_.
  33. To do so:
  34. . Go to `application.yml` and set the following configuration:
  35. +
  36. [source,yaml]
  37. ----
  38. spring:
  39. security:
  40. oauth2:
  41. client:
  42. registration: <1>
  43. google: <2>
  44. client-id: google-client-id
  45. client-secret: google-client-secret
  46. ----
  47. +
  48. .OAuth Client properties
  49. ====
  50. <1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties.
  51. <2> Following the base property prefix is the ID for the xref:servlet/oauth2/oauth2-client.adoc#oauth2Client-client-registration[ClientRegistration], such as google.
  52. ====
  53. . Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier.
  54. [[webflux-oauth2-login-sample-start]]
  55. === Boot up the application
  56. Launch the Spring Boot 2.0 sample and go to `http://localhost:8080`.
  57. You are then redirected to the default _auto-generated_ login page, which displays a link for Google.
  58. Click on the Google link, and you are then redirected to Google for authentication.
  59. After authenticating with your Google account credentials, the next page presented to you is the Consent screen.
  60. The Consent screen asks you to either allow or deny access to the OAuth Client you created earlier.
  61. Click *Allow* to authorize the OAuth Client to access your email address and basic profile information.
  62. At this point, the OAuth Client retrieves your email address and basic profile information from the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] and establishes an authenticated session.
  63. [[webflux-oauth2-login-openid-provider-configuration]]
  64. == Using OpenID Provider Configuration
  65. For well known providers, Spring Security provides the necessary defaults for the OAuth Authorization Provider's configuration.
  66. If you are working with your own Authorization Provider that supports https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig[OpenID Provider Configuration] or https://tools.ietf.org/html/rfc8414#section-3[Authorization Server Metadata], the https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse[OpenID Provider Configuration Response]'s `issuer-uri` can be used to configure the application.
  67. [source,yml]
  68. ----
  69. spring:
  70. security:
  71. oauth2:
  72. client:
  73. provider:
  74. keycloak:
  75. issuer-uri: https://idp.example.com/auth/realms/demo
  76. registration:
  77. keycloak:
  78. client-id: spring-security
  79. client-secret: 6cea952f-10d0-4d00-ac79-cc865820dc2c
  80. ----
  81. The `issuer-uri` instructs Spring Security to query in series the endpoints `https://idp.example.com/auth/realms/demo/.well-known/openid-configuration`, `https://idp.example.com/.well-known/openid-configuration/auth/realms/demo`, or `https://idp.example.com/.well-known/oauth-authorization-server/auth/realms/demo` to discover the configuration.
  82. [NOTE]
  83. Spring Security will query the endpoints one at a time, stopping at the first that gives a 200 response.
  84. The `client-id` and `client-secret` are linked to the provider because `keycloak` is used for both the provider and the registration.
  85. [[webflux-oauth2-login-explicit]]
  86. == Explicit OAuth2 Login Configuration
  87. A minimal OAuth2 Login configuration is shown below:
  88. .Minimal OAuth2 Login
  89. ====
  90. .Java
  91. [source,java,role="primary"]
  92. ----
  93. @Bean
  94. ReactiveClientRegistrationRepository clientRegistrations() {
  95. ClientRegistration clientRegistration = ClientRegistrations
  96. .fromIssuerLocation("https://idp.example.com/auth/realms/demo")
  97. .clientId("spring-security")
  98. .clientSecret("6cea952f-10d0-4d00-ac79-cc865820dc2c")
  99. .build();
  100. return new InMemoryReactiveClientRegistrationRepository(clientRegistration);
  101. }
  102. @Bean
  103. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  104. http
  105. // ...
  106. .oauth2Login(withDefaults());
  107. return http.build();
  108. }
  109. ----
  110. .Kotlin
  111. [source,kotlin,role="secondary"]
  112. ----
  113. @Bean
  114. fun clientRegistrations(): ReactiveClientRegistrationRepository {
  115. val clientRegistration: ClientRegistration = ClientRegistrations
  116. .fromIssuerLocation("https://idp.example.com/auth/realms/demo")
  117. .clientId("spring-security")
  118. .clientSecret("6cea952f-10d0-4d00-ac79-cc865820dc2c")
  119. .build()
  120. return InMemoryReactiveClientRegistrationRepository(clientRegistration)
  121. }
  122. @Bean
  123. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  124. return http {
  125. oauth2Login { }
  126. }
  127. }
  128. ----
  129. ====
  130. Additional configuration options can be seen below:
  131. .Advanced OAuth2 Login
  132. ====
  133. .Java
  134. [source,java,role="primary"]
  135. ----
  136. @Bean
  137. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  138. http
  139. // ...
  140. .oauth2Login(oauth2 -> oauth2
  141. .authenticationConverter(converter)
  142. .authenticationManager(manager)
  143. .authorizedClientRepository(authorizedClients)
  144. .clientRegistrationRepository(clientRegistrations)
  145. );
  146. return http.build();
  147. }
  148. ----
  149. .Kotlin
  150. [source,kotlin,role="secondary"]
  151. ----
  152. @Bean
  153. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  154. return http {
  155. oauth2Login {
  156. authenticationConverter = converter
  157. authenticationManager = manager
  158. authorizedClientRepository = authorizedClients
  159. clientRegistrationRepository = clientRegistration
  160. }
  161. }
  162. }
  163. ----
  164. ====
  165. You may register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the default configuration, as shown in the following example:
  166. .GrantedAuthoritiesMapper Bean
  167. ====
  168. .Java
  169. [source,java,role="primary"]
  170. ----
  171. @Bean
  172. public GrantedAuthoritiesMapper userAuthoritiesMapper() {
  173. ...
  174. }
  175. @Bean
  176. SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  177. http
  178. // ...
  179. .oauth2Login(withDefaults());
  180. return http.build();
  181. }
  182. ----
  183. .Kotlin
  184. [source,kotlin,role="secondary"]
  185. ----
  186. @Bean
  187. fun userAuthoritiesMapper(): GrantedAuthoritiesMapper {
  188. // ...
  189. }
  190. @Bean
  191. fun webFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  192. return http {
  193. oauth2Login { }
  194. }
  195. }
  196. ----
  197. ====