如何配置 HttpClient 进行基本身份验证?

发布于 2024-10-02 08:02:51 字数 673 浏览 3 评论 0原文

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

HttpClient client = new HttpClient();

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

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

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

I found this sequence to set up basic authentication here:

HttpClient client = new HttpClient();

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

How can this be achived by using spring configuration?
The reason behind is, I need to enable authentication for a spring-integration HttpOutboundGateway.
The only piece of information I found on this topic is this

  • The question is: How to do the spring-configuration?
  • And second how can I inject the HttpClient into spring-integration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2024-10-09 08:02:51

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

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

Well, it could be something like that: (note, nothing is tested - thats just a series of random thoughts :) )

<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;
    }
}
糖粟与秋泊 2024-10-09 08:02:51

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

Create a FactoryBean class of your own which returns HttpClient instances with the configuration you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文