使用 Itext5 签署 PDF 在 Quarkus 中出现错误“PdfPKCS7 无法访问 void org.bouncycastle.asn1.DERNull.<init>()”

问题描述 投票:0回答:1

我正在尝试在 quarkus 应用程序中使用 itext5 签署 pdf 文件,但我收到一个我无法理解的错误。 在代码下方,我尝试使用它来签署我的 pdf。我升级了对 bouncyCastle 的依赖,但显示了另一个错误

代码如下:

 @Throws(GeneralSecurityException::class, IOException::class, DocumentException::class)
    fun sign(
        src: String? //The path of the pdf file that needs to be signed
        , dest: String? //The path of the pdf file where the seal is signed
        , chain: Array<Certificate> //Certificate chain
        , pk: PrivateKey? //Signature private key
        , digestAlgorithm: String? //Abstract Algorithm name, such as SHA-1
        , provider: String? //Key algorithm provider, can be null
        , subfilter: CryptoStandard? //Digital signature format, itext has 2 types
        , reason: String? //The reason for the signature, displayed in the pdf signature attribute, Just fill in
        , location: String?
    ) {
        //The steps below are fixed, just follow the

        //instructions , there is nothing to explain //Creating the reader and the stamper, start pdfreader
        val reader = PdfReader(src)
        //Target file output stream
        val os = FileOutputStream(dest)
        //Create a signature tool PdfStamper. If the last boolean parameter
        //false, the pdf file is only allowed to be signed once and signed multiple times. If the last one is valid
        //true, the pdf can be signed and verified The signature tool can identify whether the document is modified after each signature.
        val stamper = PdfStamper.createSignature(reader, os, '\u0000', null, true)
        //Get the digital signature attribute object and set the attributes of the digital signature
        val appearance = stamper.signatureAppearance
        appearance.reason = reason
        appearance.location = location
        //Set the location of the signature, the page number, and the name of the signature domain. When multiple signatures are added, the pre-name of the signature cannot be the same.
        //The position of the signature is the position coordinate of the stamp relative to the pdf page. The origin is the lower left corner of the pdf page.
        //4.The two parameters are: the lower left corner of the stamp x, the lower left corner of the stamp y, the upper right corner of the stamp x, the upper right corner of the stamp y
        appearance.setVisibleSignature(Rectangle(200f, 200f, 300f, 300f), 1, "sig1")
        //Read the stamp image, this image is the image of the itext package
        val image: Image = Image.getInstance("path_to_image")
        appearance.signatureGraphic = image
        appearance.certificationLevel = PdfSignatureAppearance.NOT_CERTIFIED
        //Set the display mode of the stamp. The following option is to display only the stamp (there are other modes, and the stamp and signature description can be displayed together)
        appearance.renderingMode = RenderingMode.GRAPHIC

        //The itext here provides 2 interfaces for signatures, which can be implemented by yourself. I will focus on this implementation later.
        //Digest algorithm
        val digest: ExternalDigest = BouncyCastleDigest()
        //Signature algorithm
        val signature: ExternalSignature = PrivateKeySignature(pk, digestAlgorithm, null)
        //Call itext signature method to complete pdf signature
        MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter)
    }

这些是我在升级 bouncyCastle 后使用的依赖项。

 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.5.13.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk15on</artifactId>
      <version>1.70</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcpkix-jdk15on</artifactId>
      <version>1.70</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15on -->
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcmail-jdk15on</artifactId>
      <version>1.70</version>
    </dependency>

这是我遇到的问题的堆栈跟踪错误。

2022-01-20 16:29:37,480 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-0) HTTP Request to /api/pdf failed, error id: 0c85cbe3-7a9b-4899-a00e-ed94d9f8be37-1: org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalAccessError: class com.itextpdf.text.pdf.security.PdfPKCS7 tried to access private method 'void org.bouncycastle.asn1.DERNull.<init>()' (com.itextpdf.text.pdf.security.PdfPKCS7 and org.bouncycastle.asn1.DERNull are in unnamed module of loader io.quarkus.bootstrap.classloading.QuarkusClassLoader @1bd4fdd)
    at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:105)
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:359)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
    at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:164)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:247)
    at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:135)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:90)
    at io.quarkus.vertx.core.runtime.VertxCoreRecorder$13.runWith(VertxCoreRecorder.java:543)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
    at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
    at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:829)
kotlin pdf bouncycastle quarkus sign
1个回答
0
投票

需要更新com.itextpdf:sign的版本到7.2.5.

© www.soinside.com 2019 - 2024. All rights reserved.