README.adoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. = Samples
  2. [[default-sample]]
  3. == Default Sample
  4. The default sample provides the minimal configuration to get started with Spring Authorization Server.
  5. [[spa-sample]]
  6. == SPA (Single Page Application) Sample
  7. The SPA sample provides a reference implementation of the https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps-19#name-backend-for-frontend-bff[Backend For Frontend (BFF)] application architecture pattern.
  8. The *spa-client* is the _frontend_ SPA implemented with Angular and the *backend-for-spa-client* is the _backend_ application.
  9. The *backend-for-spa-client* uses https://spring.io/projects/spring-cloud-gateway[Spring Cloud Gateway] to route `/userinfo` (UserInfo Endpoint) requests to *demo-authorizationserver* and `/messages` requests to *messages-resource*.
  10. The *backend-for-spa-client* performs the authorization flows and stores the access tokens.
  11. The *spa-client* is never exposed the access tokens and directly communicates with the *backend-for-spa-client* via an authenticated session cookie.
  12. [[run-spa-sample]]
  13. === Run the Sample
  14. * Run Authorization Server -> `./gradlew -b samples/demo-authorizationserver/samples-demo-authorizationserver.gradle bootRun`
  15. * Run Resource Server -> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun`
  16. * Run Backend -> `./gradlew -b samples/backend-for-spa-client/samples-backend-for-spa-client.gradle bootRun`
  17. * Run Frontend -> `ng serve` (from `samples/spa-client` directory)
  18. ** *NOTE:* Angular must be installed locally before running `ng serve`. See https://angular.dev/installation[installation instructions].
  19. * Go to `http://127.0.0.1:4200`
  20. ** Login with credentials -> user1 \ password
  21. [[demo-sample]]
  22. == Demo Sample
  23. The demo sample provides custom configuration for various features implemented by Spring Authorization Server.
  24. [[run-demo-sample]]
  25. === Run the Sample
  26. * Run Authorization Server -> `./gradlew -b samples/demo-authorizationserver/samples-demo-authorizationserver.gradle bootRun`
  27. * Run Client -> `./gradlew -b samples/demo-client/samples-demo-client.gradle bootRun`
  28. * Run Resource Server -> `./gradlew -b samples/messages-resource/samples-messages-resource.gradle bootRun`
  29. * Go to `http://127.0.0.1:8080`
  30. ** Login with credentials -> user1 \ password
  31. [[configuring-social-login]]
  32. === Configuring Social Login
  33. The demo sample may be configured to provide social login capability.
  34. [[google-login]]
  35. ==== Login with Google
  36. This section shows how to configure Google as a social login provider.
  37. [[google-initial-setup]]
  38. ===== Initial setup
  39. 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.
  40. NOTE: https://developers.google.com/identity/protocols/OpenIDConnect[Google's OAuth 2.0 implementation] for authentication conforms to the
  41. https://openid.net/connect/[OpenID Connect 1.0] specification and is https://openid.net/certification/[OpenID Certified].
  42. Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page, starting in the section, "Setting up OAuth 2.0".
  43. 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.
  44. [[google-redirect-uri]]
  45. ===== Setting the redirect URI
  46. 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
  47. and have granted access to the OAuth Client _(created in the previous step)_ on the Consent page.
  48. 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`.
  49. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  50. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  51. [[google-application-config]]
  52. ===== Configure application.yml
  53. 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:
  54. . Go to `application.yml` and set the following configuration:
  55. +
  56. [source,yaml]
  57. ----
  58. spring:
  59. security:
  60. oauth2:
  61. client:
  62. registration: <1>
  63. google-idp: <2>
  64. provider: google
  65. client-id: google-client-id
  66. client-secret: google-client-secret
  67. ----
  68. +
  69. .OAuth Client properties
  70. ====
  71. <1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties.
  72. <2> Following the base property prefix is the ID for the `ClientRegistration`, such as google-idp.
  73. ====
  74. . Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier.
  75. Alternatively, you can set the following environment variables in the Spring Boot application:
  76. * `GOOGLE_CLIENT_ID`
  77. * `GOOGLE_CLIENT_SECRET`
  78. [[github-login]]
  79. ==== Login with GitHub
  80. This section shows how to configure GitHub as a social login provider.
  81. [[github-register-application]]
  82. ===== Register OAuth application
  83. To use GitHub's OAuth 2.0 authentication system for login, you must https://github.com/settings/applications/new[Register a new OAuth application].
  84. When registering the OAuth application, ensure the *Authorization callback URL* is set to `http://localhost:9000/login/oauth2/code/github-idp`.
  85. 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
  86. and have granted access to the OAuth application on the _Authorize application_ page.
  87. TIP: The default redirect URI template is `{baseUrl}/login/oauth2/code/{registrationId}`.
  88. The *_registrationId_* is a unique identifier for the `ClientRegistration`.
  89. [[github-application-config]]
  90. ===== Configure application.yml
  91. 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:
  92. . Go to `application.yml` and set the following configuration:
  93. +
  94. [source,yaml]
  95. ----
  96. spring:
  97. security:
  98. oauth2:
  99. client:
  100. registration: <1>
  101. github-idp: <2>
  102. provider: github
  103. client-id: github-client-id
  104. client-secret: github-client-secret
  105. ----
  106. +
  107. .OAuth Client properties
  108. ====
  109. <1> `spring.security.oauth2.client.registration` is the base property prefix for OAuth Client properties.
  110. <2> Following the base property prefix is the ID for the `ClientRegistration`, such as github-idp.
  111. ====
  112. . Replace the values in the `client-id` and `client-secret` property with the OAuth 2.0 credentials you created earlier.
  113. Alternatively, you can set the following environment variables in the Spring Boot application:
  114. * `GITHUB_CLIENT_ID`
  115. * `GITHUB_CLIENT_SECRET`