春云侦探和Oauth2Template

时间:2016-09-07 10:42:56

标签: java rest spring-data microservices spring-cloud-sleuth

我们使用带有oauth2的微服务作为安全机制。 目前我们正在调用OAuth2RestTemplate这样的其他微服务:

template.postForObject("http://"+MY_DISCOVERY_NAME+"/path/to/restservice", params, Void.class);

我们正在使用@Autowired注入OAuth2RestTemplate,如下所示:

  @Configuration
  public class ApplicationConfig {

      @Autowired
      OAuth2RestTemplate oauth2Resttemplate;
      ...
      @Bean
      public MyBean getMyBean() {
          MyBeanImpl myBean = new MyBeanImpl();
          oauth2Resttemplate.setErrorHandler(getErrorHandler());
          myBean.setTemplate(oauth2Resttemplate);
          return myBean;
      }
      ...
  }

因此,我们的下一步是使调用成为可追踪的。 我们想使用spring cloud sleuth。

所以我添加了依赖项如下:

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

之后,Spring无法再自动装载OAuth2RestTemplate:

Caused by: java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.client.OAuth2RestTemplate

在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor中抛出IllegalArgumentException:

            @Override
            protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
                 ...

                            }
                            if (value != null) {
                                    ReflectionUtils.makeAccessible(field);
                                    field.set(bean, value);
                            }
                    }
                ...

field.set(bean, value);导致以下异常:

java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.client.OAuth2RestTemplate field my.package.ApplicationConfig.oauth2Resttemplate to com.sun.proxy.$Proxy120

如何将OAuth2RestTemplate与侦探结合使用?

由于

最高

1 个答案:

答案 0 :(得分:0)

问题是我连接了OAuth2RestTemplate而不是OAuth2RestOperations接口。

Wireing OAuth2RestOperations适用于我。