Web Services WSSE -在soapenv:Header内签署项目

px9o7tmv  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(124)

我想把wsse:security添加到我的soap消息中。这是我的代码:

public Document signSoapMessage(SOAPMessage message) {
    try {
        Document doc = message.getSOAPBody().getOwnerDocument();
        Crypto crypto = CryptoFactory.getInstance(properties); //File

        WSSecHeader secHeader = new WSSecHeader(doc);
        secHeader.insertSecurityHeader();

        InputStream inStream = new FileInputStream(properties.getProperty("org.apache.ws.security.crypto.merlin.keystore.file"));

        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(inStream, properties.getProperty("privatekeypassword").toCharArray());

        String alias = ks.aliases().nextElement();
        X509Certificate cert = (X509Certificate) ks.getCertificate(alias);

        WSSecSignature sign = new WSSecSignature(secHeader);
        sign.setX509Certificate(cert);
        sign.setUserInfo(properties.getProperty("org.apache.ws.security.crypto.merlin.keystore.alias"), properties.getProperty("privatekeypassword"));
        sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE); // Binary Security Token - SecurityTokenReference
        sign.setUseSingleCertificate(true);
        sign.setDigestAlgo(DigestMethod.SHA1);

        //sign.build(crypto);
        Document signedDoc = sign.build(crypto);

        return signedDoc;
    } catch (SOAPException e) {
        e.printStackTrace();
        return null;
    } catch (WSSecurityException e) {
        e.printStackTrace();
        throw new RuntimeException("Error: " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (CertificateException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (KeyStoreException e) {
        e.printStackTrace();
        return null;
    }
}

它适用于soapenv:Body(它添加了wsu:Idxmlns:wsu参数)
但是在soapenv:Header中有一个额外的元素,并且它没有对此元素进行签名。没有wsu:Idxmlns:wsu参数,并且缺少一个ds:Reference。
未签名的soap消息示例:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
    <!-- this element should be signed but is not - NOT WORKING -->
    <something>
    </something>

   </soapenv:Header>
   <!-- this element should be signed and It does. -->
   <soapenv:Body>
    </soapenv:Body>
</soapenv:Envelope>
  • 我将程序中的soap消息与SoapUI项目中的工作soap消息进行比较。*

向Web服务发布消息时出现错误:wsse:InvalidSecurity - Soap Header must be signed。虽然在SoupUI中它确实工作。
因此,我的问题是如何强制WSS4jsoapenv:Header中签署额外的元素?

9jyewag0

9jyewag01#

好了,我已经解决了这个问题。
通常情况下,这段代码应该适用于我的情况。

//strange static method from apache o.O
org.apache.xml.security.Init.init();
List<WSEncryptionPart> wsEncryptionParts = new ArrayList<>();
WSEncryptionPart somethingPart = new WSEncryptionPart("something","somethingNamespace","");
wsEncryptionParts.add(somethingPart);
sign.addReferencesToSign(wsEncryptionParts);

然而,它不起作用。它总是抛出一个异常:
org.apache.wss4j.common.ext.WSSecurityException:在资源绑定“org/apache/xml/security/resource/xmlsecurity”中未找到ID为“noXMLSig”的消息。原始异常为org.apache.wss4j.common.ext.WSSecurityException和消息在资源绑定“org/apache/xml/security/resource/xmlsecurity”中未找到ID为“noEncElement”的消息
我找不到我的soap消息或代码有什么问题的答案。
但是,在调试org.apache.wss4j.dom.message.WSSecSignature一段时间后,我觉得这个类有问题。我决定修改一个方法build(Crypto cr)

public Document build(Crypto cr) throws WSSecurityException {
        LOG.debug("Beginning signing...");
        this.prepare(cr);
        if (this.getParts().isEmpty()) {
            this.getParts().add(WSSecurityUtil.getDefaultEncryptionPart(this.getDocument()));

            // --- Here is my edit - And it works!

            WSEncryptionPart aaa = new WSEncryptionPart("something","somethingNamespace","");
            this.getParts().add(aaa);

            // ----------------------------------

        } else {
            Iterator var2 = this.getParts().iterator();

            label33:
            while(true) {
                while(true) {
                    if (!var2.hasNext()) {
                        break label33;
                    }

                    WSEncryptionPart part = (WSEncryptionPart)var2.next();
                    if (part.getId() == null && "STRTransform".equals(part.getName())) {
                        part.setId(this.strUri);
                    } else if ("KeyInfo".equals(part.getName()) && "http://www.w3.org/2000/09/xmldsig#".equals(part.getNamespace()) && part.getElement() == null) {
                        Element keyInfoElement = this.getKeyInfoElement();
                        part.setElement(keyInfoElement);
                    }
                }
            }
        }

        List<javax.xml.crypto.dsig.Reference> referenceList = this.addReferencesToSign(this.getParts());
        this.computeSignature(referenceList);
        if (this.bstToken != null) {
            this.prependBSTElementToHeader();
        }

        return this.getDocument();
    }

当然,这个解决方案很弱,但至少现在有效。
最新版本中存在此问题:

wss 4j-ws-安全域2.2.2
wss 4j-ws-安全性通用2.2.2

46scxncf

46scxncf2#

我想应该是这样的:

WSSecSignature sign = new WSSecSignature(secHeader);
        sign.getParts().addAll(getEncryptionParts());

getEncryptionParts()是您要添加的WSEncryptionPart列表。如果以这种方式添加,则构建方法能够找到它们,而无需修补框架。

相关问题