You don’t need to understand all the OAuth2 classes to use OAuth2.0 login. However, knowing the core OAuth2 classes and internals will help you configure OAuth2 authentication process and take full advantage of Spring Boot’s OAuth2.0 support.
If you are not familiar with OAuth2, see this post OAuth2 to understand OAuth2 concepts first.
OAuth2LoginAuthenticationToken
An AbstractAuthenticationToken for OAuth 2.0 Login, which leverages the OAuth 2.0 Authorization Code Grant Flow.
OAuth2LoginAuthenticationToken is used in OAuth2LoginAuthenticationFilter for login purpose
OAuth2AuthenticationToken contains the following fields
principal of type OAuth2User
authorizedClientRegistrationId
Note that OAuth2AuthenticationToken doesn’t contain AccessToken or RefreshToken, If you need to use AccessToken or RefreshToken, you can get it from OAuth2AuthorizedClient.
OAuth2User and OidcUser
OAuth2User is an Interface that extends OAuth2AuthenticatedPrincipal. DefaultOAuth2User is the default implementation
OAuth2AuthenticationToken contains OAuth2User.
OAuth2AuthenticatedPrincipal is an AuthenticatedPrincipal that represents the principal associated with an OAuth 2.0 token.
OidcUser extends OAuth2User. It is created when OpenID Connect is used. This class contains additional information including ID Token. ID Token is a Jwt returned only for OpenID Connect authentication.
ClientRegistration is a representation of a client registration with an OAuth 2.0 or OpenID Connect 1.0 Provider.
A client registration holds information, such as client id, client secret, authorization grant type, redirect URI, scope(s), authorization URI, token URI, and other details.
It has a Builder static nested class
Its registrationId uniquely identifies the ClientRegistration.
CommonOAuth2Provider preconfigure Client Registration Builders for Google, Github, Facebook and OKTA. That is why you only need to set client ID and client secret for Google ClientRegistration. Scope, authoriztionUri tokenUri etc are preconfigured already. If the provider is not one of them, you need to provide the necessary information.
The common way is to define ClientRegistration in application.yml. Another approach is to manually define ClientRegistration and provide the custom ClientRegistration to ClientRegistrationRepository.
A ClientRegistrationRepository is a repository of ClientRegistration(s).
The auto-configuration registers the ClientRegistrationRepository as a @Bean in the ApplicationContext so that it is available for dependency-injection
publicinterfaceClientRegistrationRepository { /** * Returns the client registration identified by the provided {@code registrationId}, or {@code null} if not found. */ ClientRegistration findByRegistrationId(String registrationId); }
InMemoryClientRegistrationRepository is an in-memory implementation of ClientRegistrationRepository. It uses a UnmodifiableConcurrentMap to store registration information. Spring Security does not provide another implementation.
If you don’t provide a ClientRegistrationRepository bean, Spring Security will create an InMemoryClientRegistrationRepository bean for you.
OAuth2AuthorizedClient
A representation of an OAuth 2.0 Authorized Client. A client is considered “authorized” when the End-User (Resource Owner) has granted authorization to the client to access it’s protected resources.
OAuth2AuthorizedClient serves the purpose of associating an OAuth2AccessToken (and optional OAuth2RefreshToken) to a ClientRegistration (client) and resource owner, who is the Principal end-user that granted the authorization.
You can get the current user’s OAuth2AuthorizedClient using OAuth2AuthorizedClientService.loadAuthorizedClient method.
OAuth2AuthorizedClient contains accessToken and an optional refreshToken.
AuthenticatedPrincipalOAuth2AuthorizedClientRepository - the default repository to use. see details below
HttpSessionOAuth2AuthorizedClientRepository - An implementation of an OAuth2AuthorizedClientRepository that stores OAuth2AuthorizedClient’s in the HttpSession.
AuthenticatedPrincipalOAuth2AuthorizedClientRepository is the default bean to use when no OAuth2AuthorizedClientRepository bean is found
An implementation of an OAuth2AuthorizedClientRepository that delegates to the provided OAuth2AuthorizedClientService if the current Principal is authenticated, otherwise, to the default (or provided) OAuth2AuthorizedClientRepository if the current request is unauthenticated (or anonymous).
Implementations of this interface are responsible for the management of Authorized Client(s), which provide the purpose of associating an Access Token credential to a Client and Resource Owner, who is the Principal that originally granted the authorization.
The default implementation of OAuth2AuthorizedClientService is InMemoryOAuth2AuthorizedClientService