applet-数字签名-访问windows注册表

[color=green]★★★ 本篇为原创,需要引用转载的朋友请注明:《 http://stephen830.iteye.com/blog/254549 》 谢谢支持! ★★★[/color]

本篇将讲述2个问题:
(1)applet如何进行数字签名?
(2)applet如何访问本地的windows注册表?

applet作为java唯一的一种运行在浏览器客户端的小程序,在项目中有时候也会起到很关键的作用。applet如果需要访问一些本地资源(这是指客户终端电脑中的资源),那么该applet必须要经过数字签名,而且客户在使用applet前必须接受你设定的数字签名,这样applet才允许访问你所需要的本地资源,比如访问本地的注册表。

(1)如何进行数字签名?
首先将你写好的applet生成jar包,如果用的是eclipse的话,你可以直接用eclipse的Export菜单将写好的applet生成一个jar文件。具体方法如下图:

[img]http://stephen830.iteye.com/upload/attachment/43271/70c36685-f41f-330e-8cc4-aec5bc9d115c.png[/img]
[img]http://stephen830.iteye.com/upload/attachment/43273/77c78714-baef-3159-bc49-aca7275f9ed7.png[/img]
[img]http://stephen830.iteye.com/upload/attachment/43275/99eabfea-8e8e-3493-8a46-576ae982cf2c.png[/img]
[img]http://stephen830.iteye.com/upload/attachment/43277/50e564e8-4e72-3cb5-b1a6-84d4a3147efa.png[/img]
[img]http://stephen830.iteye.com/upload/attachment/43279/6e9ce386-1be1-303e-adef-c5d36e0d51f2.png[/img]

这样子就生成jar文件了。

接下来就要对jar文件进行数字签名了。首先打开一个dos窗口,进入到你安装的jdk目录下的bin目录。运行下面的命令:
keytool -genkey -alias loginKey -keystore mylogin.key
[img]http://stephen830.iteye.com/upload/attachment/43281/607b417c-4714-3e77-9f49-8a5999151dab.png[/img]
按照命令出现的提示依次回答,其中第1项是要求输入密码,这个必须记下来,等下会用到的。
[img]http://stephen830.iteye.com/upload/attachment/43283/555fb53c-6e08-3b05-98d6-13269c4194d8.png[/img]

当完成上面的操作后,就可以在这个bin目录中看到一个签名文件mylogin.key,然后把jar文件复制到bin目录下,再执行下面的命令:
jarsigner -keystore mylogin.key login.jar loginKey
其中的[mylogin.key]和[loginKey]分别是前面keytool命令中的[mylogin.key]和[loginKey],login.jar就是你的jar文件名。
命令运行后,会要求你输入前面设置的密码。
[img]http://stephen830.iteye.com/upload/attachment/43285/64ad049d-7e21-3b67-a501-390493ae5d85.png[/img]

这样,你的jar文件就经过数字签名了。然后把签名后的jar文件复制到你的前台展示html页面的同一个目录下,将applet嵌在html页面中。

<applet archive="login.jar" code="com.soft4j.Login.class" codebase="." name=Login style="HEIGHT: 40px; WIDTH: 500px; border:1px solid #CCC;" ></applet>


当客户打开你的html页面时会弹出一个要求接受数字签名证书的对话框。

(2)如何访问本地的windows注册表?
applet访问本地windows注册表是有限制的,只能对[HKEY_CURRENT_USER\Software\JavaSoft\Prefs]下面的进行操作。其余的一概不允许操作。


package com.soft4j;
import java.applet.Applet;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.prefs.Preferences;

import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/*
* Created on 2006-3-20
* Author stephen
* Email zhoujianqiang AT gmail DOT com
* CopyRight(C)2005-2008 , All rights reserved.
*/

/**
*
* @author stephen

* @version 1.0.0
*
*/
public class Login extends Applet implements ActionListener{
/*
* applet 签名步骤:
* 1.生成jar文件
* jar命令
* 2.生成key
* keytool -genkey -alias key名字 -keystore key文件名

* 3.签名jar文件
* jarsigner -keystore key文件名 login.jar key名字
*/

/**
* 构造方法

*/
public Login() {
super();
}

JTextField userName = null;
JPasswordField userPass = null;
String key = null;
Label l1, l2;
JButton button = null;

/**
* 初始化

*/
public void init()
{

l1 = new Label("姓名:");
l2 = new Label("密码:");
userName = new JTextField(10);
userPass = new JPasswordField(10);
button = new JButton("登录");
add(l1);
add(userName);
add(l2);
add(userPass);
add(button);
userName.addActionListener(this);
userPass.addActionListener(this);
button.addActionListener(this);
}

/**
* 动作事件
*/
public void actionPerformed(ActionEvent e) {
URL url = null;
StringBuffer theUrl = new StringBuffer();
try {
Preferences prefsdemo = Preferences.userRoot().node("/fileds");//读取数据 HKEY_CURRENT_USER\Software\JavaSoft\Prefs\fileds
key = prefsdemo.get("key1",null);//读取数据 HKEY_CURRENT_USER\Software\JavaSoft\Prefs\fileds下的key为key1的值
if(this.getParameter("url")==null || "".equals(this.getParameter("url"))){
URL srcUrl = null;
srcUrl = this.getDocumentBase();
theUrl.append(srcUrl.getProtocol());
theUrl.append("://");
theUrl.append(srcUrl.getAuthority());
String path = srcUrl.getPath();
int len = path.lastIndexOf("/");
path= path.substring(0,len+1);
theUrl.append(path);
}else{
theUrl.append(this.getParameter("url"));
}
theUrl.append("l");//其中l是WEB应用中设置的servlet
theUrl.append("?l=");
theUrl.append(userName.getText());
theUrl.append(",");
theUrl.append(userPass.getPassword());
theUrl.append(",");
theUrl.append(key);
url = new URL(theUrl.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
this.getAppletContext().showDocument(url);
}

}


把applet、数字证书签名、访问本地windows注册表结合起来,把用户的登录和注册表结合起来,进行双重认证,相对更加安全一点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值