简体   繁体   English

Java Reflection权限错误

[英]Java Reflection permission error

I'm trying to load a class via an URLClassLoader (well, it neither works with an normal class loader) and want them to not have any permission. 我正在尝试通过URLClassLoader加载一个类(好吧,它既不适用于普通的类加载器),也希望它们没有任何权限。

Therefore, i created my own security manager, which genereates a key on startup, which can only by requested once (in main thread). 因此,我创建了自己的安全管理器,它在启动时创建了一个密钥,只能通过请求一次(在主线程中)。 The security manager has 2 lists, the applicationThread, which will be granted any right and the temporaryList, which will be granted one right just once (it's about the reflection). 安全管理器有两个列表,即applicationThread,它们将被授予任何权限和temporaryList,它们只被授予一次权限(它是关于反射)。

As it is very hard to descripe, i decided to upload the whole thing: look at the link below 由于很难描述,我决定上传整个内容: 请看下面的链接

Ok, coming back: I created a WatchDog thread, which checks if the thread doesn't take too much time. 好的,回来:我创建了一个WatchDog线程,它检查线程是否不会占用太多时间。

When i now start to instance two classes from an URLClassLoader, I call exactly 30 methods without getting any errors, but on the 31st call, it tries to check Permissions for the following but this is just happaning after the 30th call. 当我现在开始从URLClassLoader实例化两个类时,我调用了30个方法而没有出现任何错误,但是在第31次调用时,它尝试检查以下的权限,但这只是在第30次调用之后感到满意。

java.lang.RuntimePermission accessClassInPackage.sun.reflect),

Does anyone know what's going on there? 有谁知道那里发生了什么?

edit: I had time to strip down the example. 编辑:我有时间去除这个例子。 http://myxcode.at/securitymanager.zip I found out, that the SecurityManager is not asked synchronous. http://myxcode.at/securitymanager.zip我发现,SecurityManager不会被要求同步。 Just run this small piece of code and have a look at the red lines. 只需运行这一小段代码,看看红线。

If the red lines come in the very first line, just run the program again, you will find out that it seems a little bit uncontrolled. 如果红线出现在第一行,只需再次运行程序,您会发现它似乎有点不受控制。

The problem more or less is, that i need the security manager to be synchronized. 或多或少的问题是,我需要同步安全管理器。 Here is my output for those who cannot face the error(bug?) http://pastebin.com/E9yLRLif 以下是那些无法面对错误的人的输出(bug?) http://pastebin.com/E9yLRLif

edit2: maybe its about the console? edit2:也许是关于控制台的? maybe the console is too slow? 也许控制台太慢了?

For me the check occurs when i=15 : 对我来说,当i=15时检查发生:

checkPermission ( (java.lang.RuntimePermission accessClassInPackage.sun.reflect) ) for Thread[main,5,main] 线程的checkPermission((java.lang.RuntimePermission accessClassInPackage.sun.reflect))[main,5,main]

The reason for the delayed permission check is an inflationThreshold of the ReflectionFactory class which is used by the invoke method of NativeMethodAccessorImpl.java : 延迟权限检查的原因是ReflectionFactory类的inflationThreshold ,它由NativeMethodAccessorImpl.javainvoke方法使用:

public Object invoke(Object obj, Object[] args)
        throws IllegalArgumentException, InvocationTargetException {
    if (++numInvocations > ReflectionFactory.inflationThreshold()) {
        MethodAccessorImpl acc = (MethodAccessorImpl) new MethodAccessorGenerator()
                .generateMethod(method.getDeclaringClass(), method
                        .getName(), method.getParameterTypes(),
                        method.getReturnType(), method
                                .getExceptionTypes(), method
                                .getModifiers());
        parent.setDelegate(acc);
    }

    return invoke0(method, obj, args);
}

To disable the delay you could use Reflection API :) 要禁用延迟,您可以使用Reflection API :)

Field hack = Class.forName("sun.reflect.ReflectionFactory").getDeclaredField("inflationThreshold");
hack.setAccessible(true);
hack.set(null, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM