禁用当前线程的Java反射 [英] Disable Java reflection for the current thread

查看:113
本文介绍了禁用当前线程的Java反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一些半可靠的Java代码,并希望禁用在代码执行期间使用反射的功能。

I need to call some semi-trustworthy Java code and want to disable the ability to use reflection for the duration of that code's execution.

try{
   // disable reflection somehow
   someObject.method();
}
finally{
   // enable reflection again
}

可以使用SecurityManager完成,如果是,怎么做?

Can this be done with a SecurityManager, and if so, how?

澄清/背景:这是另一个问题关于限制可以从JavaScript / Rhino调用的包。接受的答案引用了关于如何做到这一点的博客条目,它需要两个步骤,第一个使用Rhino API(ClassShutter),第二个关闭反射和Class.forName()。我想我可以使用SecurityManager更干净地完成第二步(了解SecurityManager,正如已经指出的那样,沿途是一个复杂的野兽)。

Clarification/Context: This is a follow-up to another question about restricting the packages that can be called from JavaScript/Rhino. The accepted answer references a blog entry on how to do that, and it requires two steps, the first one using a Rhino API (ClassShutter), the second one turning off reflection and Class.forName(). I was thinking I can do that second step more cleanly using a SecurityManager (learning about SecurityManager, which as has been pointed out, is a complex beast, along the way).

总而言之,我希望(来自代码,而不是设置文件)关闭Class.forName()以及对整个反射包的任何访问。

To sum up, I want (from code, not setting file) to turn off Class.forName() and any access to the whole reflection package.

推荐答案

这取决于你想要限制的内容。

It depends on what you are trying to restrict.

通常,可公开访问的API不受限制。但是,只要您不授予不值得信任的代码 ReflectPermission(suppressAccessChecks) 权限,它将无法访问另一个包中的非公共API。

In general, publicly accessible API is not restricted. However, as long as you don't grant the untrustworthy code the ReflectPermission("suppressAccessChecks") permission, it won't be able to get access to non-public API in another package.

如果您有要限制所有访问权限的软件包列表,则有两个步骤。首先,在安全性属性中, package.access 列表中包含受限制的包。然后提供可信代码 RuntimePermission(accessClassInPackage。+ pkg)

If you have a list of packages to which you want to restrict all access, there are two steps. First, in the Security properties, include the restricted package in the package.access list. Then give your trusted code RuntimePermission("accessClassInPackage." + pkg).

区分不受信任代码的常用方法是从其他位置加载它,并在授予权限时引用策略文件中的不同代码库。

A common way to distinguish your untrusted code is to load it from a different location, and refer to the different codebases in your policy file when granting permissions.

Java安全体系结构非常强大,但我知道它也很复杂;如果你想要一个更具体的例子,请准确描述你想要限制的电话,我会更加明确。

The Java security architecture is very powerful, but I know it is also complicated; if you would like a more concrete example, please describe exactly what calls you want to restrict and I'll try to be more explicit.

在不修改 java.policy 文件和/或 java.security 文件的情况下执行所需操作会很困难,也许是不可能的。 java.security.Policy 表示 java.policy 中的信息,但它不提供写访问权限。您可以创建自己的 Policy 实现,并在运行时安装它,只要任何现有的 SecurityManager 允许它。

To do what you want without modifying the java.policy file and/or the java.security file would be very difficult, maybe impossible. The java.security.Policy represents the information in java.policy, but it doesn't offer write access. You could create your own Policy implementation and install it at runtime as long as any existing SecurityManager permits it.

另一方面,您可以将自定义java.policy文件指定为命令行选项。如果您使用某种启动器提供完整的应用程序,则可能很容易实现。它还为您的用户提供了一些透明度。复杂的用户可以查看您希望授予应用程序的权限。

On the other hand, you can specify a custom java.policy file as a command-line option. If you are providing a complete application with some sort of launcher, that might be easily accomplished. It also provides some transparency to your users. A sophisticated user can review the permissions you'd like to have granted to the application.

这篇关于禁用当前线程的Java反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆