Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中移动签名XML中的签名节点_Java_Xml_Sign - Fatal编程技术网

如何在Java中移动签名XML中的签名节点

如何在Java中移动签名XML中的签名节点,java,xml,sign,Java,Xml,Sign,我有一个XML示例。我需要的是用Java对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:Busines

我有一个XML示例。我需要的是用Java对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>20DF8D44-ABBE-76BA-E053-AA02010AEA8E</fu:MessageID>
            <fu:DateTime>2015-09-24T12:07:28</fu:DateTime>
        </fu:Header>
        <fu:BusinessPremise>
            <fu:TaxNumber>99999862</fu:TaxNumber>
            <fu:BusinessPremiseID>P001</fu:BusinessPremiseID>
            <fu:BPIdentifier>
                <fu:RealEstateBP>
                    <fu:PropertyID>
                        <fu:CadastralNumber>2606</fu:CadastralNumber>
                        <fu:BuildingNumber>6967</fu:BuildingNumber>
                        <fu:BuildingSectionNumber>1</fu:BuildingSectionNumber>
                    </fu:PropertyID>
                    <fu:Address>
                        <fu:Street>TEST</fu:Street>
                        <fu:HouseNumber>7</fu:HouseNumber>
                        <fu:HouseNumberAdditional>C</fu:HouseNumberAdditional>
                        <fu:Community>KOPER</fu:Community>
                        <fu:City>KOPER</fu:City>
                        <fu:PostalCode>6000</fu:PostalCode>
                    </fu:Address>
                </fu:RealEstateBP>
            </fu:BPIdentifier>
            <fu:ValidityDate>2010-09-24</fu:ValidityDate>
            <fu:SoftwareSupplier>
                <fu:TaxNumber>10031685</fu:TaxNumber>
            </fu:SoftwareSupplier>
            <fu:SpecialNotes>Primer prijave poslovnega prostora</fu:SpecialNotes>
        </fu:BusinessPremise>
    </fu:BusinessPremiseRequest>
</soapenv:Body>
我想要的是在“fu:businessPremiseRequest”节点中获取“签名”节点

<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:Header>
<fu:BusinessPremise>...</fu:BusinessPremise>
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">...</Signature>
</fu:BusinessPremiseRequest>
</soapenv:Body>

...
...
...

在我的例子中,我将节点置于错误的位置(下面的示例)


....
..

我在Java示例中做错了什么?或者如何在内部移动节点?

您的错误可能在这里:
DOMSignContext dsc=new-DOMSignContext(privateKey,doc.getDocumentElement())
您告诉
DOMSignContext
应该将
Signature
附加到主文档元素


如果您想在
fu:BusinessPremiseRequest
中使用该节点,您应该首先获取该节点,并将其作为参数传递到
new DOMSignContext()
构造函数中。

有人能帮我吗?这是我第一次使用Java对xml进行签名。。我可以使用一些Xpath表达式来获取节点吗?我已经这样做了:使用Xpath我提取了节点列表。nodes=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);System.out.println(“节点长度:+nodes.getLength());if(nodes.getLength()==0){System.out.println(“找不到id为:+id的节点”);return;}nodeToSign=nodes.item(0);sigParent=nodeToSign.getParentNode();现在你在你想要的地方签名了吗?:)问题是不是在fu:businesspremiserequest里面,而是在这个标签下面……所以我的答案不正确?您是否将“fu:BusinessPremiseRequest”节点传递给
new-DOMSignContext()
constructor?
<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:Header>
<fu:BusinessPremise>...</fu:BusinessPremise>
     <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">...</Signature>
</fu:BusinessPremiseRequest>
</soapenv:Body>
<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:BusinessPremiseRequest>
</soapenv:Body>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">..</Signature>
</soapenv:Envelope>