当我尝试构建我的项目时,我收到此错误消息:
: 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.
这是我的应用程序。属性:
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
这是我的配置类:
@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();
}
}
这是服务等级:
@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");
}
}
这是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>
我已经包含了它的父级和依赖项部分。这个 pom.xml 可能是原因,但我不确定。 webclient bean 有什么问题?我已经尝试了从 stackoverflow 找到的许多解决方案,但没有一个有效。谢谢你的帮助。
您有两项需要修复。
最重要的是,缺少豆子就是缺少“开胃菜”。在 pom.xml 中替换
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
<version>5.3.13.RELEASE</version>
<type>jar</type>
</dependency>
与
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
修复后,它会抱怨缺少提供商 ID。将以下内容添加到 application.properties 修复了该问题。您需要将 github 替换为与 eipo 相关的任何提供程序:
spring.security.oauth2.client.registration.eipo.provider=github
有时仅与身份验证相关的应用程序属性和 Maven 配置是不够的。
让我们尝试显式启用 spring 的自动配置属性并创建默认值
ClientRegistrationRepository
build.gradle
dependencies {
implementation "org.springframework.boot:spring-boot-starter-oauth2-client"
}
Java
@Configuration
@EnableWebSecurity
@EnableAutoConfiguration
@EnableConfigurationProperties(OAuth2ClientProperties.class)
public class OAuth2Config {
@Bean
@ConditionalOnMissingBean
public ClientRegistrationRepository clientRegistrationRepository(
OAuth2ClientProperties oAuth2ClientProperties) {
var clientRegistrations =
List.copyOf(
new OAuth2ClientPropertiesMapper(oAuth2ClientProperties)
.asClientRegistrations()
.values());
return new InMemoryClientRegistrationRepository(clientRegistrations);
}
}