我正在使用Spring Integration 4.2.4.RELEASE,但似乎它不想将我的授权头添加到SOAP请求中。
以下是相关的spring-integration配置:
<ws:outbound-gateway uri="${soap.ws.url}" message-factory="messageFactory" message-sender="messageSender"/>
<bean id="httpClientFactory" class="com.myorg.http.HttpClientFactoryBean">
<property name="credentials" ref="httpClientCredentials"/>
</bean>
<bean name="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="credentials" ref="httpClientCredentials"/>
<property name="httpClient" ref="httpClientFactory"/>
</bean>
<bean id="httpClientCredentials" class="org.apache.http.auth.UsernamePasswordCredentials">
<constructor-arg value="${username}"/>
<constructor-arg value="${password}"/>
</bean>
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_11"/>
</property>
<property name="messageFactory" ref="myMessageFactory" />
</bean>
<bean id="myMessageFactory" class="com.myorg.soap.CustomMessageFactory" />
这是自定义的messageFactory类
package com.myorg.soap;
import com.sun.xml.messaging.saaj.soap.MessageFactoryImpl;
import com.sun.xml.messaging.saaj.soap.ver1_1.Message1_1Impl;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.io.InputStream;
/**
* Custom Message Factory.
*/
public class CustomMessageFactory extends MessageFactoryImpl {
/**
* Class Constructor.
*
* @return Message1_1Impl
* @throws SOAPException when there is a SOAP error.
*/
@Override
public SOAPMessage createMessage() throws SOAPException {
return new Message1_1Impl();
}
/**
* Create a new message.
*
* @param mimeHeaders headers to add to the message
* @param in input stream to use in the message.
* @return New SOAP 1.1 message.
* @throws IOException when there is an IO error
* @throws SOAPException when there is a SOAP error
*/
@Override
public SOAPMessage createMessage(final MimeHeaders mimeHeaders, final InputStream in)
throws IOException, SOAPException {
MimeHeaders headers = mimeHeaders;
if (headers == null) {
headers = new MimeHeaders();
}
headers.setHeader("Content-Type", "text/xml");
Message1_1Impl msg = new Message1_1Impl(headers, in);
msg.setLazyAttachments(this.lazyAttachments);
return msg;
}
}
这是自定义httpClientFactory类的代码:
package com.myorg.http;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.FactoryBean;
/**
* Provide an HttpClient factory bean, so we can configure the ODE web service client to use TLS1.1 and TLS1.2.
*/
public class HttpClientFactoryBean implements FactoryBean<HttpClient> {
/**
* Default socket timeout will be 15 seconds.
*/
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (15 * 1000);
/**
* Max connections defaults to 5.
*/
private static final int MAX_CONNECTIONS = 5;
/**
* Local storage for credentials.
*/
private Credentials credentials;
@Override
public HttpClient getObject() throws Exception {
HttpClientBuilder builder = HttpClientBuilder.create();
SocketConfig socketConfig = SocketConfig.custom()
.setSoTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS)
.build();
builder.useSystemProperties()
.setMaxConnTotal(MAX_CONNECTIONS)
.setDefaultSocketConfig(socketConfig);
if (credentials != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
builder.setDefaultCredentialsProvider(credentialsProvider);
}
return builder.build();
}
@Override
public Class<?> getObjectType() {
return HttpClient.class;
}
@Override
public boolean isSingleton() {
return true;
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(final Credentials credentials) {
this.credentials = credentials;
}
}
我看到的问题是请求中好像没有设置Authorization头。我漏掉了什么?
HttpClient
,所以Basic Authentication
的HTTP请求头应该是存在的。我建议你从HttpComponentsMessageSender.createConnection()
开始调试代码,试着找出你的credentials
去了哪里。 - Artem Bilan