下面是我需要用Java生成的XML签名keyinfo。

<ds:KeyInfo Id="idhere">
<wsse:SecurityTokenReference wsse11:TokenType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" wsu:Id="idhere" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd">
<wsse:Reference URI="#X509" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1"/>
                    </wsse:SecurityTokenReference>
                </ds:KeyInfo>


以下是我到目前为止在Java中取得的成就。如何将安全令牌引用添加到密钥信息?

KeyInfoFactory kif = fac.getKeyInfoFactory();

KeyInfo ki = kif.newKeyInfo(Collections.singletonList(Whatgoeshere?));


XMLSignature signature = fac.newXMLSignature(si, ki,null,"id-2FC89B275743456788xtdcfyvg9014",null);


需要任何其他信息,请随时询问。谢谢!

最佳答案

我今天为此一直在挣扎,但找到了解决方案。为了生成安全令牌参考,我使用了javax.xml.crypto之外的其他库,即org.apache.ws.security。想法是使用所需的keyinfo生成安全令牌引用,然后使用keyinfofactory创建keyinfo对象。

参见示例:

   import org.apache.ws.security.message.token.DOMX509Data;
   import org.apache.ws.security.message.token.DOMX509IssuerSerial;
   import org.apache.ws.security.message.token.SecurityTokenReference;

   import javax.xml.crypto.XMLStructure;
   import javax.xml.crypto.dom.DOMStructure;
   import javax.xml.crypto.dsig.keyinfo.*;

   SecurityTokenReference secRef = new SecurityTokenReference(doc);
               secRef.addWSSENamespace();

   String issuer = "issuer information";
   BigInteger serialNumber = new BigInteger("issuer serial number");
   DOMX509IssuerSerial domIssuerSerial = new DOMX509IssuerSerial(doc, issuer, serialNumber);
   DOMX509Data domX509Data = new DOMX509Data(doc, domIssuerSerial);
   secRef.setX509Data(domX509Data);

   XMLStructure structure = new DOMStructure(secRef.getElement());
   KeyInfo keyInfo = keyInfoFac.newKeyInfo(java.util.Collections.singletonList(structure), "key-info");


这将生成如下的keyinfo:

<ds:KeyInfo Id="key-info">
    <wsse:SecurityTokenReference>
        <ds:X509Data>
            <ds:X509IssuerSerial>
                <ds:X509IssuerName>issuer information</ds:X509IssuerName>
                <ds:X509SerialNumber>issuer serial number</ds:X509SerialNumber>
            </ds:X509IssuerSerial>
        </ds:X509Data>
    </wsse:SecurityTokenReference>
</ds:KeyInfo>

10-05 18:23