Java 使用wss4j和jax ws追加安全标头时出现文档错误

Java 使用wss4j和jax ws追加安全标头时出现文档错误,java,web-services,soap,jax-ws,soap-client,Java,Web Services,Soap,Jax Ws,Soap Client,我正在实现一个SOAP客户端,它需要用一个安全头对每个请求进行签名。 为此,我使用wss4j来处理我的消息,并将SAML签名添加到SOAP文档的安全头中 @Override public boolean handleMessage(SOAPMessageContext context) { if (!(Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) { return t

我正在实现一个SOAP客户端,它需要用一个安全头对每个请求进行签名。 为此,我使用wss4j来处理我的消息,并将SAML签名添加到SOAP文档的安全头中

@Override
    public boolean handleMessage(SOAPMessageContext context) {
        if (!(Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
            return true;
        }

        Document doc = context.getMessage().getSOAPPart();

        URL serviceUrl = null;
        try {
            // retrieve the SAML token using the STSClient
            serviceUrl = new URL("https://services-sim.socialsecurity.be/SecurityTokenService/v1");
            Element assertion = (Element) doc.importNode(stsClient.getSAMLToken(serviceUrl.getHost()), true);

            Crypto crypto = CryptoFactory.getInstance("credentials/crypto.properties");

            // insert security header
            WSSecHeader secHeader = new WSSecHeader();
            secHeader.setMustUnderstand(true);
            secHeader.insertSecurityHeader(doc);
但是我得到了一个错误:org.w3c.dom.DOMException:error\u DOCUMENT\u ERR:当我调用该方法时,在不同的文档中使用了一个节点

secHeader.insertSecurityHeader(doc);
完整错误消息:

Exception in thread "main" javax.xml.ws.WebServiceException: java.lang.RuntimeException: Cannot retrieve SAML Token from STS
    at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:120)
    at com.sun.xml.ws.handler.HandlerTube.processRequest(HandlerTube.java:97)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1106)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1020)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:989)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:847)
    at com.sun.xml.ws.client.Stub.process(Stub.java:433)
    at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:161)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:62)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:131)
    at com.sun.proxy.$Proxy37.registerPresences(Unknown Source)
    at be.socialsecurity.presenceregistration.v1.PresenceRegistrationService.main(PresenceRegistrationService.java:108)
Caused by: java.lang.RuntimeException: Cannot retrieve SAML Token from STS
    at be.socialsecurity.platformintegrationconsumertest.impl.STSClient.getSAMLToken(STSClient.java:58)
    at be.socialsecurity.platformintegrationconsumertest.impl.SAMLHoKSecurityHandler.handleMessage(SAMLHoKSecurityHandler.java:68)
    at be.socialsecurity.platformintegrationconsumertest.impl.SAMLHoKSecurityHandler.handleMessage(SAMLHoKSecurityHandler.java:46)
    at com.sun.xml.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:267)
    at com.sun.xml.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:108)
    at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:112)
    ... 12 more
Caused by: java.lang.RuntimeException: org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
    at be.socialsecurity.platformintegrationconsumertest.impl.X509SecurityHandler.addSecurityHeaders(X509SecurityHandler.java:118)
    at be.socialsecurity.platformintegrationconsumertest.impl.STSClient.requestSecurityToken(STSClient.java:99)
    at be.socialsecurity.platformintegrationconsumertest.impl.STSClient.getSAMLToken(STSClient.java:49)
    ... 17 more
Caused by: org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
    at java.xml/com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(ParentNode.java:355)
    at java.xml/com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(ParentNode.java:286)
    at java.xml/com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(NodeImpl.java:230)
    at org.apache.ws.security.util.WSSecurityUtil.prependChildElement(WSSecurityUtil.java:686)
    at org.apache.ws.security.util.WSSecurityUtil.findWsseSecurityHeaderBlock(WSSecurityUtil.java:780)
    at org.apache.ws.security.message.WSSecHeader.insertSecurityHeader(WSSecHeader.java:138)
    at be.socialsecurity.platformintegrationconsumertest.impl.X509SecurityHandler.addSecurityHeaders(X509SecurityHandler.java:75)
    ... 19 more
Command execution failed.
这是我在pom.xml中的依赖项:

<dependencies>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-tools</artifactId>
        <version>2.3.2</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>javax.xml.ws</groupId>
        <artifactId>jaxws-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ws.security</groupId>
        <artifactId>wss4j</artifactId>
        <version>1.6.19</version>
    </dependency>
    <dependency>
        <groupId>org.apache.wss4j</groupId>
        <artifactId>wss4j-ws-security-dom</artifactId>
        <version>2.3.0</version>
    </dependency>
</dependencies>

com.sun.xml.ws
jaxws工具
2.3.2
聚甲醛
javax.xml.ws
JAXWSAPI
2.3.1
org.apache.ws.security
wss4j
1.6.19
org.apache.wss4j
wss4j ws-security dom
2.3.0