Class OAuth2Authenticator

All Implemented Interfaces:
IAuthenticator, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

public class OAuth2Authenticator extends AbstractOAuth2Authenticator
OAuth2 Authentication provider which contains 5 defaults (Google, GitHub, Facebook, Okta and Keycloak), as well as a custom setting which allows users to use their own IDP.

Default redirect url is as follows:

  {baseUrl}/-servlet-name-/oauth2/code/{registrationId}

{baseUrl} resolves to {baseScheme}://{baseHost}{basePort}{basePath}.
The redirect url has been modified to match the servlet path and is deduced from the default OAuth2LoginAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI. Authentication base URL is: -servlet-name-/oauth2/authorization

This authenticator should be configured by setting its type to 'OAUTH2', for example:

  application.security.console.authentication.type=OAUTH2
  application.security.console.authentication.provider=google
  application.security.console.authentication.clientId=my-client-id
  application.security.console.authentication.clientSecret=my-client-secret

Please note that we assume keycloak version 18 or higher is used, so we use the new endpoints and not the old ones. So `HOST_NAME/realms/test` is used and not `HOST_NAME/auth/realms/test`.

Author:
Niels Meijer
See Also:
  • Constructor Details

    • OAuth2Authenticator

      public OAuth2Authenticator()
  • Method Details

    • configure

      public org.springframework.security.web.SecurityFilterChain configure(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws Exception
      Description copied from class: AbstractServletAuthenticator
      Before building, configure the FilterChain.
      Specified by:
      configure in class AbstractServletAuthenticator
      Throws:
      Exception
    • getOrCreateClientRegistrationRepository

      public org.springframework.security.oauth2.client.registration.ClientRegistrationRepository getOrCreateClientRegistrationRepository()
    • getRegistration

      protected org.springframework.security.oauth2.client.registration.ClientRegistration getRegistration(@NonNull String provider, @NonNull ICredentials credentials)
    • createCustomBuilder

      public org.springframework.security.oauth2.client.registration.ClientRegistration.Builder createCustomBuilder(String name, String registrationId)