【问题标题】:oauth2ClientContext is not getting injectedoauth2ClientContext 没有被注入
【发布时间】:2020-03-09 09:35:57
【问题描述】:

应用程序没有启动,因为有两个候选 bean。启动应用程序时出现以下错误。

***************************
 APPLICATION FAILED TO START
***************************
Description:

Parameter 0 of method oauth2RestTemplate in com.classpath.assetservice.AssetServiceApplication required a bean of type 'org.springframework.security.oauth2.client.OAuth2ClientContext' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'oauth2ClientContext' in 'OAuth2RestOperationsConfiguration.RequestScopedConfiguration' not loaded because OAuth Client ID did not find security.oauth2.client.client-id property
    - Bean method 'oauth2ClientContext' in 'OAuth2RestOperationsConfiguration.SingletonScopedConfiguration' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on OAuth2RestOperationsConfiguration.ClientCredentialsCondition.NoWebApplication @ConditionalOnWebApplication found 'session' scope and did not find reactive web application classes; NestedCondition on OAuth2RestOperationsConfiguration.ClientCredentialsCondition.ClientCredentialsConfigured @ConditionalOnProperty (security.oauth2.client.grant-type=client_credentials) did not find property 'grant-type'


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.client.OAuth2ClientContext' in your configuration.


Process finished with exit code 1

下面是我的Controller

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import java.util.Collections;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableResourceServer
public class AssetServiceApplication {

    @Bean
    public OAuth2RestTemplate oauth2RestTemplate(
            OAuth2ClientContext oauth2ClientContext,
            OAuth2ProtectedResourceDetails details) {
        return new OAuth2RestTemplate(details, oauth2ClientContext);
    }

    public static void main(String[] args) {
        SpringApplication.run(AssetServiceApplication.class, args);
    }

下面是对应的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-security</artifactId>
</dependency>

我哪里错了。

【问题讨论】:

    标签: spring-cloud spring-security-oauth2 spring-oauth2


    【解决方案1】:

    根据 Spring 文档,您只需添加 @EnableOAuth2Client,Spring Boot 就会为您创建一个 OAuth2ClientContext。详情请见here

    【讨论】:

      【解决方案2】:

      通过在 application.yml 文件中添加以下属性解决了这个问题

      security:
        oauth2:
          resource:
            userInfoUri: http://localhost:8901/auth/user
          client: # This property (client.grant-type)
            grant-type: client_credentials
      

      【讨论】:

        猜你喜欢
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多