Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向SOAP请求添加身份验证头_Java_Spring_Soap_Spring Integration - Fatal编程技术网

Java 如何向SOAP请求添加身份验证头

Java 如何向SOAP请求添加身份验证头,java,spring,soap,spring-integration,Java,Spring,Soap,Spring Integration,我使用的是SpringIntegration4.2.4.RELEASE,它似乎不想将我的授权头添加到SOAP请求中 以下是相关的spring集成配置: <ws:outbound-gateway uri="${soap.ws.url}" message-factory="messageFactory" message-sender="messageSender"/> <bean id="httpClientFactory" class="com.myorg.http.HttpC

我使用的是SpringIntegration4.2.4.RELEASE,它似乎不想将我的授权头添加到SOAP请求中

以下是相关的spring集成配置:

<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" />
下面是定制的httpClientFactory类

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;
    }
}
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;
    }
}
package com.myorg.http;
导入org.apache.http.auth.AuthScope;
导入org.apache.http.auth.Credentials;
导入org.apache.http.client.CredentialsProvider;
导入org.apache.http.client.HttpClient;
导入org.apache.http.config.SocketConfig;
导入org.apache.http.impl.client.BasicCredentialsProvider;
导入org.apache.http.impl.client.HttpClientBuilder;
导入org.springframework.beans.factory.FactoryBean;
/**
*提供一个HttpClient工厂bean,这样我们就可以将ODE web服务客户端配置为使用TLS1.1和TLS1.2。
*/
公共类HttpClientFactoryBean实现FactoryBean{
/**
*默认套接字超时为15秒。
*/
私有静态final int DEFAULT\u READ\u TIMEOUT\u毫秒=(15*1000);
/**
*最大连接数默认为5。
*/
专用静态最终int MAX_连接=5;
/**
*凭据的本地存储。
*/
私人证书;
@凌驾
公共HttpClient getObject()引发异常{
HttpClientBuilder=HttpClientBuilder.create();
SocketConfig SocketConfig=SocketConfig.custom()
.setSoTimeout(默认读取超时毫秒)
.build();
builder.useSystemProperties()
.SetMaxConntTotal(最大连接数)
.setDefaultSocketConfig(socketConfig);
如果(凭据!=null){
CredentialsProvider CredentialsProvider=新的BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,credentials);
builder.setDefaultCredentialsProvider(credentialsProvider);
}
返回builder.build();
}
@凌驾
公共类getObjectType(){
返回HttpClient.class;
}
@凌驾
公共布尔isSingleton(){
返回true;
}
公共凭据getCredentials(){
返回凭证;
}
公共无效设置凭据(最终凭据){
this.credentials=凭证;
}
}

我看到的问题是,似乎没有在请求上设置授权头。我遗漏了什么?

我在您的代码中没有看到任何明显的内容。看起来您完全委托给了
HttpClient
,因此
Basic Authentication
HTTP请求头必须在那里。我建议您从
HttpComponentsMessageSender.createConnection()
调试代码,并尝试找出您的
凭证的去向。