5

我正在尝试使用 spring cloud feign 创建一个简单的 REST 客户端来使用受 OAuth2 安全令牌保护的服务。我正在使用 OAuth2FeignRequestInterceptor 添加不记名令牌,请检查我的以下代码。我正面临 401。当尝试调试我的代码时,我在我的 Request 对象中找不到承载令牌。

@Configuration
@EnableConfigurationProperties(value=OAuth2ClientCredentialsProperties.class)
@EnableOAuth2Client
@Profile(OAuth2Profiles.CLIENT_CREDENTIALS)
public class ClientCredentialsConfiguration {

    @Autowired
    private OAuth2ClientCredentialsProperties oAuth2ClientCredentialsProperties;

    @Bean
    @Qualifier("ClientCredentialsOAuth2FeignRequestInterceptor")
    public OAuth2FeignRequestInterceptor oauth2schemeRequestInterceptor() {
        return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), oauth2schemeResourceDetails());
    }

    @Bean
    public ClientCredentialsResourceDetails oauth2schemeResourceDetails() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        details.setClientId(oAuth2ClientCredentialsProperties.getClientId());
        details.setClientSecret(oAuth2ClientCredentialsProperties.getClientSecret());
        details.setAccessTokenUri(oAuth2ClientCredentialsProperties.getAccessTokenUri());
        details.setScope(oAuth2ClientCredentialsProperties.getScopes());
        return details;
    }

}

这是我的客户端界面

@FeignClient(name = "test", url = "http://localhost:8080", configuration = ClientCredentialsConfiguration.class)
interface GitHubClient {


    @RequestMapping(value = "/api/v1/products",
            produces = "application/json",
            consumes = "application/json;charset=UTF-8",
            method = RequestMethod.POST)
    ResponseEntity<Object> testUsingPOST(@RequestBody TestDTO testDTO);

我的属性在下面

server.port=10080

security.user.name=admin
security.user.password=admin
security.basic.enabled=false

org.springframework.boot.autoconfigure.EnableAutoConfiguration=sgcib.clips.bcsapi.configuration.ClientCredentialsConfiguration

feign.oauth2.enabled=true

feign.hystrix.enabled=false

我的主课

@SpringBootApplication
@EnableWebMvc
@Controller
@EnableFeignClients
@EnableAutoConfiguration
public class App extends SpringBootServletInitializer {


    @Autowired
    private GitHubClient gitHub;

    @RequestMapping("/")
    public String home() {
        return "index";
    }

    @RequestMapping("/{owner}")
    @ResponseBody
    public ResponseEntity<Object> contributors(@PathVariable String owner) {
        return gitHub.productsUsingPOST(new TestDTO());
    }

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

2 回答 2

2

我也在使用 feign 和请求拦截器。对我来说,它可以将@Bean 方法的返回类型更改为通用的 RequestInterceptor。像这样:

@Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
    return new OAuth2FeignRequestInterceptor(...);
}

本教程还很好地描述了如何使用 feign 设置 OAuth: spring-cloud-feign-oauth2

于 2017-08-24T14:58:41.687 回答
0

我发现了问题,这是由于类中的@Profile(OAuth2Profiles.CLIENT_CREDENTIALS)注释ClientCredentialsConfiguration。我错过了启用配置文件,所以我的拦截器 Bean 根本没有加载。

于 2017-08-25T15:19:06.033 回答