Is there a legal way to add/remove permissions to Java security policy at runtime?
From 1.4 dynamic [ProtectionDomain
][1]s can delegate to the Policy
. Dynamically removing permissions from code is unlikely to make any sense. The two argument forms of [AccessController.doPrivileged
][3] might also be useful.
[1]: http://java.sun.com/javase/6/docs/api/java/security/ProtectionDomain.html#ProtectionDomain(java.security.CodeSource, java.security.PermissionCollection, java.lang.ClassLoader, java.security.Principal[]) [3]: http://java.sun.com/javase/6/docs/api/java/security/AccessController.html#doPrivileged(java.security.PrivilegedAction, java.security.AccessControlContext)
Javadoc says that Policy.refresh()
for file-based policy would re-read the file. Thus, it is possible to modify system-wide policy at runtime by editing policy file and then calling Policy.refresh()
It is possible to set custom Policy implementation, using Policy.setPolicy()
method. For example see JAAS in Action book.