我找到了这个序列来设置基本身份验证here:
HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope("www.domain.com", 443, "realm"),
new UsernamePasswordCredentials("username", "password") );
使用弹簧配置如何实现这一目标? 背后的原因是,我需要为spring-integration HttpOutboundGateway启用身份验证。 我在这个主题上找到的唯一信息是this
答案 0 :(得分: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;
}
}
答案 1 :(得分:1)
创建一个自己的FactoryBean
类,它返回具有您喜欢的配置的HttpClient实例。