Applet java.io.FilePermission异常

时间:2013-09-11 19:53:38

标签: java javascript security applet sandbox

我有一个内部网站点的Java小程序,需要访问客户端PC上的文件系统(特别是LAN共享)。这个小程序的功能类似于:

JSObject win;

public void init() {
    win=(JSObject) JSObject.getWindow(this);
    win.call("appletMsg", new Object[] {"<b>Applet Loaded</b>", "win"});
}

public void saveFile(String filepath, String filename) {
    File theDir = new File(filepath);
    try {
        if (theDir.exists()) { // This throws exception
            win.call("appletMsg", new Object[] {"Directory Exists", "win"});
        }
        else {
            win.call("appletMsg", new Object[] {"Creating Directory...", "msg"});
            if (theDir.mkdir()) {
                win.call("appletMsg", new Object[] {"Directory Created", "win"});
            }
            else win.call("appletMsg", new Object[] {"Directory Creation Failed!", "fail"});
        }
    }
    catch (Exception e) { // This exception is caught
        win.call("appletMsg", new Object[] {"Error Reading Directory!", "fail"});
        win.call("appletMsg", new Object[] {filepath, "fail"});
    }
    // More code for working with files, error happens above this
}

applet背后的Javascript

// call applet method
function save() {
    document.myApplet.saveFile('\\\\LOCATION\\DIR\\DIR\\', 'test.txt');
}

// output responses from applet to div
function appletMsg(response, type) {
    document.getElementById('output').innerHTML+='<br><span class='+type+'>'+response+'</span>';
}

故障排除/思想:

  • 在我调用Java方法之前,applet工作正常 (参数在param列表中,applet完全重新加载时 需要的,文件操作是在init()方法,恢复到这个工作,但是很糟糕的做法)
  • 小程序使用自我证书
  • 签名
  • 我在init()方法中放了一个JFileChooser来确保路径 对,将其移动到saveFile()方法,对话框没有 节目。它没有像使用它那样导致JS错误显示 .exists()在Java中调用,它确实在一个文件中抛出一个FilePermission异常 尝试/捕获
  • 因为这适用于init()而不是saveFile(),我只能假设 这是为了防止JavaScript本身访问文件系统??

1 个答案:

答案 0 :(得分:2)

为了能够从JavaScript调用您的applet函数,您必须使用访问控制器。 请参阅documentation

所以试试:

public void saveFile(String filepath, String filename) {
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            File theDir = new File(filepath);
            try {
                if (theDir.exists()) { // This throws exception
                    win.call("appletMsg", new Object[] { "Directory Exists", "win" });
                } else {
                    win.call("appletMsg", new Object[] { "Creating Directory...", "msg" });
                    if (theDir.mkdir()) {
                        win.call("appletMsg", new Object[] { "Directory Created", "win" });
                    } else
                        win.call("appletMsg", new Object[] { "Directory Creation Failed!", "fail" });
                }
            } catch (Exception e) { // This exception is caught
                win.call("appletMsg", new Object[] { "Error Reading Directory!", "fail" });
                win.call("appletMsg", new Object[] { filepath, "fail" });
            }
            // More code for working with files, error happens above this
        }
    });
}

即使使用自签名证书也可以,您只需获得安全警告。

始终记住使特权代码部分尽可能小。