尝试使用此代码段使用apache POI OOXML验证已签名的docx文件。
OPCPackage pkg = OPCPackage.open(inputStream);
SignatureConfig sic = new SignatureConfig();
sic.setOpcPackage(pkg);
SignatureInfo si = new SignatureInfo();
si.setSignatureConfig(sic);
boolean isValid = si.verifySignature();
这是我的异常堆栈跟踪:告诉我有一个类强制转换异常!!
java.lang.ClassCastException: org.apache.xmlbeans.impl.values.XmlAnyTypeImpl cannot be cast to org.openxmlformats.schemas.xpackage.x2006.digitalSignature.CTRelationshipReference
at org.apache.poi.poifs.crypt.dsig.services.RelationshipTransformService.init(RelationshipTransformService.java:150)
at org.apache.jcp.xml.dsig.internal.dom.DOMTransform.<init>(DOMTransform.java:92)
at org.apache.jcp.xml.dsig.internal.dom.DOMReference.<init>(DOMReference.java:205)
at org.apache.jcp.xml.dsig.internal.dom.DOMManifest.<init>(DOMManifest.java:102)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLObject.<init>(DOMXMLObject.java:112)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignature.<init>(DOMXMLSignature.java:163)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:206)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:161)
at org.apache.poi.poifs.crypt.dsig.SignatureInfo$SignaturePart.validate(SignatureInfo.java:237)
at org.apache.poi.poifs.crypt.dsig.SignatureInfo.verifySignature(SignatureInfo.java:296)
at com.hamed.docx_signer.DocxSigner.verify(DocxSigner.java:74)
这是我的POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.me</groupId>
<artifactId>docx-signer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>docx-signer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11-beta3</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.51</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.51</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
</project>
这是我的日志:
2014/11/19 01:17:28 DEBUG org.apache.poi.openxml4j.opc.PackageRelationshipCollection - Parsing relationship: /_xmlsignatures/_rels/origin.sigs.rels
2014/11/19 01:17:28 DEBUG org.apache.poi.openxml4j.opc.PackageRelationshipCollection - Parsing relationship: /word/_rels/document.xml.rels
2014/11/19 01:17:28 DEBUG org.apache.xml.security.Init - Registering default algorithms
2014/11/19 01:17:29 DEBUG org.apache.poi.openxml4j.opc.PackageRelationshipCollection - Parsing relationship: /_rels/.rels
2014/11/19 01:17:29 DEBUG org.apache.poi.poifs.crypt.dsig.SignatureInfo - Digital Signature Origin partName: /_xmlsignatures/origin.sigs - Content Type: application/vnd.openxmlformats-package.digital-signature-origin
2014/11/19 01:17:29 DEBUG org.apache.poi.poifs.crypt.dsig.SignatureInfo - XML Signature partName: /_xmlsignatures/sig1.xml - Content Type: application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml
2014/11/19 01:17:29 DEBUG org.apache.poi.poifs.crypt.dsig.services.RelationshipTransformService - constructor
2014/11/19 01:17:29 DEBUG org.apache.poi.poifs.crypt.dsig.services.RelationshipTransformService - init(parent,context)
2014/11/19 01:17:29 DEBUG org.apache.poi.poifs.crypt.dsig.services.RelationshipTransformService - parent java type: javax.xml.crypto.dom.DOMStructure
2014/11/19 01:17:29 ERROR org.apache.poi.poifs.crypt.dsig.SignatureInfo - error in marshalling and validating the signature
java.lang.ClassCastException: org.apache.xmlbeans.impl.values.XmlAnyTypeImpl cannot be cast to org.openxmlformats.schemas.xpackage.x2006.digitalSignature.CTRelationshipReference
at org.apache.poi.poifs.crypt.dsig.services.RelationshipTransformService.init(RelationshipTransformService.java:150)
at org.apache.jcp.xml.dsig.internal.dom.DOMTransform.<init>(DOMTransform.java:92)
at org.apache.jcp.xml.dsig.internal.dom.DOMReference.<init>(DOMReference.java:205)
at org.apache.jcp.xml.dsig.internal.dom.DOMManifest.<init>(DOMManifest.java:102)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLObject.<init>(DOMXMLObject.java:112)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignature.<init>(DOMXMLSignature.java:163)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:206)
at org.apache.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:161)
at org.apache.poi.poifs.crypt.dsig.SignatureInfo$SignaturePart.validate(SignatureInfo.java:237)
at org.apache.poi.poifs.crypt.dsig.SignatureInfo.verifySignature(SignatureInfo.java:296)
at com.hamed.docx_signer.DocxSigner.verify(DocxSigner.java:74)
at com.hamed.docx_signer.DocxSignerTest.testVerify(DocxSignerTest.java:72)
请你帮忙吗?!
答案 0 :(得分:1)
这似乎是3.11-Beta3的发布问题:(
目前,请将ooxml-security.jar添加到您的pom.xml:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-security</artifactId>
<version>1.0</version>
</dependency>
在它上演中央之前,您需要添加apache repo:
<repositories>
<repository>
<id>apache-repo</id>
<name>Apache Repo</name>
<url>https://repository.apache.org/service/local/repositories/releases/content/</url>
</repository>
</repositories>
更新:将由1640846修正,即下一个poi发布