使用spring-integration,我想用基本身份验证调用出站网关。 我有这样的事情:
<int-http:inbound-gateway id="versionRequestGateway"
supported-methods="POST" request-channel="requestVersionChannel"
reply-channel="requestTransformerVersionChannel"
path="/consultersite" reply-timeout="10000" request-payload-type="java.lang.String">
</int-http:inbound-gateway>
<int-http:outbound-gateway order="1" request-channel="requestVersionChannel"
url-expression="@urlExpressionGateway.getUrlFor(payload) + '/consultersite'"
reply-channel="responseVersionChannel"
http-method="POST"
expected-response-type="java.lang.String" >
</int-http:outbound-gateway>
出站网关的URL是动态的。
我决定在出站网关上使用rest-template属性,使用:
<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="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" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory"/>
</bean>
在加载spring应用程序上下文后,在ApplicationListener中注入UsernamePasswordCredentials时,它可以正常工作。
HttpClient client = ctx.getBean("httpClient", HttpClient.class);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(AuthScope.ANY, credentials);
但根据出站网关的网址,用户名和密码不同。 如何根据url outbound-gateway使用好用户名/密码?
有必要实现我自己的BasicSecureSimpleClientHttpRequestFactory扩展SimpleClientHttpRequestFactory来根据连接的URL映射信息Credentials。希望一个实现Spring将有一天可用... 感谢。
答案 0 :(得分:1)
不要键入任何可以结合使用Spring WS HttpComponentsMessageSender
和Spring WEB HttpComponentsClientHttpRequestFactory
的Java代码:
<bean id="httpComponentsMessageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="credentials">
<bean class="org.apache.commons.httpclient.UsernamePasswordCredentials">
<constructor-arg value="userName"/>
<constructor-arg value="password"/>
</bean>
</property>
</bean>
<bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<property name="httpClient" value="#{httpComponentsMessageSender.httpClient}"/>
</bean>
<int-http:outbound-gateway url-expression="@urlExpressionGateway.getUrlFor(payload) + '/consultersite'"
request-factory="clientHttpRequestFactory"/>
我可以相信,我的答案可能不适合你的情况。 但是我希望它能有所帮助。
也许需要在运行时实现自己的HttpComponentsClientHttpRequestFactory#createRequest
进行身份验证并执行此操作:
method.addRequestHeader(new Header(WWW_AUTH_RESP, authstring, true));
查看HttpMethodDirector#authenticateHost
答案 1 :(得分:0)
你应该使用org.apache.http.auth.UsernamePasswordCredentials实现而不是org.apache.commons.httpclient.UsernamePasswordCredentials
<beans:bean id="httpComponentsMessageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<beans:property name="credentials">
<beans:bean class="org.apache.http.auth.UsernamePasswordCredentials">
<beans:constructor-arg value="user"/>
<beans:constructor-arg value="password"/>
</beans:bean>
</beans:property>
</beans:bean>