README.adoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. = OAuth 2.0 Login Sample
  2. This guide provides instructions on setting up the sample application with OAuth 2.0 Login using an OAuth 2.0 Provider or OpenID Connect 1.0 Provider.
  3. The following sections provide detailed steps for setting up OAuth 2.0 Login for these Providers:
  4. * <<google-login, Google>>
  5. * <<github-login, GitHub>>
  6. * <<facebook-login, Facebook>>
  7. * <<okta-login, Okta>>
  8. [[google-login]]
  9. == Login with Google
  10. This section shows how to configure the sample application using Google as the Authentication Provider and covers the following topics:
  11. * <<google-initial-setup,Initial setup>>
  12. * <<google-redirect-uri,Setting the redirect URI>>
  13. * <<google-application-config,Configure security.xml>>
  14. * <<google-boot-application,Deploy and start the application>>
  15. [[google-initial-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
  19. https://openid.net/connect/[OpenID Connect 1.0] specification and is https://openid.net/certification/[OpenID Certified].
  20. Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page, starting in the section, "Setting up OAuth 2.0".
  21. 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.
  22. [[google-redirect-uri]]
  23. === Setting the redirect URI
  24. 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
  25. and have granted access to the OAuth Client _(created in the previous step)_ on the Consent page.
  26. 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`.
  27. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  28. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  29. IMPORTANT: If the application is running behind a proxy server, it is recommended to check https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#appendix-proxy-server[Proxy Server Configuration] to ensure the application is correctly configured.
  30. Also, see the supported https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#oauth2Client-auth-code-redirect-uri[`URI` template variables] for `redirect-uri`.
  31. [[google-application-config]]
  32. === Configure security.xml
  33. 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_. To do so:
  34. . Go to `security.xml` and set the following configuration:
  35. +
  36. [source,xml]
  37. ----
  38. <client-registration registration-id="google"
  39. client-id="google-client-id"
  40. client-secret="google-client-secret"
  41. provider-id="google"/>
  42. ----
  43. . Replace the values in the `client-id` and `client-secret` attributes with the OAuth 2.0 credentials you created earlier.
  44. [[google-boot-application]]
  45. === Deploy and start the application
  46. Deploy the WAR to a `Servlet` container and then go to `http://localhost:8080`.
  47. You are then redirected to the default _auto-generated_ login page, which displays a link for Google.
  48. Click on the Google link, and you are then redirected to Google for authentication.
  49. After authenticating with your Google account credentials, the next page presented to you is the Consent screen.
  50. The Consent screen asks you to either allow or deny access to the OAuth Client you created earlier.
  51. Click *Allow* to authorize the OAuth Client to access your email address and basic profile information.
  52. At this point, the OAuth Client retrieves your email address and basic profile information
  53. from the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] and establishes an authenticated session.
  54. [[github-login]]
  55. == Login with GitHub
  56. This section shows how to configure the sample application using GitHub as the Authentication Provider and covers the following topics:
  57. * <<github-register-application,Register OAuth application>>
  58. * <<github-application-config,Configure security.xml>>
  59. * <<github-boot-application,Deploy and start the application>>
  60. [[github-register-application]]
  61. === Register OAuth application
  62. To use GitHub's OAuth 2.0 authentication system for login, you must https://github.com/settings/applications/new[Register a new OAuth application].
  63. When registering the OAuth application, ensure the *Authorization callback URL* is set to `http://localhost:8080/login/oauth2/code/github`.
  64. The Authorization callback URL (redirect URI) is the path in the application that the end-user's user-agent is redirected back to after they have authenticated with GitHub
  65. and have granted access to the OAuth application on the _Authorize application_ page.
  66. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  67. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  68. IMPORTANT: If the application is running behind a proxy server, it is recommended to check https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#appendix-proxy-server[Proxy Server Configuration] to ensure the application is correctly configured.
  69. Also, see the supported https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#oauth2Client-auth-code-redirect-uri[`URI` template variables] for `redirect-uri`.
  70. [[github-application-config]]
  71. === Configure security.xml
  72. Now that you have a new OAuth application with GitHub, you need to configure the application to use the OAuth application for the _authentication flow_. To do so:
  73. . Go to `security.xml` and set the following configuration:
  74. +
  75. [source,xml]
  76. ----
  77. <client-registration registration-id="github"
  78. client-id="github-client-id"
  79. client-secret="github-client-secret"
  80. provider-id="github"/>
  81. ----
  82. . Replace the values in the `client-id` and `client-secret` attributes with the OAuth 2.0 credentials you created earlier.
  83. [[github-boot-application]]
  84. === Deploy and start the application
  85. Deploy the WAR to a `Servlet` container and then go to `http://localhost:8080`.
  86. You are then redirected to the default _auto-generated_ login page, which displays a link for GitHub.
  87. Click on the GitHub link, and you are then redirected to GitHub for authentication.
  88. After authenticating with your GitHub credentials, the next page presented to you is "Authorize application".
  89. This page will ask you to *Authorize* the application you created in the previous step.
  90. Click _Authorize application_ to allow the OAuth application to access your personal user data information.
  91. At this point, the OAuth Client retrieves your personal user information
  92. from the UserInfo Endpoint and establishes an authenticated session.
  93. [TIP]
  94. For detailed information returned from the UserInfo Endpoint, see the API documentation
  95. for https://developer.github.com/v3/users/#get-the-authenticated-user["Get the authenticated user"].
  96. [[facebook-login]]
  97. == Login with Facebook
  98. This section shows how to configure the sample application using Facebook as the Authentication Provider and covers the following topics:
  99. * <<facebook-register-application,Add a New App>>
  100. * <<facebook-application-config,Configure security.xml>>
  101. * <<facebook-boot-application,Deploy and start the application>>
  102. [[facebook-register-application]]
  103. === Add a New App
  104. To use Facebook's OAuth 2.0 authentication system for login, you must first https://developers.facebook.com/apps[Add a New App].
  105. Select "Create a New App" and then the "Create a New App ID" page is presented. Enter the Display Name, Contact Email, Category and then click "Create App ID".
  106. NOTE: The selection for the _Category_ field is not relevant but it's a required field - select "Local".
  107. The next page presented is "Product Setup". Click the "Get Started" button for the *Facebook Login* product.
  108. In the left sidebar, under _Products -> Facebook Login_, select _Settings_.
  109. For the field *Valid OAuth redirect URIs*, enter `http://localhost:8080/login/oauth2/code/facebook` then click _Save Changes_.
  110. The OAuth redirect URI is the path in the application that the end-user's user-agent is redirected back to after they have authenticated with Facebook
  111. and have granted access to the application on the _Authorize application_ page.
  112. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  113. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  114. IMPORTANT: If the application is running behind a proxy server, it is recommended to check https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#appendix-proxy-server[Proxy Server Configuration] to ensure the application is correctly configured.
  115. Also, see the supported https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#oauth2Client-auth-code-redirect-uri[`URI` template variables] for `redirect-uri`.
  116. [[facebook-application-config]]
  117. === Configure security.xml
  118. Now that you have created a new application with Facebook, you need to configure the sample application to use the application for the _authentication flow_. To do so:
  119. . Go to `security.xml` and set the following configuration:
  120. +
  121. [source,xml]
  122. ----
  123. <client-registration registration-id="facebook"
  124. client-id="facebook-client-id"
  125. client-secret="facebook-client-secret"
  126. provider-id="facebook"/>
  127. ----
  128. . Replace the values in the `client-id` and `client-secret` attributes with the OAuth 2.0 credentials you created earlier.
  129. [[facebook-boot-application]]
  130. === Deploy and start the application
  131. Deploy the WAR to a `Servlet` container and then go to `http://localhost:8080`.
  132. You are then redirected to the default _auto-generated_ login page, which displays a link for Facebook.
  133. Click on the Facebook link, and you are then redirected to Facebook for authentication.
  134. After authenticating with your Facebook credentials, the next page presented to you is "Authorize application".
  135. This page will ask you to *Authorize* the application you created in the previous step.
  136. Click _Authorize application_ to allow the OAuth application to access your _public profile_ and _email address_ information.
  137. At this point, the OAuth Client retrieves your personal user information
  138. from the UserInfo Endpoint and establishes an authenticated session.
  139. [[okta-login]]
  140. == Login with Okta
  141. This section shows how to configure the sample application using Okta as the Authentication Provider and covers the following topics:
  142. * <<okta-register-application,Add Application>>
  143. * <<okta-assign-application-people,Assign Application to People>>
  144. * <<okta-application-config,Configure security.xml>>
  145. * <<okta-boot-application,Deploy and start the application>>
  146. [[okta-register-application]]
  147. === Add Application
  148. To use Okta's OAuth 2.0 authentication system for login, you must first https://www.okta.com/developer/signup[create a developer account].
  149. Sign in to your account sub-domain and navigate to _Applications -> Applications_ and then select the "Add Application" button.
  150. From the "Add Application" page, select the "Create New App" button and enter the following:
  151. * *Platform:* Web
  152. * *Sign on method:* OpenID Connect
  153. Select the _Create_ button.
  154. On the "General Settings" page, enter the Application Name (for example, "Spring Security Okta Login") and then select the _Next_ button.
  155. On the "Configure OpenID Connect" page, enter `http://localhost:8080/login/oauth2/code/okta` for the field *Redirect URIs* and then select _Finish_.
  156. 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 Okta
  157. and have granted access to the application on the _Authorize application_ page.
  158. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  159. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  160. IMPORTANT: If the application is running behind a proxy server, it is recommended to check https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#appendix-proxy-server[Proxy Server Configuration] to ensure the application is correctly configured.
  161. Also, see the supported https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#oauth2Client-auth-code-redirect-uri[`URI` template variables] for `redirect-uri`.
  162. [[okta-assign-application-people]]
  163. === Assign Application to People
  164. From the "General" tab of the application, select the "Assignments" tab and then select the _Assign_ button.
  165. Select _Assign to People_ and assign your account to the application. Then select the _Save and Go Back_ button.
  166. [[okta-application-config]]
  167. === Configure security.xml
  168. Now that you have created a new application with Okta, you need to configure the sample application to use the application for the _authentication flow_. To do so:
  169. . Go to `security.xml` and set the following configuration:
  170. +
  171. [source,xml]
  172. ----
  173. <client-registration registration-id="okta"
  174. client-id="okta-client-id"
  175. client-secret="okta-client-secret"
  176. provider-id="okta"/>
  177. <provider provider-id="okta"
  178. authorization-uri="https://your-subdomain.oktapreview.com/oauth2/v1/authorize"
  179. token-uri="https://your-subdomain.oktapreview.com/oauth2/v1/token"
  180. user-info-uri="https://your-subdomain.oktapreview.com/oauth2/v1/userinfo"
  181. jwk-set-uri="https://your-subdomain.oktapreview.com/oauth2/v1/keys"/>
  182. ----
  183. . Replace the values in the `client-id` and `client-secret` attributes with the OAuth 2.0 credentials you created earlier.
  184. As well, replace `https://your-subdomain.oktapreview.com` in `authorization-uri`, `token-uri`, `user-info-uri` and `jwk-set-uri` with the sub-domain assigned to your account during the registration process.
  185. [[okta-boot-application]]
  186. === Deploy and start the application
  187. Deploy the WAR to a `Servlet` container and then go to `http://localhost:8080`.
  188. You are then redirected to the default _auto-generated_ login page, which displays a link for Okta.
  189. Click on the Okta link, and you are then redirected to Okta for authentication.
  190. After authenticating with your Okta account credentials, the OAuth Client retrieves your email address and basic profile information
  191. from the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] and establishes an authenticated session.