3

我发现这个序列在这里设置基本身份验证:

HttpClient client = new HttpClient();

client.getState().setCredentials(
   new AuthScope("www.domain.com", 443, "realm"),
   new UsernamePasswordCredentials("username", "password") );

如何通过使用弹簧配置来实现这一点?背后的原因是,我需要为 spring-integration HttpOutboundGateway 启用身份验证。我在这个主题上找到的唯一一条信息

  • 问题是:如何进行弹簧配置?
  • 其次,如何将 HttpClient 注入到 spring-integration 中?
4

2 回答 2

5

好吧,可能是这样的:(注意,没有经过任何测试 - 这只是一系列随机想法:))

<bean id="httpOutbound" class="org.springframework.integration.http.HttpOutboundEndpoint" >
    <property name="requestExecutor" ref="executor" />
</bean>

<bean id="executor" class="org.springframework.integration.http.CommonsHttpRequestExecutor">
    <property name="httpClient">
        <bean factory-bean="clientFactory" factory-method="getHttpClient">
    </property>
</bean>

<bean id="clientFactory" class="bla.bla.bla.HttpClientFactoryBean">
    <constructor-arg ref="httpClient" />
    <constructor-arg ref="credentials" />
</bean>

<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <constructor-arg ref="httpClientParams" />
</bean>

<bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams">
    <property name="authenticationPreemptive" value="true" />
    <property name="connectionManagerClass" value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager" />
</bean>

<bean id="credentials" class="org.apache.commons.httpclient.UsernamePasswordCredentials">
    <constructor-arg value="user" />
    <constructor-arg value="password" />
</bean>


public class HttpClientFactoryBean{
    private HttpClient httpClient;
    public HttpClientFactoryBean(HttpClient httpClient, Credentials credentials){
        this.httpClient = httpClient;
        httpClient.getState().setCredentials(AuthScope.ANY, credentials);
    }

    public HttpClient getHttpClient(){
        return httpClient;
    }
}
于 2010-11-16T15:19:56.520 回答
1

创建一个FactoryBean您自己的类,它返回具有您喜欢的配置的 HttpClient 实例。

于 2010-11-16T14:59:36.780 回答