Create a trust applet for accessing the local file system (1)

zhaozj2021-02-16  46

Create a trust applet from the local file system (1) Written by Olexiy & Alexander Prokhorenko Translated by Caiyi0903 (Willpower), 2004.1.29

Create a trust applet, which has access to the local client file system, which has always been an important topic. Due to some specification, many issues can only be implemented on the client. Therefore, in many cases, the programmers who are unfamiliar with trust Applet technology think that it is impossible to implement the operation of the client file and search other operations. However, there is indeed such a way and can be widely used in many ways. In this article, I will show you an example to create a simple trust applet, which will access a local text file you specify (Note: You need to use JDK1.3 or higher).

First of all, I want to talk about the restrictions we have to do on the client software. I have to declare that the signature applet given here is only suitable for the client installed with JDK (1.3 version or more) or the like plug-in, and uses IE 4 or more browsers above Netscape 4.75 or later. In addition to the clients other than these conditions, this article will not support this article, and the signature applet for these old-fashioned other clients is required to use JDK1.1 technology, which is used by Javakey Tools. Because the use of the old way to create a signature applet has exceeded the scope of this article, it is necessary to explain it here.

Now, let's talk about the platform of our (programmer). We use the Windows platform with J2SDK 1.4.1 and Java container Jakarta-Tomcat 4.1.29. If you are using the same software, you can guarantee that all content of this skills will be perfect under your platform. In general, all the contents described herein should be able to run on a compatible software.

Before starting the encoding, I want to explain why we need to find a specific rather than the value of doing these simple standards, such as reading local files. There is a view in the Java 1.1 era, and it is necessary to change a part of the task to be executed to the client computer with the help of the applet to create future generation network computers. However, this approach is impossible to implement because Applet limits security. Developers also speculated that Applet is a so-called "sandbox" (Sandbox) - a simple location ---- does not allow reference to any system functions. With the development of Java technology, the trusted applet (Trusted Applet) is invented, and the "sandbox" safety rule in this applet is not included. So how should we take this opportunity? Below we will find the answer.

In JDK, in addition to Javac, we need two additional utilities - KeyTool and Jarsigner. KeyTool is a tool for creating and controling a pair of private and public keys to manage your private key, namely .keystore file. The Jarsigner tool is used to sign a JAR file, which has an Applet class in the jar file that can be used to verify the digital signature distributed together with the applet. The entire process of signatures is very simple; however, we need to do some initialization preparations. Let us first create our applet. One of the text fields, we will enter a file name, a big text area (Text Area) to display the file content, and a LOAD button will perform the execution file. The source code of this applet is shown in the following picture, because it is simple, we have added some comments. We don't have specially add some special checkboxes and more complex features, so this code is easy to read, and readers can focus on the process of transitioning to trust to the trust applet. In addition, you can even create your own applet based on your own favorite visual tools such as Sun One Studio, JBuilder et al., But we provide this source code to help you understand the applet signature. the whole process. 1: // TestApplet.java

2: Import java.io. *;

3: Import java.aw. *;

4: import java.awt.event. *;

5: Import java.applet. *;

6: Import javax.swing. *;

7: Import javax.swing.border. *;

8:

9: Public Class TestApplet Extends Japplet

Implements anctionsListener {

10: Private JPanel Pane = NULL;

11: private jscrollpane scrolling = null;

12: private jtextpane filebox = null;

13: Private JtextField Tffilename = NULL;

14: private jbutton butload = null;

15: private final string loading = "loading";

16:

17: Public void init () {

18: Try {

19: jbinit ();

20:} catch (exception e) {

21: E.PrintStackTrace ();

twenty two: }

twenty three: }

twenty four:

25: // Method Which Will Read Data from File, And Return IT IN

// String

25: Public String Readfile (String Fn) {

26: String thisline, Ret = "";

27: Try {

28: FileInputStream Fin = New FileInputStream (FN);

29: BufferedReader Myinput = New BufferedReader

NEW INPUTSTREAMREADER (FIN);

30: While ((thisline = Myinput.readline ())! = Null) {31: RET = thisline "/ n";

32:}

33:} catch (exception e) {

34: Ret = "cannot loading, Exception!";

35:}

36: Return Ret;

37:}

39:

40: Private void jbinit () throws exception {

41: PANE = New jPanel ();

42: Pane.setbounds (New Rectangle (0, 0, 500, 325));

43: Pane.setLayout (NULL);

44: Pane.setBorder (BorderFactory.createEtchedborder)

Etchedborder.Lowered));

45: Pane.setBackground (New Color (221, 194, 219);

46:

47: filebox = new jtextpane ();

48: FileBox.Settext ("");

49: filebox.setedItable (false);

50: scrolling = new jscrollpane (filebox);

51: Scrolling.setBounds (New Rectangle (16, 65, 295, 225);

52:

53: TffiLename = New jtextfield ();

54: TFFILENAME.SETTEXT ("");

55: TffiLename.SetBounds (New Rectangle (16, 23, 206, 29);

56:

57: butload = new jbutton ();

58: butload.setbounds (New Rectangle (231, 23, 80, 30));

59: Butload.Settext ("LOAD");

60: Butload.SetActionCommand (LOAD);

61: butload.addactionListener (this);

62:

63: Pane.Add (scrolling);

64: Pane.Add (TFFFileName);

65: Pane.Add (Butload);

66:

67: SetContentPane (PANE);

68:}

69:

70: Public void actionperformed (ActionEvent E) {

71: IF (E.GetActionCommand (). Equals (loading)) {

72: filebox.settext (readfile (tffilename.gettext ()));

73:}

74:}

75:}

The following steps are also very simple. We have to compile, create a JAR compressed package, create a simple HTML file to present text files and try to use it. Perhaps it can also load a file without using the signature technology. Let's take a look, to compile and create a JAR compression package, use the following steps:

Javac testapplet.javajar CVF Testapplet.jar testapplet.class

The CVF parameters here are the file named TestApplet.jar of the JAR package and display detailed output information. Ok, let's follow the text editor to create a TestApplet.html file.

1:

2:

Width = 325 height = 325>

3:

4:

Then create a text file in the same text editor, such as C: /Demo.txt, in this file, you need to enter some words and sentences. Here, we entered HelloWorld. Therefore, if it is successfully loaded, you should be able to see these content.

Copy your TestApplet.html and testapplet.jar files into your Tomcat's WebApp directory, which stores your web application. Then browse this html file, here is http: // localhost: 8080 / green / testapplet.html

When Applet is loaded, enter a text file path (C: /Demo.txt) and click the "LOAD" button. The following will be what you see:

OK, we get an exception information. This is not surprising. Let us try to sign it, and then look at what happens. Do you want to run the ToolKey tool in the following ways:

KeyTool -Genkey -Alaias Testapplet -validity 365

(To be contract ...)

转载请注明原文地址:https://www.9cbs.com/read-24917.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.060, SQL: 9