2015-04-03 110 views
3

我试图实现Feign客户端从用户的服务,我目前正在请求与oAuth2RestTemplate,它的工作原理我的用户信息。但是现在我想更改为Feign,但是我得到错误代码401可能是因为它没有携带用户令牌,所以如果Spring对Feign的支持使用了RestTemplate,我可以使用它来自定义方式我自己的豆?春天云Feign OAuth2RestTemplate

今天我实现这样

该服务的客户端

@Retryable({RestClientException.class, TimeoutException.class, InterruptedException.class}) 
@HystrixCommand(fallbackMethod = "getFallback") 
public Promise<ResponseEntity<UserProtos.User>> get() { 
    logger.debug("Requiring discovery of user"); 
    Promise<ResponseEntity<UserProtos.User>> promise = Broadcaster.<ResponseEntity<UserProtos.User>>create(reactorEnv, DISPATCHER) 
      .observe(Promises::success) 
      .observeError(Exception.class, (o, e) -> Promises.error(reactorEnv, ERROR_DISPATCHER, e)) 
      .filter(entity -> entity.getStatusCode().is2xxSuccessful()) 
      .next(); 
    promise.onNext(this.client.getUserInfo()); 
    return promise; 

} 

而客户端

@FeignClient("account") 
public interface UserInfoClient { 

    @RequestMapping(value = "/uaa/user",consumes = MediaTypes.PROTOBUF,method = RequestMethod.GET) 
    ResponseEntity<UserProtos.User> getUserInfo(); 
} 

回答

7

假死不使用RestTemplate所以你必须找到不同的方式。如果您创建@Bean类型的feign.RequestInterceptor它将应用于所有请求,所以也许其中一个OAuth2RestTemplate(仅用于管理令牌获取)将是最佳选择。

+0

感谢@戴夫,创造RequestInterceptor'的'一个bean,并从设置承载令牌o Request在RequestTemplate头上工作 – 2015-04-04 15:52:21

+0

太棒了。也许你可以将它贡献回春季云安全? – 2015-04-05 16:58:09

+0

当然,我会在GitHub上提出一张票,所以我们可以在那里讨论它 – 2015-04-05 20:52:07

3

这是我的解决方案,只是为了补充与源代码中的另一个答案,实现接口feign.RequestInterceptor

@Bean 
public RequestInterceptor requestTokenBearerInterceptor() { 
    return new RequestInterceptor() { 
     @Override 
     public void apply(RequestTemplate requestTemplate) { 
      OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) 
        SecurityContextHolder.getContext().getAuthentication().getDetails(); 

      requestTemplate.header("Authorization", "bearer " + details.getTokenValue()); 
     } 
    }; 
} 
+0

我使用了一个具体的实现,所以我可以避免空指针并发送令牌给未受保护的资源,以防请求中不包含令牌或经过验证的用户,因为现在我们无法自定义每个客户端拦截器。如果你有兴趣,你可以在这里查看:https://github.com/crly/commons/blob/master/src/main/java/io/curly/commons/config/feign/OAuth2FeignRequestInterceptor.java – 2015-07-09 17:20:47

+0

很好的解决方案!在我的风景中(一个资源服务器请求另一个资源服务器)总是需要经过认证的用户/令牌。感谢分享! – 2015-07-09 18:18:07

+0

只需添加我的两分钱:配置sessionCreationPolicy至少为“IF_REQUIRED”是很重要的,因为SecurityContext将是空的,否则=)感谢这个解决方案,最终得到令牌中继 – 2016-07-08 10:37:46