Home > Net >  "No converter found capable of converting from type String to OAuth2ClientProperties$Provider&q
"No converter found capable of converting from type String to OAuth2ClientProperties$Provider&q

Time:06-25

My application needs to be an API client, using Spring Security, Oauth 2.0, and OpenID, in Spring Boot. For OAuthClientConfiguration I followed this tutorial (starting with the "Creating Web Client-Based Application" header): https://developer.okta.com/blog/2021/05/05/client-credentials-spring-security#create-a-webclient-based-application

I'm getting this error upon starting the app:

Failed to bind properties under 'spring.security.oauth2.client.provider.authorization-uri' to org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties$Provider:

    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties$Provider]

My OAuthClientConfiguration class

@Configuration
public class OAuthClientConfiguration
{
    @Bean
    ReactiveClientRegistrationRepository  clientRegistrations(
            @Value(value = "${spring.security.oauth2.client.provider.token-uri}") String tokenUri,
            @Value(value = "${spring.security.oauth2.client.registration.IdOfMyApp.client-id}") String clientId,
            @Value(value = "${spring.security.oauth2.client.registration.IdOfMyApp.client-secret}") String clientSecret,
            @Value(value = "${spring.security.oauth2.client.registration.IdOfMyApp.authorization-grant-type}") String authorizationGrantType,
            @Value(value = "${spring.security.oauth2.client.registration.IdOfMyApp.redirect-uri}") String redirectUri,
            @Value(value = "${spring.security.oauth2.client.provider.authorization-uri}") String authorizationUri)
    {
        ClientRegistration registration = ClientRegistration
                .withRegistrationId("IdOfMyApp")
                .tokenUri(tokenUri)
                .clientId(clientId)
                .clientSecret(clientSecret)
                .scope("pr.pro", "pr.act", "openid", "offline")
                .authorizationGrantType(new AuthorizationGrantType(authorizationGrantType))
                .redirectUri(redirectUri)
                .authorizationUri(authorizationUri)
                .build();
        
        return new InMemoryReactiveClientRegistrationRepository(registration);
    }
    
    @Bean
    WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations)
    {
        InMemoryReactiveOAuth2AuthorizedClientService clientService = new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrations);
        AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager  = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations, clientService);
        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
        oauth.setDefaultClientRegistrationId("MarvelGuru");
        return WebClient.builder().filter(oauth).build();
    }
}

application.yaml file:

spring:
  security:
    oauth2:
      client:
        registration:
          IdOfMyApp:
            provider: https://api.provider.guys.com
            client-id: [my id here]
            client-secret: [my secret here]
            client-authentication-method: basic
            authorization-grant-type: authorization_code
            scope:
            - pr.pro
            - pr.act
            - openid
            - offline
            redirect-uri: https://my.domain.com/fallback
            client-name: My App Name
        provider:
          authorization-uri: https://api.provider.guys.com/oauth2/auth
          token-uri: https://api.provider.guys.com/oauth2/token
          issuer-uri: https://api.provider.guys.com
      resourceserver:
        jwt:
          issuer-uri: https://api.provider.guys.com
logging:
  level:
    '[org.springframework.web]': DEBUG

CodePudding user response:

You need to have a provider id key in the properties:

provider:
  IdOfMyApp:
    authorization-uri: https://api.provider.guys.com/oauth2/auth
    token-uri: https://api.provider.guys.com/oauth2/token
    issuer-uri: https://api.provider.guys.com
  • Related