浏览 1176 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-01-29
这几天上网查了好多资料如何突破java安全沙箱限制,使用了数字签名仍然不行,万不得以,采用如下方法解决,呵呵:
第一步:编写在本地文件用户目录写java.policy的工具类
package com.northking;
import java.io.File;
import java.io.FileWriter;
/**
* 处理java安全以突破java安全沙箱限制
* <p>Title: </p>
* <p>Description: 影像查询socket客户端</p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: 北京京北方科技股份有限公司</p>
* @author 毛昌明
* @version 1.0
*/
public class JavaPolicyUtil {
public JavaPolicyUtil() {
}
/**
* 检查JAVA 安全是否自动到位
* @return boolean
*/
public static boolean checkJavaPolicy() {
//处理系统安全
String sysDir = System.getProperty("deployment.system.home");
System.out.println("sysDir:" + sysDir);
boolean isSysBoot = checkPolicyDir(sysDir);
String userDir = System.getProperty("deployment.user.home");
System.out.println("userDir:" + userDir);
boolean isUserBoot = checkPolicyDir(userDir);
if (isSysBoot || isUserBoot) {
System.out.println("您的系统作首次初始化操作成功,如果无法正常工作,请关闭浏览器并重新打开重试。");
}
return true; //文件已存在,
}
private static boolean checkPolicyDir(String sDir) {
boolean result = false;
String vers = System.getProperty("java.version");
try {
if (vers.compareTo("1.4") < 0) {
//处理JRE或JDK低于1.4的安全性
String path = System.getProperty("java.home");
String securityFilePath = path + "/" + "lib/security";
System.out.println("securityFilePath:" + securityFilePath);
String policyFile = securityFilePath + "/java.policy";
if (checkFile(policyFile) == 0) { //文件存在
File f = new File(policyFile);
f.delete();
System.out.println("Original Policy file deleted!");
writePolicyFile(policyFile);
}
else {
writePolicyFile(policyFile);
}
}
else { //处理JDK1.4的安全,原有的处理方法。
String policyPath;
String policyFile;
if (sDir != null) {
policyPath = sDir + "/security";
policyFile = policyPath + "/java.policy";
if (checkFile(policyFile) == 0) { //文件存在
return false;
}
else {
createDir(policyPath);
result = writePolicyFile(policyFile);
}
}
return result;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/**
* control the java application access JVM permittion
* @param policyFile String
* @return boolean
*/
private static boolean writePolicyFile(String policyFile) {
try {
FileWriter fout = new FileWriter(policyFile);
fout.write("grant { \r");
fout.write(" permission java.security.AllPermission; \r");
fout.write(" }; \r");
fout.flush();
fout.close();
System.out.println("policy Write OK:" + policyFile);
return true;
}
catch (Exception ex) {
System.err.println("write policy file is error:" + ex.getMessage());
return false;
}
}
/**
* 创建指定路径的文件夹
* @param _path String
*/
private static void createDir(String _path) {
try {
File f = new File(_path); //create directory each level by level
if (!f.exists()) {
f.mkdirs();
}
System.out.println("Create Direcory:" + _path);
}
catch (Exception ex) {
System.err.println("Create Direcory Error: " + ex.getMessage());
}
}
/**
* 检查一个是否为文件或子目录
* @param filename String
* @return int 0: file, 1:directory,-1:no exits
*/
private static int checkFile(String filename) {
File f = new File(filename);
if (f.isFile()) {
return 0;
}
else if (f.isDirectory()) {
return 1;
}
else {
return -1;
}
}
}
第二步:在applet 的init方法中调用:
public void init(){
System.out.println("socket transfer client version:(" + version + ")");
JavaPolicyUtil.checkJavaPolicy();
}
第三步:进行数字签名,我所用的语句如下: jar cvf PicTransApplet.jar com/ echo keytool -genkey -alias mykey -keyalg RSA -validity 2000 -keysize 1024 -keypass 123456 -keystore mykey.store -storepass 123456 jarsigner -keystore mykey.store -storepass 123456 -keypass 123456 PicTransApplet.jar mykey pause 当然这种方法并不是最好的,但是项目实在太紧,就先用它顶着啦,呵呵;听说还有其它方法,就是说在init方法中启动一个线程一直进行业务监听,因为applet对于其内部实现是不受安全沙箱限制的(我的applet是要在页面上通过javascript调用的哟),但是我不是很懂,如果其它朋友有此经验,请分享,谢谢。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-01-30
你没有理解Applet的安全模型,看看这儿的文档就明白了:
http://java.sun.com/javase/6/docs/technotes/guides/plugin/developer_guide/security.html 三条原则: 引用 * All unsigned applets are run under the standard applet security model.
* If usePolicy is not defined in the java.policy file, then a signed applet has the AllPermission permission only if Java Plug-in can verify the signers, and the user agrees to granting the AllPermission permission when prompted. * If usePolicy is defined, then a signed applet has only the permissions defined in java.policy and no prompting occurs. |
|
| 返回顶楼 | |
|
时间:2008-01-30
二楼说的是正确的。对于签名的applet,我想你需要的是使用 AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
//you unsafe code
return null;
}
});
|
|
| 返回顶楼 | |
|
时间:2008-01-30
既然把权限放开了,还需要进行签名?
|
|
| 返回顶楼 | |
|
时间:2008-05-19
這個與ActiveX控件是否一樣原理???
|
|
| 返回顶楼 | |








