Applet的數字簽名

需求:從客戶端本地properties文件中讀取上、下、左、右頁邊距,設置打印頁邊距;

說明:設置頁邊距的properties文件存放於C:/SCSIHIS/orderprinter.properties,打印代碼通過JavaScript實現,需要將頁邊距取出送送入對應的JavaScript函數。

分析與實現:

       當需要用Applet訪問客戶端本地文件時,需要對Applet進行數據簽名,授予其訪問本地文件的權限,此外,需要在JavaScript中調用Applet中的函數,即需要JavaScriptApplet進行通訊。

       因爲JavaScript可以訪問Applet中爲訪問權限爲public的方法,因此,可以使用這點來完成讀取數據工作,目前的重點是如何爲Applet簽名,讓其有權限訪問本地文件,下面,我會一步一步地來教會大家如何對Applet進行數字簽名。

首選,讓我們來看下 Applet源代碼:

       MarginApplet.java

import java.applet.Applet;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.Properties;

 

public class MarginApplet extends Applet {

      

       private static final long serialVersionUID = 1L;

       public String top;

       public String bottom;

       public String left;

       public String right;

 

       public void init() {       }

      

       public void start() {

              setTop(getValue("Top"));

              setBottom(getValue("Bottom"));

              setLeft(getValue("Left"));

              setRight(getValue("Right"));

       }

 

       public String getValue(String key) {

              InputStream in = null;

              try {

                     Properties props = new Properties();

                     String filePath = "C:/SCSIHIS/orderprinter.properties";

                     try {

                            in = new BufferedInputStream(new FileInputStream(filePath));

                            props.load(in);

                            String value = props.getProperty(key);

                            System.out.println(key + value);

                            return value;

                     } catch (Exception e) {

                            e.printStackTrace();

                            return null;

                     }

 

              } catch (Exception e) {

                     e.printStackTrace();

              }

              return null;

       }

      

       public String getBottom() {

              repaint();

              return bottom;

       }

       public String getLeft() {

              repaint();

              return left;

       }

public String getRight() {

              repaint();

              return right;

       }

       public String getTop() {

              repaint();

              return top;

       }

       public void setBottom(String string) {

              bottom = string;

              repaint();

       }

       public void setLeft(String string) {

              left = string;

              repaint();

       }

       public void setRight(String string) {

              right = string;

              repaint();

       }

       public void setTop(String string) {

              top = string;

              repaint();

       }     

       public String getDemo(){

              return "abcde";

       }

 

1.              MarginApplet打包成jar文件

javac MarginApplet.java

jar –cvf MarginApplet.jar *.class

 

2.             在網頁上寫入調用Applet的代碼

<applet

codebase="."

code="MarginApplet.class"

archive="<%=request.getContextPath()%>/

protekted/ihis/om/orderentry/MarginApplet.jar"

width="0"

height="0"

name="app"

mayscript="mayscript">

</applet>

以上代碼中需要注意的是標註爲紅色的部分,如果要在JavaScript中直接對Applet中的public類型方法進行訪問的話,mayscript=”mayscript”是必須的,否則將不能訪問

 

3.             生成證書與及簽名

l         keytool -genkey -keystore yuzp.store -alias yuzp

該命令用於產生密鑰,執行完畢後會在當前目錄下產生一個名爲yuzp.store的文件,上面命令中的yuzp是我的名字,可以隨便更換的,此外,在執行此命令時會要求輸入密鑰庫密碼,該密碼需要記住,否則以後將無法使用。

 

l         keytool -export -keystore yuzp.store -alias yuzp -file yuzp.cert

該命令用於生成證書文件,執行完畢後會在當前目錄生成一個名爲yuzp.cert的證書

 

l         jarsigner -keystore yuzp.store MarginApplet.jar yuzp

該命令用於對我們生成的MarginApplet.jar包進行簽名

 

4.             創建策略文件

經過以上步驟,我們的MarginApplet就已經完成了數字簽名,但還需要經過以下步後才能使用:

創建名爲applet.policy的文件,其內容爲:

keystore "file:c: /demo/yuzp.store", "JKS";
  grant signedBy "yuzp"
  { permission java.io.FilePermission "<<ALL FILES>>", "read";

此文件讓作用是讓由yuzp簽名的Applet擁有本地所有文件的讀權限.

 

5.             修改java.security文件

找到{JAVA_HOME}/jre/lib/security目錄下的java.security,找到下面這兩行:
  policy.url.1=file:${java.home}/lib/security/java.policy
  policy.url.2=file:${user.home}/.java.policy
  
  在下面添寫第三行  
  policy.url.3=file:c: /demo/applet.policy  
  完成這個修改後我們在前面創建的applet.policy文件纔有效。

 

至此,對Applet的簽名就完成了,呵呵,還是比較簡單吧^_^

以下給出在JavaScript中訪問Applet方法的代碼:

function getMarginApplet(){

              if (document.applets[0].getTop() == undefined){

                     topMargin = "5";        

              }else{

                     topMargin = document.applets[0].getTop();

              }     

             

              if (undefined == document.applets[0].getBottom()){

                     bottomMargin = "5";

              }else{

                     bottomMargin = document.applets[0].getBottom()

              }

             

              if (undefined == document.applets[0].getLeft()){

                     leftMargin = "5";

              }else{

                     leftMargin = document.applets[0].getLeft()

              }

             

              if (undefined == document.applets[0].getRight()){

                     rightMargin = "5";

              }else{

                     rightMargin = document.applets[0].getRight()

              }

       }

 

除了使用“document.applets[0].Applet方法名”這種方式名,還可以使用

document.AppletName.Applet方法名”這種方式來訪問,其中AppletName<applet/>定義中的name所指定的名稱。

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章