简体   繁体   English

Java 1.7 com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException:无法解析具有ID数据的元素

[英]Java 1.7 com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID data

I have done some xml signing in Java 1.6 but when I run the same example in Java 1.7 I get this kind of exception: 我在Java 1.6中做了一些xml签名但是当我在Java 1.7中运行相同的例子时,我得到了这种异常:

 javax.xml.crypto.dsig.XMLSignatureException:
 javax.xml.crypto.URIReferenceException:
 com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException:
 Cannot resolve element with ID data

my xml: 我的xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fu="http://www.fu.gov.si/" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
    <soapenv:Body>
        <fu:BusinessPremiseRequest Id="data">
            <fu:Header>
                <fu:MessageID>24A4BACB-2355-0564-E053-AB02010AD740</fu:MessageID>
                <fu:DateTime>2015-11-16T09:22:58</fu:DateTime>
            </fu:Header>
        </fu:BusinessPremiseRequest>
    </soapenv:Body>
</soapenv:Envelope>

and this is my source code: 这是我的源代码:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
Document doc = (Document) dbFactory.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));


NodeList nl = doc.getElementsByTagName("fu:BusinessPremiseRequest");
Node node = nl.item(0);

KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream("c:/cert/testfurs.p12"), "Geslo123#".toCharArray());
Enumeration e = p12.aliases();
String alias = (String) e.nextElement();
System.out.println("Alias certifikata:" + alias);
Key privateKey = p12.getKey(alias, "Geslo123#".toCharArray());

KeyStore.PrivateKeyEntry keyEntry
        = (KeyStore.PrivateKeyEntry) p12.getEntry(alias, new KeyStore.PasswordProtection("Geslo123#".toCharArray()));

X509Certificate cert = (X509Certificate) keyEntry.getCertificate();

PublicKey publicKey = cert.getPublicKey();

final XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM");
// Create a Reference to the enveloped document
Reference ref = sigFactory.newReference("#data",
        sigFactory.newDigestMethod(DigestMethod.SHA256, null),
        Collections.singletonList(sigFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
        null,
        null);


SignedInfo si = sigFactory.newSignedInfo(sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), sigFactory.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null), Collections.singletonList(ref));

// Create a KeyValue containing the RSA PublicKey 
KeyInfoFactory keyInfoFactory = sigFactory.getKeyInfoFactory();
X509IssuerSerial x509IssuerSerial = keyInfoFactory.newX509IssuerSerial(cert.getSubjectX500Principal().getName(), cert.getSerialNumber());

List x509Content = new ArrayList();

x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(x509IssuerSerial);

KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey);
X509Data xd = keyInfoFactory.newX509Data(x509Content);

// Create a KeyInfo and add the KeyValue to it
KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(xd));

// Create a DOMSignContext and specify the RSA PrivateKey and
// location of the resulting XMLSignature's parent element
DOMSignContext dsc = new DOMSignContext(
        privateKey,
        node
);

try like this (source: https://slo-tech.com/forum/t652679/449 ): 尝试这样(来源: https//slo-tech.com/forum/t652679/449 ):

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    Document doc = (Document) dbFactory.newDocumentBuilder().parse(new ByteArrayInputStream(vhodniXml.getBytes()));
    //Node node = doc.getElementsByTagName("fu:BusinessPremiseRequest").item(0);
    Enumeration e = p12.aliases();
    String alias = (String) e.nextElement();
    System.out.println("Alias certifikata:" + alias);
    Key privateKey = p12.getKey(alias, geslo.toCharArray());

    KeyStore.PrivateKeyEntry keyEntry
            = (KeyStore.PrivateKeyEntry) p12.getEntry(alias, new KeyStore.PasswordProtection(geslo.toCharArray()));

    X509Certificate cert = (X509Certificate) keyEntry.getCertificate();

    PublicKey publicKey = cert.getPublicKey();
    final XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM");
    // Create a Reference to the enveloped document
    Reference ref = sigFactory.newReference("#data",
            sigFactory.newDigestMethod(DigestMethod.SHA256, null),
            Collections.singletonList(sigFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
            null,
            null);

    SignedInfo si = sigFactory.newSignedInfo(sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), sigFactory.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null), Collections.singletonList(ref));

    // Create a KeyValue containing the RSA PublicKey
    KeyInfoFactory keyInfoFactory = sigFactory.getKeyInfoFactory();
    X509IssuerSerial x509IssuerSerial = keyInfoFactory.newX509IssuerSerial(cert.getSubjectX500Principal().getName(), cert.getSerialNumber());

    List x509Content = new ArrayList();

    x509Content.add(cert.getSubjectX500Principal().getName());
    x509Content.add(x509IssuerSerial);

    KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey);
    X509Data xd = keyInfoFactory.newX509Data(x509Content);

    // Create a KeyInfo and add the KeyValue to it
    KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(xd));

    NodeList nl = doc.getElementsByTagName("fu:BusinessPremiseRequest");
    Node node = nl.item(0);

    // Create a DOMSignContext and specify the RSA PrivateKey and
    // location of the resulting XMLSignature's parent element
    DOMSignContext dsc = new DOMSignContext(
            privateKey,
            node
    );
    ((Element) node).setIdAttribute("Id", true);


    // Create the XMLSignature (but don't sign it yet)
    XMLSignature signature = sigFactory.newXMLSignature(si, keyInfo);

    // Marshal, generate (and sign) the enveloped signature
    signature.sign(dsc);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 包com.sun.org.apache.xml.internal.security.utils.Base64不存在 - Package com.sun.org.apache.xml.internal.security.utils.Base64 does not exist java.lang.ClassNotFoundException: com.sun.org.apache.xml.internal.resolver.CatalogManager Java 11 - java.lang.ClassNotFoundException: com.sun.org.apache.xml.internal.resolver.CatalogManager Java 11 org.apache.commons.codec.binary.Base64和com.sun.org.apache.xml.internal.security.utils.Base64之间有什么区别吗? - Is there any difference between org.apache.commons.codec.binary.Base64 & com.sun.org.apache.xml.internal.security.utils.Base64? ResourceResolverException:无法解析 ID 为主体的元素 - ResourceResolverException: Cannot resolve element with ID Body Java XML: ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to class javax.swing.text.Element - Java XML: ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to class javax.swing.text.Element 如何解决netbean javafx应用程序中不存在com.sun.org.apache.xml.internal.security包 - How to solve com.sun.org.apache.xml.internal.security package does not exist in netbean javafx application 错误:&#39;com.sun.org.apache.xml.internal.utils.WrappedRuntimeException:XML文档结构必须在同一实体内开始和结束 - ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: XML document structures must start and end within the same entity 更改com.sun.org.apache.xml.internal.serialize.XMLSerializer&com.sun.org.apache.xml.internal.serialize.OutputFormat - Change the com.sun.org.apache.xml.internal.serialize.XMLSerializer & com.sun.org.apache.xml.internal.serialize.OutputFormat JAVA和XML:com.sun.org.apache.xpath.internal.XPathException:无法将#STRING转换为NodeList - JAVA & XML : com.sun.org.apache.xpath.internal.XPathException: Can not convert #STRING to a NodeList Java 8 to openJdk 11 com.sun.org.apache.xml.internal.* types not accessible - Java 8 to openJdk 11 com.sun.org.apache.xml.internal.* types not accessible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM