我正在将微服务作为eureka客户端编写。我想使用Oauth2保护它们,因此我将其配置为资源服务器。
@SpringBootApplication
@EnableEurekaClient
@EnableResourceServer
public class CustomerServiceApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(CustomerServiceApplication.class, args);
}
这是yml配置。
security:
oauth2:
resource:
user-info-uri: https://api.github.com/user
到目前为止,它运作良好,我可以访问我的受保护的enpoints任何问题。 现在,我尝试将访问令牌转发到其他一些也配置为ResourceServers的微服务。为此,我创建了一个OAuth2RestTemplate Bean。
@LoadBalanced
@Bean
public OAuth2RestTemplate oAuth2RestTemplate(UserInfoRestTemplateFactory factory) {
return factory.getUserInfoRestTemplate();
}
它使用@LoadBalanced注释,因为我想在我的被叫端点中使用eureka服务ID。这是我的控制器映射之一:
@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;
}
问题在于:由于我使用功能区客户端,在调用基础微服务时,网址ai.github.com被解释为服务ID,导致此错误:
UserInfoTokenServices : Could not fetch user details: class java.lang.IllegalStateException, No instances available for api.github.com
并在程序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
如何让UserInfoTokenServices使用github url(或我将在生产中使用的授权服务器URL)作为真实的URL,同时保持eureka客户端激活,以便我可以在restTemplate调用中使用服务ID?
感谢您的帮助。