README.adoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. = OAuth 2.0 Login Sample
  2. Joe Grandja
  3. :toc:
  4. :security-site-url: https://projects.spring.io/spring-security/
  5. [.lead]
  6. This guide will walk you through the steps for setting up the sample application with OAuth 2.0 Login using an external _OAuth 2.0_ or _OpenID Connect 1.0_ Provider.
  7. The sample application is built with *Spring Boot 1.5* and the *spring-security-oauth2-client* module that is new in {security-site-url}[Spring Security 5.0].
  8. The following sections outline detailed steps for setting up OAuth 2.0 Login with these Providers:
  9. * <<google-login, Google>>
  10. * <<github-login, GitHub>>
  11. * <<facebook-login, Facebook>>
  12. * <<okta-login, Okta>>
  13. NOTE: The _"authentication flow"_ is realized using the *Authorization Code Grant*, as specified in the https://tools.ietf.org/html/rfc6749#section-4.1[OAuth 2.0 Authorization Framework].
  14. [[sample-app-content]]
  15. == Sample application content
  16. The sample application contains the following package structure and artifacts:
  17. *org.springframework.security.samples*
  18. [circle]
  19. * _OAuth2LoginApplication_ - the main class for the _Spring application_.
  20. ** *user*
  21. *** _GitHubOAuth2User_ - a custom _UserInfo_ type for <<github-login, GitHub Login>>.
  22. ** *web*
  23. *** _MainController_ - the root controller that displays user information after a successful login.
  24. *org.springframework.boot.autoconfigure.security.oauth2.client*
  25. [circle]
  26. * <<client-registration-auto-configuration-class, _ClientRegistrationAutoConfiguration_>> - a Spring Boot auto-configuration class
  27. that automatically registers a _ClientRegistrationRepository_ bean in the _ApplicationContext_.
  28. * <<oauth2-login-auto-configuration-class, _OAuth2LoginAutoConfiguration_>> - a Spring Boot auto-configuration class that automatically enables OAuth 2.0 Login.
  29. WARNING: The Spring Boot auto-configuration classes (and dependent resources) will eventually _live_ in the *Spring Boot Security Starter*.
  30. NOTE: See <<oauth2-login-auto-configuration, OAuth 2.0 Login auto-configuration>> for a detailed overview of the auto-configuration classes.
  31. [[google-login]]
  32. == Setting up *_Login with Google_*
  33. The goal for this section of the guide is to setup login using Google as the _Authentication Provider_.
  34. NOTE: https://developers.google.com/identity/protocols/OpenIDConnect[Google's OAuth 2.0 implementation] for authentication conforms to the
  35. http://openid.net/connect/[OpenID Connect] specification and is http://openid.net/certification/[OpenID Certified].
  36. [[google-login-register-credentials]]
  37. === Register OAuth 2.0 credentials
  38. In order 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.
  39. Follow the instructions on the https://developers.google.com/identity/protocols/OpenIDConnect[OpenID Connect] page starting in the section *_"Setting up OAuth 2.0"_*.
  40. After completing the sub-section, *_"Obtain OAuth 2.0 credentials"_*, you should have created a new *OAuth Client* with credentials consisting of a *Client ID* and *Client Secret*.
  41. [[google-login-redirect-uri]]
  42. === Setting the redirect URI
  43. The redirect URI is the path in the sample application that the end-user's user-agent is redirected back to after they have authenticated with Google
  44. and have granted access to the OAuth Client _(created from the <<google-login-register-credentials, previous step>>)_ on the *Consent screen* page.
  45. For the sub-section, *_"Set a redirect URI"_*, ensure the *Authorised redirect URIs* is set to *http://localhost:8080/oauth2/authorize/code/google*
  46. TIP: The default redirect URI is *_"{scheme}://{serverName}:{serverPort}/oauth2/authorize/code/{clientAlias}"_*.
  47. See <<oauth2-client-properties, OAuth client properties>> for more details on this default.
  48. [[google-login-configure-application-yml]]
  49. === Configuring application.yml
  50. Now that we have created a new OAuth Client with Google, we need to configure the sample application to use this OAuth Client for the _authentication flow_.
  51. Go to *_src/main/resources_* and edit *application.yml*. Add the following configuration:
  52. [source,yaml]
  53. ----
  54. security:
  55. oauth2:
  56. client:
  57. google:
  58. client-id: ${client-id}
  59. client-secret: ${client-secret}
  60. ----
  61. Replace *${client-id}* and *${client-secret}* with the OAuth 2.0 credentials created in the previous section <<google-login-register-credentials, Register OAuth 2.0 credentials>>.
  62. [TIP]
  63. .OAuth client properties
  64. ====
  65. . *security.oauth2.client* is the *_base property prefix_* for OAuth client properties.
  66. . Just below the *_base property prefix_* is the *_client property key_*, for example *security.oauth2.client.google*.
  67. . At the base of the *_client property key_* are the properties for specifying the configuration for an OAuth Client.
  68. A list of these properties are detailed in <<oauth2-client-properties, OAuth client properties>>.
  69. ====
  70. [[google-login-run-sample]]
  71. === Running the sample
  72. Launch the Spring Boot application by running *_org.springframework.security.samples.OAuth2LoginApplication_*.
  73. After the application successfully starts up, go to http://localhost:8080. You will be redirected to http://localhost:8080/login, which will display an _auto-generated login page_ with an anchor link for *Google*.
  74. Click through on the Google link and you'll be redirected to Google for authentication.
  75. After you authenticate using your Google credentials, the next page presented to you will be the *Consent screen*.
  76. The Consent screen will ask you to either *_Allow_* or *_Deny_* access to the OAuth Client you created in the previous step <<google-login-register-credentials, Register OAuth 2.0 credentials>>.
  77. Click *_Allow_* to authorize the OAuth Client to access your _email address_ and _basic profile_ information.
  78. At this point, the OAuth Client will retrieve your email address and basic profile information from the http://openid.net/specs/openid-connect-core-1_0.html#UserInfo[*UserInfo Endpoint*] and establish an _authenticated session_.
  79. The home page will then be displayed showing the user attributes retrieved from the *UserInfo Endpoint*, for example, name, email, profile, sub, etc.
  80. [[oauth2-login-auto-configuration]]
  81. == OAuth 2.0 Login auto-configuration
  82. As you worked through this guide and setup OAuth 2.0 Login with one of the Providers,
  83. we hope you noticed the ease in configuration and setup required in getting the sample up and running?
  84. And you may be asking, how does this all work? Thanks to some Spring Boot auto-configuration _magic_,
  85. we were able to automatically register the OAuth Client(s) configured in the `Environment`,
  86. as well, provide a minimal security configuration for OAuth 2.0 Login for these registered OAuth Client(s).
  87. The following provides an overview of the Spring Boot auto-configuration classes:
  88. [[client-registration-auto-configuration-class]]
  89. *_org.springframework.boot.autoconfigure.security.oauth2.client.ClientRegistrationAutoConfiguration_*::
  90. `ClientRegistrationAutoConfiguration` is responsible for registering a `ClientRegistrationRepository` _bean_ with the `ApplicationContext`.
  91. The `ClientRegistrationRepository` is composed of one or more `ClientRegistration` instances, which are created from the OAuth client properties
  92. configured in the `Environment` that are prefixed with `security.oauth2.client.[client-alias]`, for example, `security.oauth2.client.google`.
  93. NOTE: `ClientRegistrationAutoConfiguration` also loads a _resource_ named *oauth2-clients-defaults.yml*,
  94. which provides a set of default client property values for a number of _well-known_ Providers.
  95. More on this in the later section <<oauth2-default-client-properties, Default client property values>>.
  96. [[oauth2-login-auto-configuration-class]]
  97. *_org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2LoginAutoConfiguration_*::
  98. `OAuth2LoginAutoConfiguration` is responsible for enabling OAuth 2.0 Login,
  99. only if there is a `ClientRegistrationRepository` _bean_ available in the `ApplicationContext`.
  100. WARNING: The auto-configuration classes (and dependent resources) will eventually _live_ in the *Spring Boot Security Starter*.
  101. [[oauth2-client-properties]]
  102. === OAuth client properties
  103. The following specifies the common set of properties available for configuring an OAuth Client.
  104. [TIP]
  105. ====
  106. - *security.oauth2.client* is the *_base property prefix_* for OAuth client properties.
  107. - Just below the *_base property prefix_* is the *_client property key_*, for example *security.oauth2.client.google*.
  108. - At the base of the *_client property key_* are the properties for specifying the configuration for an OAuth Client.
  109. ====
  110. - *client-authentication-method* - the method used to authenticate the _Client_ with the _Provider_. Supported values are *header* and *form*.
  111. - *authorized-grant-type* - the OAuth 2.0 Authorization Framework defines the https://tools.ietf.org/html/rfc6749#section-1.3.1[Authorization Code] grant type,
  112. which is used to realize the _"authentication flow"_. Currently, this is the only supported grant type.
  113. - *redirect-uri* - this is the client's _registered_ redirect URI that the _Authorization Server_ redirects the end-user's user-agent
  114. to after the end-user has authenticated and authorized access for the client.
  115. NOTE: The default redirect URI is _"{scheme}://{serverName}:{serverPort}/oauth2/authorize/code/{clientAlias}"_, which leverages *URI template variables*.
  116. - *scopes* - a comma-delimited string of scope(s) requested during the _Authorization Request_ flow, for example: _openid, email, profile_
  117. NOTE: _OpenID Connect 1.0_ defines these http://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims[standard scopes]: _profile, email, address, phone_
  118. NOTE: Non-standard scopes may be defined by a standard _OAuth 2.0 Provider_. Please consult the Provider's OAuth API documentation to learn which scopes are supported.
  119. - *authorization-uri* - the URI used by the client to redirect the end-user's user-agent to the _Authorization Server_ in order to obtain authorization from the end-user (the _Resource Owner_).
  120. - *token-uri* - the URI used by the client when exchanging an _Authorization Grant_ (for example, Authorization Code) for an _Access Token_ at the _Authorization Server_.
  121. - *user-info-uri* - the URI used by the client to access the protected resource *UserInfo Endpoint*, in order to obtain attributes of the end-user.
  122. - *user-info-converter* - the `Converter` implementation class used to convert the *UserInfo Response* to a `UserInfo` (_OpenID Connect 1.0 Provider_) or `OAuth2User` instance (_Standard OAuth 2.0 Provider_).
  123. TIP: The `Converter` implementation class for an _OpenID Connect 1.0 Provider_ is *org.springframework.security.oauth2.client.user.converter.UserInfoConverter*
  124. and for a standard _OAuth 2.0 Provider_ it's *org.springframework.security.oauth2.client.user.converter.OAuth2UserConverter*.
  125. - *user-info-name-attribute-key* - the _key_ used to retrieve the *Name* of the end-user from the `Map` of available attributes in `UserInfo` or `OAuth2User`.
  126. NOTE: _OpenID Connect 1.0_ defines the http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims[*"name"* Claim], which is the end-user's full name and is the default used for `UserInfo`.
  127. IMPORTANT: Standard _OAuth 2.0 Provider's_ may vary the naming of their *Name* attribute. Please consult the Provider's *UserInfo* API documentation.
  128. This is a *_required_* property when *user-info-converter* is set to `OAuth2UserConverter`.
  129. - *client-name* - this is a descriptive name used for the client. The name may be used in certain scenarios, for example, when displaying the name of the client in the _auto-generated login page_.
  130. - *client-alias* - an _alias_ which uniquely identifies the client. It *must be* unique within a `ClientRegistrationRepository`.
  131. [[oauth2-default-client-properties]]
  132. === Default client property values
  133. As noted previously, <<client-registration-auto-configuration-class, `ClientRegistrationAutoConfiguration`>> loads a _resource_ named *oauth2-clients-defaults.yml*,
  134. which provides a set of default client property values for a number of _well-known_ Providers.
  135. For example, the *authorization-uri*, *token-uri*, *user-info-uri* rarely change for a Provider and therefore it makes sense to
  136. provide a set of defaults in order to reduce the configuration required by the user.
  137. Below are the current set of default client property values:
  138. .oauth2-clients-defaults.yml
  139. [source,yaml]
  140. ----
  141. security:
  142. oauth2:
  143. client:
  144. google:
  145. client-authentication-method: header
  146. authorized-grant-type: authorization_code
  147. redirect-uri: "{scheme}://{serverName}:{serverPort}{baseAuthorizeUri}/{clientAlias}"
  148. scopes: openid, email, profile
  149. authorization-uri: "https://accounts.google.com/o/oauth2/auth"
  150. token-uri: "https://accounts.google.com/o/oauth2/token"
  151. user-info-uri: "https://www.googleapis.com/oauth2/v3/userinfo"
  152. user-info-converter: "org.springframework.security.oauth2.client.user.converter.UserInfoConverter"
  153. client-name: Google
  154. client-alias: google
  155. github:
  156. client-authentication-method: header
  157. authorized-grant-type: authorization_code
  158. redirect-uri: "{scheme}://{serverName}:{serverPort}{baseAuthorizeUri}/{clientAlias}"
  159. scopes: user
  160. authorization-uri: "https://github.com/login/oauth/authorize"
  161. token-uri: "https://github.com/login/oauth/access_token"
  162. user-info-uri: "https://api.github.com/user"
  163. user-info-converter: "org.springframework.security.oauth2.client.user.converter.OAuth2UserConverter"
  164. client-name: GitHub
  165. client-alias: github
  166. facebook:
  167. client-authentication-method: form
  168. authorized-grant-type: authorization_code
  169. redirect-uri: "{scheme}://{serverName}:{serverPort}{baseAuthorizeUri}/{clientAlias}"
  170. scopes: public_profile, email
  171. authorization-uri: "https://www.facebook.com/v2.8/dialog/oauth"
  172. token-uri: "https://graph.facebook.com/v2.8/oauth/access_token"
  173. user-info-uri: "https://graph.facebook.com/me"
  174. user-info-converter: "org.springframework.security.oauth2.client.user.converter.OAuth2UserConverter"
  175. client-name: Facebook
  176. client-alias: facebook
  177. okta:
  178. client-authentication-method: header
  179. authorized-grant-type: authorization_code
  180. redirect-uri: "{scheme}://{serverName}:{serverPort}{baseAuthorizeUri}/{clientAlias}"
  181. scopes: openid, email, profile
  182. user-info-converter: "org.springframework.security.oauth2.client.user.converter.UserInfoConverter"
  183. client-name: Okta
  184. client-alias: okta
  185. ----
  186. = Appendix
  187. '''
  188. [[configure-non-spring-boot-app]]
  189. == Configuring a _Non-Spring-Boot_ application
  190. If you are not using Spring Boot for your application, you will not be able to leverage the auto-configuration features for OAuth 2.0 Login.
  191. You will be required to provide your own _security configuration_ in order to enable OAuth 2.0 Login.
  192. The following sample code demonstrates a minimal security configuration for enabling OAuth 2.0 Login.
  193. Assuming we have a _properties file_ named *oauth2-clients.properties* on the _classpath_ and it specifies all the _required_ properties for an OAuth Client, specifically _"Google"_:
  194. .oauth2-clients.properties
  195. [source,properties]
  196. ----
  197. security.oauth2.client.google.client-id=${client-id}
  198. security.oauth2.client.google.client-secret=${client-secret}
  199. security.oauth2.client.google.client-authentication-method=header
  200. security.oauth2.client.google.authorized-grant-type=authorization_code
  201. security.oauth2.client.google.redirect-uri=http://localhost:8080/oauth2/authorize/code/google
  202. security.oauth2.client.google.scopes=openid,email,profile
  203. security.oauth2.client.google.authorization-uri=https://accounts.google.com/o/oauth2/auth
  204. security.oauth2.client.google.token-uri=https://accounts.google.com/o/oauth2/token
  205. security.oauth2.client.google.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo
  206. security.oauth2.client.google.user-info-converter=org.springframework.security.oauth2.client.user.converter.UserInfoConverter
  207. security.oauth2.client.google.client-name=Google
  208. security.oauth2.client.google.client-alias=google
  209. ----
  210. The following _security configuration_ will enable OAuth 2.0 Login using _"Google"_ as the _Authentication Provider_:
  211. [source,java]
  212. ----
  213. @EnableWebSecurity
  214. @PropertySource("classpath:oauth2-clients.properties")
  215. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  216. private Environment environment;
  217. public SecurityConfig(Environment environment) {
  218. this.environment = environment;
  219. }
  220. @Override
  221. protected void configure(HttpSecurity http) throws Exception {
  222. http
  223. .authorizeRequests()
  224. .anyRequest().authenticated()
  225. .and()
  226. .oauth2Login()
  227. .clients(clientRegistrationRepository())
  228. .userInfoEndpoint()
  229. .userInfoTypeConverter(
  230. new UserInfoConverter(),
  231. new URI("https://www.googleapis.com/oauth2/v3/userinfo"));
  232. }
  233. @Bean
  234. public ClientRegistrationRepository clientRegistrationRepository() {
  235. List<ClientRegistration> clientRegistrations = Collections.singletonList(
  236. clientRegistration("security.oauth2.client.google."));
  237. return new InMemoryClientRegistrationRepository(clientRegistrations);
  238. }
  239. private ClientRegistration clientRegistration(String clientPropertyKey) {
  240. String clientId = this.environment.getProperty(clientPropertyKey + "client-id");
  241. String clientSecret = this.environment.getProperty(clientPropertyKey + "client-secret");
  242. ClientAuthenticationMethod clientAuthenticationMethod = ClientAuthenticationMethod.valueOf(
  243. this.environment.getProperty(clientPropertyKey + "client-authentication-method").toUpperCase());
  244. AuthorizationGrantType authorizationGrantType = AuthorizationGrantType.valueOf(
  245. this.environment.getProperty(clientPropertyKey + "authorized-grant-type").toUpperCase());
  246. String redirectUri = this.environment.getProperty(clientPropertyKey + "redirect-uri");
  247. String[] scopes = this.environment.getProperty(clientPropertyKey + "scopes").split(",");
  248. String authorizationUri = this.environment.getProperty(clientPropertyKey + "authorization-uri");
  249. String tokenUri = this.environment.getProperty(clientPropertyKey + "token-uri");
  250. String userInfoUri = this.environment.getProperty(clientPropertyKey + "user-info-uri");
  251. String clientName = this.environment.getProperty(clientPropertyKey + "client-name");
  252. String clientAlias = this.environment.getProperty(clientPropertyKey + "client-alias");
  253. return new ClientRegistration.Builder(clientId)
  254. .clientSecret(clientSecret)
  255. .clientAuthenticationMethod(clientAuthenticationMethod)
  256. .authorizedGrantType(authorizationGrantType)
  257. .redirectUri(redirectUri)
  258. .scopes(scopes)
  259. .authorizationUri(authorizationUri)
  260. .tokenUri(tokenUri)
  261. .userInfoUri(userInfoUri)
  262. .clientName(clientName)
  263. .clientAlias(clientAlias)
  264. .build();
  265. }
  266. }
  267. ----