【发布时间】:2019-04-05 15:52:34
【问题描述】:
我有一个 jee 应用程序,我在其中生成 pdf 并在浏览器上将其显示给用户。我要求保护 pdf 以供打印,因此用户除了阅读之外不能对文档进行任何其他用途。我正在使用 itext 5 并尝试加密文档,但出现错误:java.lang.SecurityException:类“org.bouncycastle.asn1.ASN1Primitive”的签名者信息与同一包中其他类的签名者信息不匹配"
我查看了一些加密示例,甚至在 itext5 的书中,但我无法修复该错误。我还查看了我的问题的一些解决方案,有人说这是一个依赖问题,可能是重复的,但我仍然找不到在哪里,因为我什至没有将 bouncycastle 添加到我的 pom 中。
这是我的 web 模块的 pom.xml 的摘录。
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<itext.version>5.5.13</itext.version>
</properties>
<dependecies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>${itext.version}</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>${itext.version}</version>
</dependency>
</dependencies>
The code I am trying to run is the next:
public void crearPDFconTextoHTML(String nombreArchivo, String texto) {
try {
String dirCarpetaTemp = directorio();//Creo el directorio temporal sin los archivos
String dirFinalArchivo = dirCarpetaTemp + nombreArchivo;//Directorio completo del archivo
FileOutputStream fos = new FileOutputStream(dirFinalArchivo);
Document document = new Document(PageSize.A4);
String USER_PASS = "Hello123";
String OWNER_PASS = "Owner123";
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
document.setMarginMirroring(true);
document.open();
Paragraph parrafo = new Paragraph();
parrafo = convertirHtmlConFormatoAParrafoPdfOld(parrafo, texto);
// parrafo = convertirTexto(parrafo, texto);
parrafo.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(parrafo);
document.close();//cierro el documento
writer.close();//cierro la escritura
setNombreArchivo(nombreArchivo);//Agregar el nombre archivo final a la dependencia
// OutputStream fos = new FileOutputStream(dirCarpetaTemp + nombreArchivo);//Crear archivo final
// PdfTempUtil.doMerge(listPdfs, fos);//Combinar pdfs que se agregaron en la lista
generarLinksAccesoAlPdf();//Generar la url que se mostrará en la lista
} catch (DocumentException | IOException ex) {
System.out.println(ex);
}
}
依赖项应该没问题,加密应该可以工作,这样用户在打开 pdf 时就无法打印它。
【问题讨论】:
标签: encryption jakarta-ee itext