使用 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>
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);
但是根据 outbound-gateway 的 url,用户名和密码是不同的。如何根据 url outbound-gateway 使用好的用户名/密码?
有必要实现我自己的 BasicSecureSimpleClientHttpRequestFactory 扩展 SimpleClientHttpRequestFactory 以根据连接的 URL 映射信息凭据。希望有朝一日可以实现 Spring ... 谢谢。