我在 Java 小程序中使用 PdfStamper 来签署 pdf 文件。问题是每次到达与 pdfStamper.close(); 一致时,小程序都会暂停。我认为我的问题与一些 java 小程序策略有关,但我已授予所有权限,例如:
grant {
permission java.security.AllPermission;
};
我的代码是:
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfSignatureAppearance;
import com.lowagie.text.pdf.PdfStamper;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.AccessController;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PrivilegedAction;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import javax.swing.JApplet;
public class SignApplet extends JApplet {
public void test() {
AccessController.doPrivileged( new PrivilegedAction() {
@Override
public Object run() {
try {
KeyStore keyStore = KeyStore.getInstance( "PKCS11-custom_name" );
String pin = "custom_pin";
keyStore.load( null, pin.toCharArray() );
Enumeration<String> aliases = keyStore.aliases();
String alias = aliases.nextElement();
PrivateKey key = ( PrivateKey ) keyStore.getKey( alias, pin.toCharArray() );
Certificate[] chain = keyStore.getCertificateChain( alias );
String unsigned_pdf = "C:\\Users\\user_name\\unsigned.pdf";
String signed_pdf = "C:\\Users\\user_name\\signed.pdf";
PdfReader pdfReader = new PdfReader( (new File( unsigned_pdf )).getAbsolutePath() );
File outputFile = new File( signed_pdf );
PdfStamper pdfStamper;
pdfStamper = PdfStamper.createSignature( pdfReader, null, '\0', outputFile );
PdfSignatureAppearance sap = pdfStamper.getSignatureAppearance();
sap.setCrypto( key, chain, null, PdfSignatureAppearance.SELF_SIGNED );
sap.setReason( "reason" );
sap.setLocation( "" );
sap.setVisibleSignature( new Rectangle( 10, 10, 50, 30 ), 1, null );
pdfStamper.setFormFlattening( true );
pdfStamper.close(); // -- applet suspends right there
} catch ( Exception ex ) {
ex.printStackTrace();
}
return null;
}
} );
}
}
我像这样从 html 运行它:
<APPLET CODE="SignApplet.class" NAME="SIGNAPPLET" ARCHIVE="SignApplet-1.0.jar, itext-2.1.7.jar" WIDTH="0" HEIGHT="0"></APPLET>
<h:form>
<h:button onclick="document.SIGNAPPLET.test();" value="--- TEST ---" />
</h:form>
密钥库来自 USB 令牌。
在 java 调试控制台中,我没有看到任何异常。当我从 main 方法运行相同的源代码时(对传递参数几乎没有修改),它工作得很好。
知道有什么问题吗?