OAuth2RestTemplate with Ribbon + Eureka

前端 未结 1 555
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 21:09

I am working on microservices with Spring Cloud and Netflix OSS Eureka and Ribbon. I have another service running as oauth-server which provides OAuth2 token. All my microse

1条回答
  •  失恋的感觉
    2021-01-16 21:30

    I was finally able to make it work with following code:

    @Bean
    @LoadBalanced
    public OAuth2RestTemplate restTemplate(SpringClientFactory clientFactory) {
        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails());
        RibbonLoadBalancerClient ribbonLoadBalancerClient = new RibbonLoadBalancerClient(clientFactory);
        LoadBalancerInterceptor loadBalancerInterceptor = new LoadBalancerInterceptor(ribbonLoadBalancerClient);
        ClientCredentialsAccessTokenProvider accessTokenProvider = new ClientCredentialsAccessTokenProvider();
        accessTokenProvider.setInterceptors(Arrays.asList(loadBalancerInterceptor));
        restTemplate.setAccessTokenProvider(accessTokenProvider);
    
        return restTemplate;
    }
    

    0 讨论(0)
提交回复
热议问题