【问题标题】:How can a HttpClient be configured for basic - authentification?如何为基本身份验证配置 HttpClient?
【发布时间】:2011-05-10 20:19:30
【问题描述】:

我找到了这个序列来设置基本身份验证here

HttpClient client = new HttpClient();

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

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

  • 问题是:怎么做spring-configuration?
  • 其次,如何将 HttpClient 注入到 spring-integration 中?

【问题讨论】:

  • HttpClient 3?为什么不升级到 HttpClient 4?
  • 我决定使用 spring-integration 1.0.3,因为 2.x 还没有发布。

标签: java spring spring-integration


【解决方案1】:

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

<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;
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-21
      • 2023-04-08
      • 1970-01-01
      • 2021-07-19
      • 2021-10-18
      • 2018-08-12
      • 2023-03-05
      • 2015-07-03
      相关资源
      最近更新 更多