如何将身份验证标头添加到 SOAP 请求

问题描述 投票:0回答:1

我正在使用 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" />

这是自定义消息工厂类

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;
    }
}

我看到的问题是授权标头似乎没有在请求中设置。我错过了什么?

java spring soap spring-integration
1个回答
0
投票

我遵循 spring 消费 web 服务指南“https://spring.io/guides/gs/consuming-web-service/#initial”也有一个例子。但是没有如何添加基本授权。所以你可以这样使用

public class WebServiceMessageSenderWithAuth extends HttpUrlConnectionMessageSender {
    
    private String username;

    private String password;

    @Override
    protected void prepareConnection(HttpURLConnection connection) throws IOException {
        BASE64Encoder enc = new sun.misc.BASE64Encoder();
        String userpassword = username+":"+password;
        String encodedAuthorization = enc.encode( userpassword.getBytes() );
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);

        super.prepareConnection(connection);
    }
}

public YourClient yourClient(Jaxb2Marshaller marshaller){
    YourClient client = new YourClient();
    
    WebServiceTemplate template = client.getWebServiceTemplate();
    template.setMessageSender(new WebServiceMessageSenderWithAuth());
    
    client.setDefaultUri("http://examplesap.com/xx/WebService/soap1.1x");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    return client;
}

然后打电话..

return (YourResponseModel) getWebServiceTemplate()
                .marshalSendAndReceive(url, request,
                        new SoapActionCallback(
                        "http://examplesap.com/xx/WebService/soap1.1x"));
© www.soinside.com 2019 - 2024. All rights reserved.