Java 签名 XML 文件 - 防止签名内的标准 XML 签名命名空间
Java signing XML File - prevent standard XML signature namespace inside 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>
我怎样才能让它工作?因为在第三方系统中我需要根据架构检查它,我自己定义签名的结构,所以我的
中应该有两个 xmlns
根据 XML 标准,您必须为 <Signature>
元素定义名称空间,因此它可以是您拥有的名称空间,也可以位于带有前缀的父元素之外。作为
<rootElemnt xmlns:sig="http://....">
<sig:Signature>....
但是,为什么要打扰您呢?没有它 <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 数字签名 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>
也许默认情况下它应该看起来像那样,但如果不是,我想如果你将 customElement
分别编组到 XML 中,然后将其添加到签名中,它一定是那样的。
我正在签署一个 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>
我怎样才能让它工作?因为在第三方系统中我需要根据架构检查它,我自己定义签名的结构,所以我的
中应该有两个 xmlns根据 XML 标准,您必须为 <Signature>
元素定义名称空间,因此它可以是您拥有的名称空间,也可以位于带有前缀的父元素之外。作为
<rootElemnt xmlns:sig="http://....">
<sig:Signature>....
但是,为什么要打扰您呢?没有它 <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 数字签名 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>
也许默认情况下它应该看起来像那样,但如果不是,我想如果你将 customElement
分别编组到 XML 中,然后将其添加到签名中,它一定是那样的。