具有基本身份验证的Http出站通道适配器

时间:2016-01-24 16:44:03

标签: spring-integration spring-dsl

我想使用Spring Boot和Integration DSL将消息作为HTTP Post发送到休息服务。有没有人有一个如何用(基本)认证做到这一点的例子?

其他一切似乎都运转良好。日志显示" org.springframework.web.client.HttpClientErrorException:401 Unauthorized"。

1 个答案:

答案 0 :(得分:0)

实际上,与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