0
運行以下代碼,getPackage()行出現StackOverflowError錯誤。 如果我不能訪問getPackage()來檢查包,我該如何授予對包內類的權限?SecurityManager StackOverflowError
package myPkg.security;
import java.security.Permission;
import javax.swing.JOptionPane;
public class SimpleSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
Class<?>[] contextArray = getClassContext();
for (Class<?> c : contextArray) {
checkPermission(perm, c);
}
}
@Override
public void checkPermission(Permission perm, Object context) {
if (context instanceof Class) {
Class clazz = (Class) context;
Package pkg = clazz.getPackage(); // StackOverflowError
String name = pkg.getName();
if (name.startsWith("java.")) {
// permission granted
return;
}
if (name.startsWith("sun.")) {
// permission granted
return;
}
if (name.startsWith("myPkg.")) {
// permission granted
return;
}
}
// permission denied
throw new SecurityException("Permission denied for " + context);
}
public static void main(String[] args) {
System.setSecurityManager(new SimpleSecurityManager());
JOptionPane.showMessageDialog(null, "test");
}
}
我將添加取決於封裝 – 2010-03-24 19:46:00
更多的代碼看起來和OP正試圖將權限授予這些特定的軟件包名稱。當然,如果這是你的政策,你不希望攻擊者在這些包中插入類(!)。你可能不想在'package.access'下面放置'java。*'(儘管如果你想提供你自己的API,不需要Object或String)。 – 2010-03-24 20:44:24