liveBook · Manning

ClientRegistrationRepository

As explained in Spring Security in Action, Second Edition

ClientRegistrationRepository is a Spring Security component that stores and retrieves OAuth 2 client registration details.

ClientRegistrationRepository stores and retrieves the ClientRegistration objects an OAuth 2 client needs. Define it as a Spring bean when property-based configuration cannot provide enough flexibility, such as dynamic credentials or conditional provider access. section 16.1.4 section 16.2

Build a custom repository by:

  • Injecting credential values or other configuration. section 16.1.4
  • Creating a ClientRegistration with credentials, redirect or token endpoints, scopes, and grant settings as needed. section 16.1.4 section 16.2
  • Returning an InMemoryClientRegistrationRepository or implementing the contract against a database, vault, or endpoint. section 16.1.4

In-memory registration

Expose a ClientRegistrationRepository bean that returns an InMemoryClientRegistrationRepository containing a Google registration. Spring Security then uses the bean to obtain client registration details for OAuth 2 login.

Listing 16.5 Implementing custom logic
@Configuration
public class SecurityConfig {

  @Value("${client-id}")
  private String clientId;                                         #1

  @Value("${client-secret}")
  private String clientSecret;                                     #1

  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) 
    throws Exception {
    
    http.oauth2Login(Customizer.withDefaults());

    http.authorizeHttpRequests(
      c -> c.anyRequest().authenticated()
    );

    return http.build();
  }

  @Bean
  public ClientRegistrationRepository clientRegistrationRepository() {
    return new InMemoryClientRegistrationRepository(
      this.googleClientRegistration());                            #2
  }
  
  private ClientRegistration googleClientRegistration() {
    return CommonOAuth2Provider.GOOGLE.getBuilder("google")        #3
                .clientId(clientId)
                .clientSecret(clientSecret)
                .build();
  }

}

Inspect the annotated configuration showing property injection, registration construction, and repository exposure as a Spring bean. section 16.1.4

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines the contract and demonstrates an in-memory bean, while deferring detailed database, vault, and endpoint implementations.
Examples
Uses an in-memory Google registration and a client-credentials registration with a token URI and scope.
What you can do after
Configure an OAuth 2 client with explicit registration details and replace in-memory lookup with a custom data source.
How it connects
Connects ClientRegistration with OAuth 2 login, OAuth 2 client configuration, and OAuth2AuthorizedClientManager.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.