如何向Spring Soap客户端添加自定义安全标头

时间:2019-06-14 11:13:00

标签: java spring spring-boot http spring-ws

我正在使用一个带有Soap客户端的Spring Boot应用程序,尝试连接到使用标头保护的Soap Web Service,以便尝试使用基于 Wss4jSecurityInterceptor 此客户端配置

的拦截器>

当我调用Soap WS时,出现此错误:

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]

在我的配置类中:

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

这是我的客户:

@Component
public class SOAPConnector extends WebServiceGatewaySupport {

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

这是我要添加到客户端的安全标头:

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

2 个答案:

答案 0 :(得分:0)

您可以将整个自定义安全标头定义为固定字符串,并覆盖WebServiceMessageCallback类doWithMessage(WebServiceMessage message)方法,以根据需要设置肥皂请求的标头;

using

答案 1 :(得分:0)

@Configuration

公共类配置{

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

}

公共类 SOAPClient 扩展了 WebServiceGatewaySupport {

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

}