带有 OAuth2RestTemplate 的 Spring Cloud Feign [英] Spring Cloud Feign with OAuth2RestTemplate

查看:49
本文介绍了带有 OAuth2RestTemplate 的 Spring Cloud Feign的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施 Feign Clients 以从用户的服务中获取我的用户信息,目前我正在使用 oAuth2RestTemplate 进行请求,它可以工作.但是现在我希望更改为 Feign,但我收到错误代码 401 可能是因为它不携带用户令牌,所以有一种方法可以自定义,如果 Spring 对 Feign 的支持正在使用,那么我可以使用 RestTemplate我自己的豆子?

I'm trying to implement Feign Clients to get my user info from the user's service, currently I'm requesting with oAuth2RestTemplate, it works. But now I wish to change to Feign, but I'm getting error code 401 probably because it doesn't carry the user tokens, so there is a way to customize, if Spring support for Feign is using, a RestTemplate so I can use my own Bean?

今天我就是这样实现的

服务客户

@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();
}

推荐答案

Feign 不使用 RestTemplate,因此您必须找到不同的方法.如果您创建 feign.RequestInterceptor 类型的 @Bean 它将应用于所有请求,因此可能其中一个带有 OAuth2RestTemplate 的请求(只是为了管理代币获取)将是最好的选择.

Feign doesn't use a RestTemplate so you'd have to find a different way. If you create a @Bean of type feign.RequestInterceptor it will be applied to all requests, so maybe one of those with an OAuth2RestTemplate in it (just to manage the token acquisition) would be the best option.

这篇关于带有 OAuth2RestTemplate 的 Spring Cloud Feign的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆