仅将Java SecurityManager设置为仅一种方法 [英] Setting the Java SecurityManager for one method only

查看:239
本文介绍了仅将Java SecurityManager设置为仅一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法A,看起来可能如下:

I have a method A which might look like following:

public double A{
  if (secM == null) {
    secM = new SecurityManager();
    System.setSecurityManager(secM);
  }
  //do something and return a double

}

问题在于,一旦设置了SecurityManager,它将用于整个项目,但是我只需要将其用于此方法所在的类即可. 如何告诉SecurityManager仅验证此方法/类的权限?

The problem is that once the SecurityManager is set, it is for the entire project, but I only need it to be for the class this method is in. How can I tell the SecurityManager to only verify the permissions for this method/class?

推荐答案

您希望SecurityManager阻止哪些操作,并且允许哪些操作? SecurityManager更常用于通过 any 方法控制特定动作,而不是通过单个方法来控制动作.

What actions do you want the SecurityManager to prevent, and which do you want to allow? SecurityManager is more often used to control particular actions by any method, rather than actions by a single method.

但是,它也可以通过创建用于检查调用堆栈的自定义SecurityManager来实现后者-请参见此答案例如-这是您需要的吗?

However, it can do the latter too, by creating a custom SecurityManager that examines the call stack - see this answer for an example - is this what you need?

您可以为系统范围的SecurityManager提供针对您的应用程序量身定制的策略,因此您可以允许大多数操作,但可以防止执行少量操作.您可以控制的权限在此处列出

You can provide the system-wide SecurityManager with a policy tailored to your application, so you can permit most things but prevent a small set of actions. The permissions you can control are listed here.

更新:通过将方法拉入一个单独的类(可以由一个不同的类加载器单独加载),您可以可以更优雅地执行此操作.你的其他班级.请参见此示例.然后,您可以使用类加载器进行简单检查,而不是检查完整的堆栈跟踪.但是,我对这种方法不熟悉-如果来自两个单独的类加载器的类需要交互,则可能会有含义...

Update: you might be able to do this more elegantly by pulling your method out into a separate class, that can be loaded separately (by a different classloader) than your other classes. See this example. Then you can do a trivial check with the classloader rather than checking the full stack trace. However, I'm not familiar with this method - there may be implications if classes from the two separate classloaders need to interact...

这篇关于仅将Java SecurityManager设置为仅一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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