README.adoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. = Samples
  2. [[messages-sample]]
  3. == Messages Sample
  4. The messages sample integrates `spring-security-oauth2-client` and `spring-security-oauth2-resource-server` with *Spring Authorization Server*.
  5. The username is `user1` and the password is `password`.
  6. [[run-messages-sample]]
  7. === Run the Sample
  8. * Run Authorization Server -> `./gradlew -b samples/default-authorizationserver/samples-default-authorizationserver.gradle bootRun`
  9. * Run Resource Server -> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun`
  10. * Run Client -> `./gradlew -b samples/messages-client/samples-messages-client.gradle bootRun`
  11. * Go to `http://127.0.0.1:8080`
  12. [[federated-identity-sample]]
  13. == Federated Identity Sample
  14. The federated identity sample builds on the messages sample above, adding social login and federated identity features to *Spring Authorization Server* using custom configuration.
  15. [[google-login]]
  16. === Login with Google
  17. This section shows how to configure Spring Security using Google as an Authentication Provider.
  18. [[google-initial-setup]]
  19. ==== Initial setup
  20. 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.
  21. NOTE: https://developers.google.com/identity/protocols/OpenIDConnect[Google's OAuth 2.0 implementation] for authentication conforms to the
  22. https://openid.net/connect/[OpenID Connect 1.0] specification and is https://openid.net/certification/[OpenID Certified].
  23. Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page, starting in the section, "Setting up OAuth 2.0".
  24. 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.
  25. [[google-redirect-uri]]
  26. ==== Setting the redirect URI
  27. 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
  28. and have granted access to the OAuth Client _(created in the previous step)_ on the Consent page.
  29. In the "Set a redirect URI" sub-section, ensure that the *Authorized redirect URIs* field is set to `http://localhost:9000/login/oauth2/code/google-idp`.
  30. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  31. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  32. [[google-application-config]]
  33. ==== Configure application.yml
  34. 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:
  35. . Go to `application.yml` and set the following configuration:
  36. +
  37. [source,yaml]
  38. ----
  39. spring:
  40. security:
  41. oauth2:
  42. client:
  43. registration: <1>
  44. google-idp: <2>
  45. provider: google
  46. client-id: google-client-id
  47. client-secret: google-client-secret
  48. ----
  49. +
  50. .OAuth Client properties
  51. ====
  52. <1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties.
  53. <2> Following the base property prefix is the ID for the `ClientRegistration`, such as google-idp.
  54. ====
  55. . Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier.
  56. Alternatively, you can set the following environment variables in the Spring Boot application:
  57. * `GOOGLE_CLIENT_ID`
  58. * `GOOGLE_CLIENT_SECRET`
  59. [[github-login]]
  60. === Login with GitHub
  61. This section shows how to configure Spring Security using Github as an Authentication Provider.
  62. [[github-register-application]]
  63. ==== Register OAuth application
  64. To use GitHub's OAuth 2.0 authentication system for login, you must https://github.com/settings/applications/new[Register a new OAuth application].
  65. When registering the OAuth application, ensure the *Authorization callback URL* is set to `http://localhost:9000/login/oauth2/code/github-idp`.
  66. 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
  67. and have granted access to the OAuth application on the _Authorize application_ page.
  68. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  69. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  70. [[github-application-config]]
  71. ==== Configure application.yml
  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 `application.yml` and set the following configuration:
  74. +
  75. [source,yaml]
  76. ----
  77. spring:
  78. security:
  79. oauth2:
  80. client:
  81. registration: <1>
  82. github-idp: <2>
  83. provider: github
  84. client-id: github-client-id
  85. client-secret: github-client-secret
  86. ----
  87. +
  88. .OAuth Client properties
  89. ====
  90. <1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties.
  91. <2> Following the base property prefix is the ID for the `ClientRegistration`, such as github-idp.
  92. ====
  93. . Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier.
  94. Alternatively, you can set the following environment variables in the Spring Boot application:
  95. * `GITHUB_CLIENT_ID`
  96. * `GITHUB_CLIENT_SECRET`
  97. [[run-federated-identity-sample]]
  98. === Run the Sample
  99. * Run Authorization Server -> `./gradlew -b samples/federated-identity-authorizationserver/samples-federated-identity-authorizationserver.gradle bootRun`
  100. * Run Resource Server -> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun`
  101. * Run Client -> `./gradlew -b samples/messages-client/samples-messages-client.gradle bootRun`
  102. * Go to `http://127.0.0.1:8080`