2

I'm writing a micro service as eureka client. I want to secure them using Oauth2 so I configured it as a ressource server.

@SpringBootApplication
@EnableEurekaClient
@EnableResourceServer
public class CustomerServiceApplication {

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

Here is the yml configuration.

security:
  oauth2:
    resource:
      user-info-uri: https://api.github.com/user

So far it works well and I can access to my protected enpoints withotu any problem. Now I'm trying to relay the access token to some other micro services that are also configured as ResourceServers. To achieve this I created an OAuth2RestTemplate Bean.

@LoadBalanced
    @Bean 
    public OAuth2RestTemplate oAuth2RestTemplate(UserInfoRestTemplateFactory factory) {
        return factory.getUserInfoRestTemplate();
    }

It is annotated with @LoadBalanced because I want to use eureka service ids in my called endpoints. Here is one of my controller mapping :

@Autowired
    private OAuth2RestTemplate oAuth2RestTemplate;

    @GetMapping("/{customerId:\\d+}")
    public @ResponseBody Customer getCustomer(@PathVariable long customerId) throws Exception{
        Customer customer =  this.oAuth2RestTemplate.getForObject(new URI("http://hbase-customer-reader/"+Long.toString(customerId)), Customer.class);
        return customer;
    }

Here is the issue : as I'm using a ribbon client, it looks like the url ai.github.com is interpreted as a service id when calling the underlying micro-service, resulting in this error :

UserInfoTokenServices      : Could not fetch user details: class java.lang.IllegalStateException, No instances available for api.github.com

and at the program init :

Client: api.github.com instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=api.github.com,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null

How can I make the UserInfoTokenServices use the github url (or the authorization server url that I will be using in production) as a real URL yet keeping eureka client activated so I can use service Ids in my restTemplate calls ?

Thanks for your help.

FabN
  • 21
  • 1
  • I forgot to post versions : spring-boot 1.5.10 and spring-cloud-security 1.2.1.RELEASE – FabN Mar 23 '18 at 10:32

0 Answers0