2

使用 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 ... 谢谢。

4

2 回答 2

1

不要输入任何 Java 代码,您可以结合使用 Spring WSHttpComponentsMessageSender和 Spring WEB HttpComponentsClientHttpRequestFactory

<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

于 2013-10-29T10:19:02.097 回答
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>
于 2018-03-08T08:42:56.433 回答