作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用以下代码片段使用 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)
请帮忙吗?!
最佳答案
这似乎是 3.11-Beta3 的发布问题:(
暂时请将 ooxml-security.jar 添加到您的 pom.xml 中:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-security</artifactId>
<version>1.0</version>
</dependency>
在将其暂存到中央之前,您需要添加 apache 存储库:
<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 版本
关于java - 无法转换为 org.openxmlformats.schemas.xpackage.x2006.digitalSignature.CTRelationshipReference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27002355/
尝试使用以下代码片段使用 apache POI OOXML 验证签名的 docx 文件时。 OPCPackage pkg = OPCPackage.open(inputStream);
我是一名优秀的程序员,十分优秀!