【问题标题】:Collision between RestTemplate with basic authentication and exchange methodRestTemplate 与基本身份验证和交换方法之间的冲突
【发布时间】:2017-11-14 16:03:43
【问题描述】:

我正在使用 Spring 4.3 和 SpringBoot 1.5 创建一个必须调用外部 REST 服务的组件。此外部服务请求 HTTP 基本身份验证。

我发现SpringBoot提供了一个非常有用的builder来设置RestTemplate属性,比如基本认证,RestTemplateBuilder

要调用外部 REST 服务,我必须使用方法 RestTemplate.exchange,因为返回类型为 List<T>

new RestTemplateBuilder().basicAuthorization(username, password)
                         .build()
                         .exchange("/some/path/with/variables",
                                   HttpMethod.GET,
                                   new HttpEntity<>(new HttpHeaders()),
                                   new ParameterizedTypeReference<List<Integer>>() {},
                                   some, params)

这里的问题是exchange方法的签名请求了一个HttpEntity类型的对象,它基本上是一个HttpHeaders的容器。

问题是,作为输入参数传递给exchange 方法的HttpEntity 对象是否会使用RestTemplateBuilder.basicAuthorization 方法订阅Basic Auth 标头集?

非常感谢。

【问题讨论】:

  • 您应该详细说明您的问题。它本质上没有正确的语法形成。
  • 问题已修改。现在好点了吗?
  • 更好,是的。但现在我的评论:你试过了吗?
  • 不。我处于分析阶段。我在问是否有人已经尝试过并且可以给我帮助。似乎合法。

标签: java spring spring-boot basic-authentication resttemplate


【解决方案1】:

在过去的几天里,我做了一些实验。传递给exchange 方法的HttpEntity 实例订阅与RestTemplateBuilder 设置的基本身份验证相关的信息。两组标题合并为一组。

不错,春天;)

【讨论】:

    猜你喜欢
    • 2017-11-24
    • 2011-08-13
    • 1970-01-01
    • 2012-09-08
    • 2012-12-06
    • 2017-12-22
    • 2014-07-02
    • 2016-08-24
    • 2014-03-26
    相关资源
    最近更新 更多