【问题标题】:Why is there a NPE on a missing Java Permission? And how to find out which Permission?为什么缺少 Java 权限会有 NPE?以及如何找出哪个权限?
【发布时间】:2014-11-10 04:05:15
【问题描述】:

我有一个 Java Applet,当 permission java.security.AllPermission;java.policy 文件中被授予时,它运行良好。

但我真的只想为我的应用程序设置一个权限 - 所以我删除了策略文件中的 permission java.security.AllPermission; 行,期待 Access Denied 异常或类似的东西,但是我得到的只是以下代码中的 NullPointerException:

protected Object[] __ObjectArrayResult;

(...)

final Container c = (Container) container;

        InvocationWrapper.invokeAndWait(new Runnable() {
            public void run() {
                __ObjectArrayResult = c.getComponents();
            }
        });

        Object[] components = __ObjectArrayResult;

        Vector componentVector = Utils.convertArrayToVector(components);

我在这里尝试的是获取 AWT 容器的所有组件,但 c.getComponents(); 返回 Null,Utils.convertArrayToVector(components); 抛出 NPE。

那么为什么 getComponents 方法(如果它需要一些特殊的权限)不抛出异常而只返回 Null?我查看了文档,但没有提到任何情况,这将返回 Null。


我尝试的下一件事是调试权限。使用这种方法,我发现了这一点: http://docs.oracle.com/javase/7/docs/technotes/guides/security/troubleshooting-security.html

其中声明-Djava.security.debug=access 可用于查看AccessController.checkPermission 的所有结果

但运行它只给了我以下信息:

access: access denied ("java.security.AllPermission" "<all permissions>" "<all actions>")
access: access denied ("java.security.AllPermission" "<all permissions>" "<all actions>")
access: access denied ("java.security.AllPermission" "<all permissions>" "<all actions>")
access: access denied ("java.security.AllPermission" "<all permissions>" "<all actions>")
access: access denied ("java.security.AllPermission" "<all permissions>" "<all actions>")
access: access allowed ("java.util.PropertyPermission" "line.separator" "read")
access: access denied ("java.security.AllPermission" "<all permissions>" "<all actions>")
Exception in thread "Thread-19" access: access allowed ("java.lang.RuntimePermission" "modifyThread")
access: access allowed ("java.io.SerializablePermission" "enableSubstitution")
java.lang.NullPointerException: Utils::convertArrayToVector - Array is NULL!

我不知道该怎么做。它说AllPermissions 被拒绝,这是否意味着某些方法向AccessController 询问所有权限?还是通过首先尝试获得更一般的权限allPermission 来尝试获得"java.lang.RuntimePermission" "modifyThread"


总结一下:为什么getComponents() 返回Null?为什么它不抛出异常并告诉我需要哪个权限?为什么需要AllPermission 而不是更具体的权限?

谢谢!

【问题讨论】:

    标签: java security permissions applet event-dispatch-thread


    【解决方案1】:

    修复您对 EDT 的调用,以免吞下异常:

        final Container c = (Container) container;
        final AtomicReference<Object[]> result = new AtomicReference<>();
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    result.set(c.getComponents());
                }
            });
        } catch (Exception e) {
             throw new RuntimeException(e.getMessage(), e);
        }
        Object[] components = result.get();
        Vector componentVector = Utils.convertArrayToVector(components);
    

    您现在可以查看您的代码在哪里丢失了权限。

    您还注意到,在 Runnable 和调用线程之间交换值是使用 local 引用(结果)安全地完成的。通过实例成员执行此操作是非常糟糕的风格,如果在多个线程可以同时执行可运行对象的上下文中使用,则会彻底破坏。

    值得注意的是,Vector 几乎从不您想要使用(因为 Vector 是同步的)。当您不需要同步访问时切换到 ArrayList(您几乎从不需要它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2021-12-31
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多