2010-11-16 15 views
3

Je trouve cette séquence pour configurer l'authentification de base here:Comment un HttpClient peut-il être configuré pour l'authentification de base?

HttpClient client = new HttpClient(); 

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

Comment cela peut-il être achived en utilisant la configuration du printemps? La raison derrière est, je dois activer l'authentification pour un HttpOutboundGateway d'intégration de printemps. La seule information que je trouve sur ce sujet est this

  • La question est: comment faire le printemps configuration?
  • Et puis comment puis-je injecter le HttpClient dans l'intégration de printemps?
+0

HttpClient 3? Pourquoi ne pas passer à HttpClient 4? –

+0

J'ai décidé d'utiliser l'intégration à ressort 1.0.3 puisque 2.x n'a pas été publié. – stacker

Répondre

5

Eh bien, il pourrait être quelque chose comme ça: (note, rien est testé - c'est juste une série de pensées au hasard :))

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

Merci très beau projet – stacker

1

Créez une classe FactoryBean qui renvoie les instances HttpClient avec la configuration que vous souhaitez.