当试图解组BES-XADES签名文档时,JAXB会遇到麻烦

时间:2013-04-30 14:33:08

标签: java jaxb xades4j

我需要你的帮助:拜托,拜托。 我有一个Xades签名的XML文档,我作为byteArray接收:所以我将我的byteArray转换为String。 之后,我尝试解组,以便获得映射的Java对象。

结果是我获得了一个带有所有信息的“MyJavaObj”实例,但是Xades-Signature。我的签名在Java对象中为空,而所有其他信息都是良好映射的。以下是我的java方法。请帮助我在MyJavaObj的实例中获取对象签名。

<school>
    <documentVersion>2.10</documentVersion>
    <teacher>
    .....
    </teacher>
    <student>
        <name></name>
        <age></age>
        ....
    </student>
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-2b72d2f4-4794-4a8b-8cbf-4c74d33629a7">
        <ds:SignedInfo>
        ........
        </ds:SignedInfo>
        .......
    </ds:Signature>
</school>

这是转换的方法

public static MyJavaObj unmarshallBinary(final byte[] pByteStr) {
    try {
        final String xmlFlow = new String(pByteStr, "UTF-8");

        final StringBuffer xmlStr = new StringBuffer(xmlFlow);
        // Unmarshalling with JAXB
        final JAXBContext jaxbContext = JAXBContext.newInstance("generated.package");
        // marshaller
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        // Unmarshalling 
        final JAXBElement<MyJavaObj> root = unmarshaller.unmarshal(
            new StreamSource(new StringReader(xmlStr.toString())), MyJavaObj.class);
        return root.getValue();
    } catch (final Throwable excep) {
        excep.printStacktrace();
    }
}

MyJavaObj result = unmarshallBinary(.. a ByteArray ..);

result.getDocumentVersion():返回2.10;

result.getStudent():返回学生;

result.getSignature():return NULL;

文件MyJavaObj.java有很好的注释

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "My_Java_Obj", namespace = "urn:my_java_obj", 
propOrder = {"documentVersion", "teacher", "student","signature"})
public class MyJavaObj {

    @XmlElement(name = "documentVersion", required = true)
    protected String documentVersion;
    @XmlElement(name = "teacher", required = true)
    protected Teacher teacher;
    @XmlElement(name = "student", required = true)
    protected Student student;

    @XmlElement(name = "Signature")
    protected Signature signature;
    @XmlAttribute(name = "Id", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String id;
    .....
    getters and setters
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

Signature元素位于http://www.w3.org/2000/09/xmldsig#"命名空间中,因此您需要将其包含在@XmlElement字段的signature中。

@XmlElement(name="Signature", namespace="http://www.w3.org/2000/09/xmldsig#")
protected Signature signature;

了解更多信息

答案 1 :(得分:0)

我的解决方案有点不同

我用uddi-ws。 Maven依赖关系在这里:

    <!-- https://mvnrepository.com/artifact/org.apache.juddi/uddi-ws -->
    <dependency>
        <groupId>org.apache.juddi</groupId>
        <artifactId>uddi-ws</artifactId>
        <version>3.3.2</version>
    </dependency>

在课堂上:

//in my case, import org.w3.x2000.x09.xmldsig.SignatureType isn't working
import org.w3._2000._09.xmldsig_.SignatureType;

//Signature declaration    
private SignatureType Signature;

//namespace is important part, without namespace, it returns null
@XmlElement(name = "Signature", namespace = "http://www.w3.org/2000/09/xmldsig#")
public SignatureType getSignature() {
    return Signature;
}

更多信息: https://www.codesynthesis.com/pipermail/xsd-users/2006-December/000674.html