0
我在Java applet中使用PdfStamper來簽署pdf文件。 問題在於,當pdfStamper.close()到達時,applet每次都會暫停。 我認爲我的問題是相關的一些Java applet的政策,但我已授予的權限都喜歡:在Java Applet中的pdfStampper
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方法運行相同的源代碼(對傳遞參數進行很少修改)時,它工作得很好。
任何想法是什麼問題?
爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-05-14 14:18:10
小程序是否已簽名?如果不是,它使用標準安全模型http://docs.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/security.html – SJuan76 2013-05-14 14:34:08
Addionally:您正在創建一個過時類型的簽名。請閱讀http://itextpdf.com/book/digitalsignatures – 2013-05-14 14:41:17