java - 小程序 java.io.FilePermission 异常

标签 java javascript security applet sandbox

我有一个用于 Intranet 站点的 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
}

小程序背后的 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>';
}

疑难解答/想法:

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

最佳答案

为了能够从 JavaScript 调用小程序函数,您必须使用访问 Controller 。 请参阅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
        }
    });
}

即使使用自签名证书它也可以工作,您只会收到安全警告。

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

关于java - 小程序 java.io.FilePermission 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18750069/

相关文章:

java - 嵌套 onClick 监听器

javascript - 如何从 Node.js 客户端访问 Realm 移动平台?

javascript - Google Chart 显示空图

c# - 使业务层的方法安全。最佳实践/最佳模式

php - session_start()、ob_start() 和安全问题

ios - 当应用程序未激活时如何实现隐私屏幕?

java - 使用 SMTP 和 javax.mail 时出现问题

java - 从模型访问 playframework 2.1 中的配置

javascript - 单击即可更改背景图像

java - 从 java 方法返回 int 值