如何使用 Spring Boot 和 @FeignClient 发送承载授权令牌 [英] How to send Bearer authorization token using Spring Boot and @FeignClient

查看:94
本文介绍了如何使用 Spring Boot 和 @FeignClient 发送承载授权令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot 编写一个与 HTTP 休息服务器交互的应用程序.我要连接的其中一台服务器 (Wit.ai) 使用 Beaerer 授权令牌.产生成功响应的 curl 请求如下所示:

I am using Spring Boot to write an application that interacts with HTTP rest servers. One of the servers I'm connecting to (Wit.ai) uses a beaerer authorization token. A curl request that yields a successful response looks like this:

GET /message?q=sample message HTTP/1.1
Host: api.wit.ai
Authorization: Bearer XXXXXXXXXXXXX
Cache-Control: no-cache
Postman-Token: 526c3a11-8e61-4552-aa19-e913f6473753

wit.ai 文档对令牌的描述如下,

The wit.ai docs say the following about the token,

Wit.ai 使用 OAuth2 作为授权层.因此,每个 API 请求都必须包含一个带有令牌的 Authorize HTTP 标头访问令牌是特定于应用的.

Wit.ai uses OAuth2 as an authorization layer. As such, every API request must contain an Authorize HTTP header with a token Access tokens are app specific.

我正在尝试使用 @FeignClient 向 Spring Boot 应用程序中的此端点发送 GET 请求.但是我端点似乎没有接受我的授权令牌.这是我的 FeignClient 代码

I am trying to send a GET request to this endpoint in a Spring Boot app using @FeignClient. However I the endpoint doesn't seem to be accepting my authorization token. Here is my FeignClient code

@FeignClient(name="witGetter", url = "${wit.url}")
    public interface WitGetter {
        @RequestMapping(method = RequestMethod.GET, value = "/message?v=20180507q={text}",
            headers = {"Authorization: Bearer XXXXXXXXXXXXX"})
        WitResponse getWitResponse(@PathVariable("text") final String text);
}

传递这样的授权令牌的正确方法是什么?我尝试了其他一些事情,但无济于事.感谢您的任何建议!!!

What is the proper way to pass such an authorization token? I have tried a few other things but to no avail. Thanks for any advice!!!

顺便说一下,以下代码使用传统的 Feign 接口工作,但在这种情况下我需要使用 @FeignClient.

By the way, the following code works using a traditional Feign interface, but I need to use @FeignClient in this case.

public interface WitGetter {
    @Headers("Authorization: Bearer XXXXXXXXXXXXX")
    @RequestLine("GET /message?q={text}")
    WitResponse getWitResponse(@Param("text") String text);
}

(下面的代码在一个单独的配置文件中)

(code below is in a separate config file)

@Bean
    public WitGetter defaultWitGetter(@Value("https://api.wit.ai") final String witUrl){
        return Feign.builder().decoder(new GsonDecoder()).target(WitGetter.class, witUrl);

}

编辑

我在使用上述代码时得到的错误代码是:

The error code I get when using the above code is:

线程main"中的异常 feign.FeignException: status 400 reading WitGetter#getWitResponse(String,String);内容:{"error" : "身份验证错误,请检查令牌/参数",代码":无身份验证"}

Exception in thread "main" feign.FeignException: status 400 reading WitGetter#getWitResponse(String,String); content: { "error" : "Bad auth, check token/params", "code" : "no-auth" }

推荐答案

通过 Spring Cloud 使用 Feign 时,您可以像定义标准 Spring MVC 控制器一样使用它.

When using Feign via Spring Cloud, you can use it as you would define a standard Spring MVC controller.

请在此处查看我关于使用 Feign 传递标头的文章:https://arnoldgalovics.com/passing-headers-with-spring-cloud-假装/

Please check my article here about passing headers with Feign: https://arnoldgalovics.com/passing-headers-with-spring-cloud-feign/

快速提示:您可以在方法定义中添加一个@RequestHeader("Authorization") String bearerToken 参数.

Quick hint: you can add a @RequestHeader("Authorization") String bearerToken parameter to the method definition.

然后当然像 client.method(..., "Bearer " + token)

这篇关于如何使用 Spring Boot 和 @FeignClient 发送承载授权令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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