【问题标题】:Spring Cloud Feign with OAuth2RestTemplate使用 OAuth2RestTemplate 的 Spring Cloud Feign
【发布时间】:2015-06-08 23:37:18
【问题描述】:

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

今天我就是这样实现的

客户的服务

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

【问题讨论】:

    标签: spring-security-oauth2 spring-cloud


    【解决方案1】:

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

    【讨论】:

    • 感谢 @Dave,创建了一个 RequestInterceptor 的 bean 并从 RequestTemplate 标头上的 oauth 设置 Bearer 令牌
    • 太棒了。也许您可以将其贡献回 spring-cloud-security?
    • 是的,当然,我会在 GitHub 上提出一张票,所以我们可以在那里讨论
    • 您好 Joao,感谢您的贡献。我有同样的问题,但我没有得到你的解决方案,也许你可以看看? stackoverflow.com/questions/32185734/…
    【解决方案2】:

    这是我的解决方案,只是用源代码补充另一个答案,实现接口 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());
            }
        };
    }
    

    【讨论】:

    • 我使用了一个具体的实现,所以我可以避免空指针并将令牌发送到未受保护的资源,以防请求不包含令牌或经过身份验证的用户,因为现在我们无法自定义每个客户端拦截器。如果您有兴趣可以在这里查看:github.com/crly/commons/blob/master/src/main/java/io/curly/…
    • 很好的解决方案!.. 在我的风景中(一个资源服务器请求另一个),始终需要经过身份验证的用户/令牌。感谢分享!
    • 只是为了增加我的两分钱:将 sessionCreationPolicy 至少配置为“IF_REQUIRED”很重要,否则 SecurityContext 将为空 =) 感谢您提供此解决方案,最终通过 feign 获得令牌中继
    • 这个可以和Hystrix一起使用吗?
    • 对于hystrix,你需要这个hystrix.shareSecurityContext=true
    猜你喜欢
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2018-06-14
    • 2015-10-21
    • 2018-08-31
    • 2015-10-12
    • 2018-04-01
    相关资源
    最近更新 更多