我试图禁止在 AccessController.doPriviliged() 方法中创建线程。下面的方法创建并运行线程。我用-Djava.security.manager 运行它。根据这个链接,如果 modifyThreadGroup 没有被授予,那么线程创建应该被禁止?
http://docs.oracle.com/javase/7/docs/technotes/guides/security/permissions.html
谁能告诉我为什么会发生这种情况以及禁止使用 AccessController 创建线程的正确方法?
// .java.policy in $UserHome:
grant codeBase "file:/C:/-" {
permission java.security.AllPermission;
};
public class ThreadTest {
public void testModifyThreadGroup() {
// grant no permissions
Permissions perms = new Permissions();
ProtectionDomain domain = new ProtectionDomain(
new CodeSource( null, (Certificate[]) null ), perms );
AccessControlContext _accessControlContext = new AccessControlContext(
new ProtectionDomain[] { domain } );
try {
AccessController.doPrivileged(new PrivilegedExceptionAction(){
@Override
public Object run() {
try {
// modifyThreadGroup not granted, so should not be able
// to call Thread constructor???
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread.run()");
}
});
t.run();
} catch (Exception ex) {
System.out.println("Error running thread: " + ex);
}
return null;
}}, _accessControlContext);
} catch(Exception e) {
System.out.println("Access Error running doPrivileged: " + e);
}
}
}