Java:在自定义安全管理器类中,是否有API来查找哪个类需要权限?

Java:在自定义安全管理器类中,是否有API来查找哪个类需要权限?,java,securitymanager,Java,Securitymanager,我试图在自定义安全管理器中找到哪个类动态请求了权限。我找不到任何API来帮助我获取调用类的代码库位置。下面是我想做的 我有一个testApp类试图写入文件 package test.ProfilingSecurityManager; import java.io.PrintWriter; public class TestApp { public static void main(String [] args) throws Exception { System.setSecurityM

我试图在自定义安全管理器中找到哪个类动态请求了权限。我找不到任何API来帮助我获取调用类的代码库位置。下面是我想做的

我有一个testApp类试图写入文件

package test.ProfilingSecurityManager;
import java.io.PrintWriter;
public class TestApp {
public static void main(String [] args) throws Exception {
    System.setSecurityManager(new NewProfilingSecurityManger());
    PrintWriter writer = new PrintWriter("profile.txt");
    writer.println("Test line");
    writer.close();
}
}
自定义安全管理器中的over ridden方法如下所示

public void checkPermission(final Permission permission) {
  try {
  // see what the parent security manager code says
  super.checkPermission(permission);
  } 
  catch (Exception e) {
   // find the code base which requested this permission
   // I can get the call stack here
   Class [] sourceClasses = getClassContext();
   Class invokingClass = sourceClasses[sourceClasses.length - 1];
   // I can also get the accesscontrol context here  
   // using -AccessController.getContext()
   // How do i find the codebase location of the class 
   // which needed this permission here
 }
}
我需要在checkPermission方法中引发异常时找到TestApp的代码库位置。 有人能帮我解决这个问题吗


如果您调用invokingClass.getProtectionDomain().getCodeSource(),这将告诉您从何处加载该类,谢谢


但是,当访问检查失败时,这只会告诉您哪个类位于调用堆栈的底部。在您给出的示例中,这将是TestApp,但如果您试图调试安全权限问题,更好的方法是运行Java时将
Java.security.debug
系统属性设置为
access,failure
。当权限检查失败时,这将告诉您哪个类没有权限,它从何处加载,以及它有哪些权限。

您检查过nio Pageage吗?否。这对我试图实现的目标有帮助吗?不,没有。NIO与安全管理器无关@KickButtowski的评论毫无意义。