Search code examples
spring-bootspring-security-oauth2

Failed to bind properties clientid to OAuth2ClientProperties$Registration


I am working on a spring boot app in order to communicate with another secured API. For that, I implement a OAuth2RestTemplate with the following properties but it failed when I run the application.

@Configuration
@EnableOAuth2Client
class RestTemplateConfiguration {

    @Bean
    @ConfigurationProperties("oauth2")
    public OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails() {
        return new ClientCredentialsResourceDetails();
    }

    @Bean
    public OAuth2RestTemplate oAuth2RestTemplate(
            @Qualifier("oAuth2ProtectedResourceDetails") OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails) {
        return new OAuth2RestTemplate(oAuth2ProtectedResourceDetails);
    }

}

application.properties

spring:
  security:
    oauth2:
      client:
        registration:
            clientId: xxxxxxxxxxxxxxxxxx
            clientSecret: xxxxxxxxxxxxxxxxx
            accessTokenUri: https://xxxxxx/oauth2/access_token
            scope: openid profile xxxxxxxxxxx
            authorizationGrantType: client_credentials

The exception I get when I run the code

Failed to bind properties under 'spring.security.oauth2.client.registration.clientid' to org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties$Registration:

Solution

  • I ran into the same issue. The reference documentation mentions that you need to provide a registrationId within the property "path"

    You application.properties should look like this:

    spring:
      security:
        oauth2:
          client:
            registration:
              YOUR_REGISTRATION_ID: # <----------
                clientId: xxxxxxxxxxxxxxxxxx
                clientSecret: xxxxxxxxxxxxxxxxx
                accessTokenUri: https://xxxxxx/oauth2/access_token
                scope: openid profile xxxxxxxxxxx
                authorizationGrantType: client_credentials