How to enforce reflection security by not allow the Method, Field, Constructor object to call setAccessible(true) ? SecurityPolicy File or something else?
Normally for stand-alone Java applications there is no SecurityManager registered.
I using this System.setSecurityManager(new SecurityManager());
This approach will work for calling methods.
I would like to enforce the whole jar or client code that uses the jar is not allow to call setAccessible(true);
Any better approach ?
Thanks.
Um, it does work for setAccessible. See:
and
Results in
One reason it might’ve not worked for you is that according to comments in this post it didn’t use to work in Java 1.5, but works in 6 and thereafter.
Edit: to deny it for specific jars, you need to either use a policy file, example:
There’s two ways of specifying the policy file, either give it as additions to default, or give only those that are specified (source):
…or implement a custom security manager, which doesn’t look that hard. Haven’t done that myself though.