【问题标题】:Http outbound channel adapter with basic authentication具有基本身份验证的 Http 出站通道适配器
【发布时间】:2016-01-24 16:44:03
【问题描述】:
我想使用 Spring Boot 和 Integration DSL 将消息作为 HTTP Post 发送到休息服务。有没有人举例说明如何通过(基本)身份验证做到这一点?
其他一切似乎都运行良好。日志显示“org.springframework.web.client.HttpClientErrorException: 401 Unauthorized”。
【问题讨论】:
标签:
spring-integration
spring-dsl
【解决方案1】:
实际上与 Spring Integration 方面的Basic Auth 无关。
这是ClientHttpRequestFactory 的责任。
例如我曾经做过这样的事情:
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory(@Value("username") String username,
@Value("password") String password) {
HttpClient httpClient = HttpClientBuilder.create().
setDefaultCredentialsProvider(getCredentialsProvider(username, password))
.build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
return clientHttpRequestFactory;
}
private CredentialsProvider getCredentialsProvider(final String username, final String password) {
CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(username, password));
return cp;
}
并将这个clientHttpRequestFactory 注入Http.outboundGateway().requestFactory()。
所有其他ClientHttpRequestFactory 可能有其他方法来为他们的请求对象配置Basic Auth。