How to programmatically grant AllPermissions
to an RMI application without using policy file?
UPDATE:
After some researching, I have written this custom Policy Class and installed it via Policy.setPolicy(new MyPolicy())
.
Now I get the following error:
invalid permission: (java.io.FilePermission \C:\eclipse\plugins\org.eclipse.osgi_3.7.0.v20110613.jar read
class MyPolicy extends Policy {
@Override
public PermissionCollection getPermissions(CodeSource codesource) {
return (new AllPermission()).newPermissionCollection();
}
}
Based on @EJP's advice, I have debugged using
-Djava.security.debug=access
and found all the needed permissions in a policy file :But because I didn't want to create a policy file, I found a way to replicate this programmatically by extending
java.security.Policy
class and setting the policy at the startup of my application usingPolicy.setPolicy(new MinimalPolicy());
Don't install the SecurityManager. You only need it if you're using the codebase feature, and if you need that you need a proper .policy file,
Short solution
Extend your updated solution to:
Consider, that Policy.getPermissions() must always return a mutable PermissionCollection
This solution works already, since it adds an AllPermission object into every call of the
Policy.getPermissions(ProtectionDomain)
, that refers toPolicy.getPermissions(CodeSource)
.Clean solution
But there is a cleaner solution, that doesn't track any unnecessary other Permissions, since AllPermissions allows pretty everything already.
Because your
is treated by Java as immutable (why add permissions to a collection that already allows all permissions?), and because Java will try to add permissions to the collection. That's where the error message comes from - Java tried to add a java.io.FilePermission to your AllPermission.
Instead, do this: