Signed jar files

2008-02-23 09:35:24来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Signed jar files
The policy file technique does not apply to Web browsers. Before you can execute FileIO in a Web browser, you first store that Applet's classfiles in a jar file and digitally sign that jar file.

Signing a jar file requires a certificate. Although you can purchase a certificate when you want to distribute an applet commercially, I will show you how to create a free self-signed certificate (which you only use for testing). Complete the following steps to create a jar file, to create a self-signed certificate, and to sign that jar file with the certificate:

  1. Create the jar file: Execute jar cvf FileIO.jar *.class. You end up with a FileIO.jar jar file.
  2. Create a new key in a new keystore: Execute keytool -genkey -keystore myKeyStore -alias me. Alias "me" is arbitrary. It reminds you that the certificate based on the keystore is self-signed so you don't accidentally put it into production.

    The keytool prompts you for information about the new key: It asks you for a password to protect the keystore. Then it asks you for your name, department, organization, city, region, and country. This information will go into the new keystore file—myKeyStore, in this example.

  3. Create a self-signed test certificate based on the keystore: Execute keytool -selfcert -alias me -keystore myKeyStore. Enter the keystore password when prompted.
  4. Sign the jar file with the testing certificate: Execute jarsigner -keystore myKeyStore FileIO.jar me. Enter the keystore password when prompted.

    The jarsigner program updates the jar file's META-INF directory to contain certificate information and digital signatures for each entry in the archive. If all goes well, you end up with a signed FileIO.jar file.

Note I recommend studying the tools documentation section of the J2SE documentation to learn more about jar, keytool, and jarsigner.

Before executing the applet in a Web browser via the signed jar file, create an appropriate HTML file whose <applet> tag includes an archive attribute identifying the jar file. Listing 4's FileIO2.html should do nicely.

Listing 4. FileIO2.html

<applet archive=FileIO.jar code=FileIO.class width=250 height=250>
</applet>

It's time to execute the applet. Assuming FileIO.jar and FileIO2.html are located in the c:\temp directory on a Windows machine, start the Web browser and enter c:\temp\FileIO2.html into that browser's address bar. After a few moments, a dialog box should appear. That dialog box, as shown in Figure 2, presents a security warning and asks you to grant permission to run the applet.


Figure 2. The Java Security Warning dialog box identifies a signed applet. Click on thumbnail to view full-sized image.

Click either the Grant This Session button or the Grant Always button to proceed. If you're curious, click the View Certificate button to view the details of the self-signed certificate that you previously created. Figure 3 shows the applet embedded in the Firefox browser.


Figure 3. The Web browser alternative to running FileIO in appletviewer

Review

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:windowsxp下集成Tomcat与Apache

下一篇:在NetBeans平台上开发J2ME游戏实例讲解(第一部分)