我正在使用 Spring Boot 编写一个与 HTTP Rest 服务器交互的应用程序。我连接的服务器之一(Wit.ai)使用承载者授权令牌。产生成功响应的卷曲请求如下所示:
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 文档对令牌做了以下说明,
Wit.ai 使用 OAuth2 作为授权层。因此,每个 API 请求都必须包含带有令牌的 Authorize HTTP 标头 访问令牌是特定于应用程序的。
我正在尝试使用 @FeignClient 在 Spring Boot 应用程序中向此端点发送 GET 请求。但是,端点似乎不接受我的授权令牌。 这是我的 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);
}
传递此类授权令牌的正确方法是什么?我尝试了其他一些方法,但没有成功。感谢您的任何建议!
顺便说一下,下面的代码使用传统的 Feign 接口工作,但在这种情况下我需要使用@FeignClient。
public interface WitGetter {
@Headers("Authorization: Bearer XXXXXXXXXXXXX")
@RequestLine("GET /message?q={text}")
WitResponse getWitResponse(@Param("text") String text);
}
(下面的代码位于单独的配置文件中)
@Bean
public WitGetter defaultWitGetter(@Value("https://api.wit.ai") final String witUrl){
return Feign.builder().decoder(new GsonDecoder()).target(WitGetter.class, witUrl);
}
编辑
使用上述代码时得到的错误代码是:
线程“main”中出现异常 feign.FeignException:状态 400 读取 WitGetter#getWitResponse(String,String);内容: { “错误”:“错误的身份验证,检查令牌/参数”, “代码”:“无身份验证” }
通过 Spring Cloud 使用 Feign 时,您可以像定义标准 Spring MVC 控制器一样使用它。
请查看我关于使用 Feign 传递标头的文章: https://arnoldgalovics.com/passing-headers-with-spring-cloud-feign/
快速提示:您可以在方法定义中添加
@RequestHeader("Authorization") String bearerToken
参数。
然后当然可以这样称呼它
client.method(..., "Bearer " + token)
package com.example.demo.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entities.CustomerDto;
import com.example.demo.feign.CustomerRestClient;
import jakarta.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("customers")
@CrossOrigin("*")
public class ClientRestController {
@Autowired
private CustomerRestClient customerRestClient;
@GetMapping("/all")
public List<CustomerDto> getAllCustomers(HttpServletRequest request) {
String authorizationHeader = request.getHeader("Authorization");
return customerRestClient.listCustomes(authorizationHeader);
}
}
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import com.example.demo.entities.CustomerDto;
import feign.Headers;
@FeignClient(url = "http://localhost:8085/customers",value ="customer-rest-client")
@Headers("Authorization: {token}")
public interface CustomerRestClient {
@GetMapping("/all")
public List<CustomerDto> listCustomes(@RequestHeader("Authorization") String token);
}