javaspringoauth-2.0spring-webclient

No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available


When I try to build my project, I get this error message:

: Unsatisfied dependency expressed through method 'webClient' parameter 0; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 
'org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository' 
available: expected at least 1 bean which qualifies as autowire candidate.

this is my application.properties:

spring.security.oauth2.client.registration.eipo.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.eipo.token-uri=https://devapi.somedomain.co.xx/v1/auth/token
spring.security.oauth2.client.registration.eipo.client-id=randomClientId
spring.security.oauth2.client.registration.eipo.client-secret=T#%*sty%xp4^sdxb(e*
spring.main.web-application-type= reactive

this is my configclass:

@Configuration
public class WebClientConfig {
  @Bean
  WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
      ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
        new ServerOAuth2AuthorizedClientExchangeFilterFunction(
          clientRegistrations,
          new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
      oauth.setDefaultClientRegistrationId("eipo"); 
      return WebClient.builder()
        .filter(oauth)
        .build();
  }
}

this is the service class:


@Slf4j
@Service
public class SettlementService
  @Autowired
  private WebClient webClient;

  public void getAuthThenGetResource(){
    ... //trying to get token
    log.info("got token");
    ... //get something from external resource
    log.info("got response");
  }
}

here is the pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-explorer</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--feign client to call other API as datasource-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
            <version>10.11</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-gson</artifactId>
            <version>10.11</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-slf4j</artifactId>
            <version>10.11</version>
        </dependency>
        <dependency>
            <groupId>com.playtika.reactivefeign</groupId>
            <artifactId>feign-reactor-jetty</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!--end of feign client dependencies-->
                
        <dependency>
          <groupId>io.swagger</groupId>
          <artifactId>swagger-annotations</artifactId>
          <version>1.5.20</version>
          <type>jar</type>
        </dependency>
     
        <!--WebClient -->
        <dependency> 
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-client</artifactId>
            <version>5.3.13.RELEASE</version>
            <type>jar</type>
        </dependency>
        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

I've included the parent & dependency part of it. This pom.xml could be the cause, but I'm not sure. What's wrong with the webclient bean? I've tried many solution i found from stackoverflow, none have worked. Thank's for the help.


Solution

  • You have two items that need to be fixed.

    Most importantly, with regards to missing beans is a missing 'starter'. In pom.xml replace

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-client</artifactId>
        <version>5.3.13.RELEASE</version>
        <type>jar</type>
    </dependency>
    

    with

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>
    

    When that is fixed it complains about a missing provider id. Adding the following to application.properties fixed that. You need to replace github with whatever provider is related to eipo:

    spring.security.oauth2.client.registration.eipo.provider=github