在 Spring 3.1 中使用基本身份验证的 RestTemplate [英] RestTemplate with Basic Auth in Spring 3.1

查看:30
本文介绍了在 Spring 3.1 中使用基本身份验证的 RestTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Spring 3.0 中使用带有 xml 配置的 RestTemplate,它运行得非常好.

We were using RestTemplate with xml configuration in Spring 3.0 and it was working perfectly fine.

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

<bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory"> 
    <constructor-arg ref="httpClient"/> 
</bean>  

  <bean id="restTemplate" name="restTemplate" class="org.springframework.web.client.RestTemplate" autowire-candidate="true">
        <constructor-arg ref="httpClientFactory" />

    <property name="messageConverters"> 
        <list> 
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            </bean> 
            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> 
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
        </list> 
    </property>
  </bean>

但是,当我们尝试迁移到 Spring 3.1 版本时,不推荐使用 CommonsClientHttpRequestFactory 类,并且不再使用 commons HttpClient.

But, when we are trying to migrate to Spring 3.1 release CommonsClientHttpRequestFactory class is deprecated and also commons HttpClient is not used anymore.

我试图使用 HttpComponentsClientHttpRequestFactory 类和 Apache HttpClient 设置类似的配置,但不知道如何设置凭据提供程序.

I was trying to set up similar config using HttpComponentsClientHttpRequestFactory class and Apache HttpClient, but not getting how can I set the Credential Provider.

我们想要具有基本身份验证的 httpclient.有没有人做过这个或任何指针都会有很大帮助.提前致谢.

We want the httpclient with basic auth. Has anybody done this or any pointers would be great help. Thanks in Advance.

推荐答案

我终于让这个工作了.不确定它是否最佳,因为这是第一个对我有用的解决方案.

I was able to finally get this working. Not sure whether it is optimal, as this the first solution which worked for me.

`

<!-- Credentials provider needed by apache default http client -->
<bean id="credentialProvider" class="org.apache.http.impl.client.BasicCredentialsProvider" />

<!-- Used to invoke a method in BasicCredentialsProvider.  This has to be done this way as BasicCredentialsProvider does not take 
provider and credentials in constructor or setter method.  It has to set by invoking setCredentials() method and passing two arguments -->  
<bean id="methodInvoke" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject"><ref local="credentialProvider" /> </property>
    <property name="targetMethod" value="setCredentials"> </property>
    <property name="arguments"  >
        <list>
            <ref local="authScope" />
            <ref local="credentials" />
        </list>
    </property>
</bean>

<!-- Authorization scope for accessing restful service.  Since we want this template to be used for everything, we are setting up it with defaults -->
<bean id="authScope" class="org.apache.http.auth.AuthScope">
    <constructor-arg name="host"><null /></constructor-arg>
    <constructor-arg><value>-1</value> </constructor-arg>
    <constructor-arg><null /></constructor-arg>
    <constructor-arg><null /></constructor-arg>
</bean>

<!-- Username and Password Credentials to access restful service -->
<bean id="credentials" class="org.apache.http.auth.UsernamePasswordCredentials">
    <constructor-arg name="userName"><value>xxx</value></constructor-arg>
    <constructor-arg name="password"><value>xxx</value></constructor-arg>
</bean>

<!-- Client factory which uses Apache HttpClient implementation.  Note that it DO NOT use apache commons httpclient -->
<bean id="httpClientFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"> 
    <constructor-arg ref="httpClient"/> 
</bean>

<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient">
    <property name="credentialsProvider" ref="credentialProvider"/>
</bean>

<!-- Rest template for Spring 3.1. Used HttpClientFactory to make request -->
  <bean id="restTemplate" name="restTemplate" class="org.springframework.web.client.RestTemplate" autowire-candidate="true">
        <constructor-arg ref="httpClientFactory" />

    <property name="messageConverters"> 
        <list> 
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            </bean> 
            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> 
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
        </list> 
    </property>
  </bean>`

这篇关于在 Spring 3.1 中使用基本身份验证的 RestTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆