从签名的applet列出本地目录

时间:2009-06-08 03:39:17

标签: java permissions applet

将以下applet编译并打包到jar中,然后使用自签名证书进行签名。

import java.applet.Applet;
import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;

public class Test extends Applet {
        private static final long serialVersionUID = -3127185193501384816L;

        private final class DirectoryLister implements PrivilegedAction<File[]> {
                private final String attachmentsFolder;

                private DirectoryLister(String attachmentsFolder) {
                        this.attachmentsFolder = attachmentsFolder;
                }

                public File[] run() {
                        return new File(attachmentsFolder).listFiles();
                }
        }

        public File[] getFiles() throws PrivilegedActionException {
                String attachmentsFolder = getParameter("attachmentsFolder");

                if (attachmentsFolder != null) {
                        return AccessController.doPrivileged(new DirectoryLister(
                                        attachmentsFolder));
                }

                return null;
        }
}

Applet实例化如下:

<applet id="applet"
    code="Test"
    archive="applet.jar">
    <param name="attachmentsFolder"
        value="c:/test" />
</applet>

Applet的用法如下:

var files = applet.getFiles();

for (var file in files) {
    // Do something to file.
}

ff。遇到错误:

java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.plugin.liveconnect.SecureInvocation$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
        at sun.plugin.liveconnect.PrivilegedCallMethodAction.run(Unknown Source)
        ... 4 more
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission c:\test read)
        at java.security.AccessControlContext.checkPermission(Unknown Source)
        at java.security.AccessController.checkPermission(Unknown Source)
        at java.lang.SecurityManager.checkPermission(Unknown Source)
        at java.lang.SecurityManager.checkRead(Unknown Source)
        at java.io.File.list(Unknown Source)
        at java.io.File.listFiles(Unknown Source)
        at Test$DirectoryLister.run(Test.java:20)
        at Test$DirectoryLister.run(Test.java:1)
        at java.security.AccessController.doPrivileged(Native Method)
        at Test.getFiles(Test.java:28)
        ... 14 more

这让我想问我是否仍然需要在Java主目录的其中一个配置文件中明确授予perms来读取c:/ test?如果是这样,有人能指点我如何做到这一点吗?

1 个答案:

答案 0 :(得分:0)

显然是缓存问题。清除所有缓存并重新构建/重新部署解决了这个问题。即,不需要额外的perms设置。