简体   繁体   English

如何使用Spring Boot和@FeignClient发送Bearer授权令牌

[英]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.

相关问题 无法使用 Springfox 发送授权承载令牌 - Cannot send Authorization Bearer Token using Springfox 使用最新的 OpenAPI / Swagger UI 的 Bearer JWT 令牌授权 (401) 问题 | 弹簧靴 - Issue with Authorization (401) with Bearer JWT token using latest OpenAPI / Swagger UI | Spring Boot Spring Boot Keycloak-如何验证承载令牌? - Spring Boot Keycloak - How to verify Bearer token? 在 Spring 启动微服务中使用 FeignClient 出现错误 302 - Error 302 Using FeignClient in Spring Boot Microservices Spring OAuth2-在授权中传递令牌:承载 - Spring OAuth2- Passing token in Authorization: Bearer 如何使用 jwt 公钥在 Spring Boot 中验证承载访问令牌 - How to validate bearer access token in spring boot using jwt public key FeignClient在春季靴子2 - FeignClient in spring boot 2 如何验证“授权:承载<token> ”与骡子3.8?</token> - how to validate “Authorization: Bearer <token>” with Mule 3.8? 如何在所有请求中传递授权令牌 header - Spring 启动 java - How to pass the authorization token in all request header - Spring boot java 添加授权Header承载认证到Spring启动Controller - Add Authorization Header Bearer Authentication to Spring Boot Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM