[英]How to send Bearer authorization token using Spring Boot and @FeignClient
I am using Spring Boot to write an application that interacts with HTTP rest servers. 我使用Spring Boot编写一个与HTTP rest服务器交互的应用程序。 One of the servers I'm connecting to (Wit.ai) uses a beaerer authorization token. 我正在连接的服务器之一(Wit.ai)使用beaerer授权令牌。 A curl request that yields a successful response looks like this: 产生成功响应的curl请求如下所示:
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
The wit.ai docs say the following about the token, wit.ai文档说出关于令牌的以下内容,
Wit.ai uses OAuth2 as an authorization layer. Wit.ai使用OAuth2作为授权层。 As such, every API request must contain an Authorize HTTP header with a token Access tokens are app specific. 因此,每个API请求必须包含带有令牌的Authorize HTTP标头。访问令牌是特定于应用程序的。
I am trying to send a GET request to this endpoint in a Spring Boot app using @FeignClient. 我正在尝试使用@FeignClient在Spring Boot应用程序中向此端点发送GET请求。 However I the endpoint doesn't seem to be accepting my authorization token. 但是我端点似乎不接受我的授权令牌。 Here is my FeignClient code 这是我的FeignClient代码
@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!!! 谢谢你的建议!
By the way, the following code works using a traditional Feign interface, but I need to use @FeignClient in this case. 顺便说一下,下面的代码使用传统的Feign接口,但在这种情况下我需要使用@FeignClient。
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);
} }
EDIT 编辑
The error code I get when using the above code is: 我使用上面代码时得到的错误代码是:
Exception in thread "main" feign.FeignException: status 400 reading WitGetter#getWitResponse(String,String); 线程“main”中的异常feign.FeignException:status 400读取WitGetter #getWitResponse(String,String); content: { "error" : "Bad auth, check token/params", "code" : "no-auth" } content:{“error”:“Bad auth,check token / params”,“code”:“no-auth”}
When using Feign via Spring Cloud, you can use it as you would define a standard Spring MVC controller. 通过Spring Cloud使用Feign时,您可以像定义标准的Spring MVC控制器一样使用它。
Please check my article here about passing headers with Feign: http://blog.arnoldgalovics.com/2018/02/15/passing-headers-with-spring-cloud-feign/ 请查看我在这里关于使用Feign传递标题的文章: http ://blog.arnoldgalovics.com/2018/02/15/passing-headers-with-spring-cloud-feign/
Quick hint: you can add a @RequestHeader("Authorization") String bearerToken
parameter to the method definition. 快速提示:您可以将@RequestHeader("Authorization") String bearerToken
参数添加到方法定义中。
And then of course call it like client.method(..., "Bearer " + token)
然后当然称之为client.method(..., "Bearer " + token)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.