简体   繁体   中英

How to add custom security header to spring soap client

I'm working on a spring boot app with soap client trying to connect to soap web service secured using header so that I try to use an interceptor based on Wss4jSecurityInterceptor this my client configuration

when I called the Soap WS I get this error :

org.springframework.ws.soap.client.SoapFaultClientException: An error was discovered processing the <wsse:Security> header
    at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:38) ~[spring-ws-core-2.4.0.RELEASE.jar:2.4.0.RELEASE]
    at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:830) ~[spring-ws-core-2.4.0.RELEASE.jar:2.4.0.RELEASE]
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:624) ~[spring-ws-core-2.4.0.RELEASE.jar:2.4.0.RELEASE]

In my config class :

@Bean
    public Wss4jSecurityInterceptor securityInterceptor() {
        Wss4jSecurityInterceptor security = new Wss4jSecurityInterceptor();
        // what should I add here 
        return security;
    }
    @Bean
    public SOAPConnector soapConnector(Jaxb2Marshaller marshaller)throws Exception {
        SOAPConnector client = new SOAPConnector();
        client.setDefaultUri(defaultUri);
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        client.setInterceptors(new ClientInterceptor[]{ securityInterceptor() });
        client.setMessageSender(httpComponentsMessageSender());
        return client;
    }

And this my client :

@Component
public class SOAPConnector extends WebServiceGatewaySupport {

    public Object callWebService(String url, Object request){
        return getWebServiceTemplate().marshalSendAndReceive(url, request);
    }
}

This's the security header that I want to add to my client :

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="X509-FD1EC894572B22912315605098864444600">MIIC1zCCAkACAiWNMA0GCSqGSIb.....=
            </wsse:BinarySecurityToken>
            <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SIG-3068">
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="soap"/>
                     </ds:CanonicalizationMethod>
                <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
                <ds:Reference URI="#id-3067">
                    <ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"><ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList=""/>
                    </ds:Transform></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue>C7JMsbXSGGOrlvGi+fIeoViI3aI=</ds:DigestValue>
                </ds:Reference>
            </ds:SignedInfo>
            <ds:SignatureValue>KDNG2Og3FcDNMvgyii/U....==</ds:SignatureValue>

            <ds:KeyInfo Id="KI-FD1EC894572B22912315605098864444601">
                <wsse:SecurityTokenReference wsu:Id="STR-FD1EC894572B22912315605098864444602">
                    <wsse:Reference URI="#X509-FD1EC894572B22912315605098864444600" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
                </wsse:SecurityTokenReference>
            </ds:KeyInfo>

        </ds:Signature>

</wsse:Security>

You may define your whole custom security header as a fixed string and override WebServiceMessageCallback class doWithMessage(WebServiceMessage message) method for setting your soap request's header as you wish;

public class SoapConnector extends WebServiceGatewaySupport {

    private static final Logger LOGGER = LoggerFactory.getLogger(SoapConnector.class);

    public static final String YOUR_CUSTOM_HEADER = "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">\n" +
        "\t<wsse:BinarySecurityToken EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\" ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3\" wsu:Id=\"X509-FD1EC894572B22912315605098864444600\">MIIC1zCCAkACAiWNMA0GCSqGSIb.....=\n" +
        "\t</wsse:BinarySecurityToken>\n" +
        "\t<ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" Id=\"SIG-3068\">\n" +
        "\t\t<ds:SignedInfo>\n" +
        "\t\t\t<ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\">\n" +
        "\t\t\t\t<ec:InclusiveNamespaces xmlns:ec=\"http://www.w3.org/2001/10/xml-exc-c14n#\" PrefixList=\"soap\"/>\n" +
        "\t\t\t</ds:CanonicalizationMethod>\n" +
        "\t\t\t<ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>\n" +
        "\t\t\t<ds:Reference URI=\"#id-3067\">\n" +
        "\t\t\t\t<ds:Transforms>\n" +
        "\t\t\t\t\t<ds:Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\">\n" +
        "\t\t\t\t\t\t<ec:InclusiveNamespaces xmlns:ec=\"http://www.w3.org/2001/10/xml-exc-c14n#\" PrefixList=\"\"/>\n" +
        "\t\t\t\t\t</ds:Transform>\n" +
        "\t\t\t\t</ds:Transforms>\n" +
        "\t\t\t\t<ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\n" +
        "\t\t\t\t<ds:DigestValue>C7JMsbXSGGOrlvGi+fIeoViI3aI=</ds:DigestValue>\n" +
        "\t\t\t</ds:Reference>\n" +
        "\t\t</ds:SignedInfo>\n" +
        "\t\t<ds:SignatureValue>KDNG2Og3FcDNMvgyii/U....==</ds:SignatureValue>\n" +
        "\t\t<ds:KeyInfo Id=\"KI-FD1EC894572B22912315605098864444601\">\n" +
        "\t\t\t<wsse:SecurityTokenReference wsu:Id=\"STR-FD1EC894572B22912315605098864444602\">\n" +
        "\t\t\t\t<wsse:Reference URI=\"#X509-FD1EC894572B22912315605098864444600\" ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3\"/>\n" +
        "\t\t\t</wsse:SecurityTokenReference>\n" +
        "\t\t</ds:KeyInfo>\n" +
        "\t</ds:Signature>\n" +
        "</wsse:Security>";

    public Object callWebService(String url, Object request) {

        return getWebServiceTemplate().marshalSendAndReceive(url, request, new WebServiceMessageCallback() {
            @Override
            public void doWithMessage(WebServiceMessage webServiceMessage) {
                try {
                    SaajSoapMessage saajSoapMessage = (SaajSoapMessage) webServiceMessage;
                    SoapHeader soapHeader = saajSoapMessage.getSoapHeader();

                    StringSource headerSource = new StringSource(YOUR_CUSTOM_HEADER);
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.transform(headerSource, soapHeader.getResult());

                } catch (Exception e) {
                    e.printStackTrace();
                    LOGGER.error(e.toString(), e);
                }
            }
        });
    }
}
@Configuration

public class Config {

@Value("${client.default-uri}")
private String defaultUri;

@Value("${client.user.name}")
private String userName;

@Value("${client.user.password}")
private String userPassword;

@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("com.example.eppmsoapclient");
    return marshaller;
}


@Bean
public Wss4jSecurityInterceptor securityInterceptor() {
    Wss4jSecurityInterceptor security = new Wss4jSecurityInterceptor();
    security.setSecurementActions("UsernameToken");
    security.setSecurementUsername(userName);
    security.setSecurementPassword(userPassword);
    security.setSecurementPasswordType("PasswordText");
    return security;
}

@Bean
public SOAPClient soapClient(Jaxb2Marshaller marshaller) {
    SOAPClient client = new SOAPClient();
    client.setDefaultUri(defaultUri);
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    client.setInterceptors(new ClientInterceptor[]{ securityInterceptor() });
    return client;
}

}

public class SOAPClient extends WebServiceGatewaySupport {

 public Response doExecute(Request request) {
    
     Response response = (Response) getWebServiceTemplate()
              .marshalSendAndReeive(request);
    
     return response;
    
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM