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所指定的名称。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值