Java签名XML文件 - 阻止签名

时间:2017-12-08 13:42:08

标签: java xml namespaces digital-signature

我签署了一个XML文件,我使用的是Java XML数字签名API,从Java 6开始到现在为止。
网络来源:http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html

签名如下:

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">[...]</Signature>

现在我想知道,有没有办法签署XML文件,以防止API在我的标签中确定此xmlns="http://www.w3.org/2000/09/xmldsig#",以便我只有以下内容:

<Signature>...</Signature>

我非常感谢任何线索。

谢谢@Vadim的回答。让我们为我的问题提供更多细节。我有一个XML结构,如:

<?xml version="1.0" encoding="UTF-8" ?>
<tests xmlns="schema1">
<test>
</test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
</Signature>
</tests>

我怎样才能使这个工作?因为在第三方系统中我需要针对Schema进行检查,我自己定义了签名的结构,所以我的内部应该有两个xmlns

1 个答案:

答案 0 :(得分:0)

根据XML标准,您必须为<Signature>元素定义名称空间,因此它可以像您拥有它一样,也可以在带有前缀的父元素之外。如

 <rootElemnt xmlns:sig="http://....">
     <sig:Signature>....

但是,为什么它会打扰你?没有它<Signature>标签属于父元素的默认命名空间,而不属于正确的签名命名空间。

更新如果您有两个名称空间,则必须有两个xmlns声明。一个可以是默认的第二个必须有前缀。或两者都必须有前缀。

如果您的自定义元素位于xmlns="schema1",我认为您需要了解如何制作<sig:Signature xmlns:sig="http://www.w3.org/2000/09/xmldsig#">

<sch1:tests xmlns:sch1="schema1">
  <sch1:test>
  </sch1:test>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
     ...
     <sch1:customElement>...</sch1:customElement>
  </Signature>
</sch1:tests>

这取决于您如何构建完整的XML。对不起,我不知道如何在Java XML Digital Signature API中做到这一点(从未直接使用它,只是通过WSDL策略),但所有其他工具都能够处理名称空间前缀

它看起来也像:

<tests xmlns="schema1">
  <test>
  </test>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
     ...
     <sch1:customElement xmlns:sch1="schema1">...</sch1:customElement>
  </Signature>
</tests>

也许它应该看起来像defualt,但如果不是,我想如果你将customElement分别编组成XML然后将其添加到Signature中,它必须是这样的。