当使用Oracle的JDBC驱动连接数据库时,会出现这种错误:access denied (javax.management.MBeanTrustPermission register)

    一般的解决办法是修改{JAVA_HOME}\jre\lib\security\java.policy文件,添加一行:

    permission javax.management.MBeanTrustPermission “register”

    但是这种方法很不方便,我经过实践,其实还有一种简便的方法,就是实现一个自己的Policy:

    源代码如下:

    1. Policy.setPolicy(new AWSPolicy()); // 启动方法里设置Policy
    2. private static class AWSPolicy extends Policy {
    3. private final Policy defaultPolicy;
    4. public AWSPolicy() {
    5. super();
    6. defaultPolicy = Policy.getPolicy();
    7. }
    8. @Override
    9. public boolean implies(ProtectionDomain domain, Permission permission) {
    10. if (permission instanceof javax.management.MBeanTrustPermission) {
    11. return true;
    12. } else {
    13. return defaultPolicy.implies(domain, permission);
    14. }
    15. }
    16. }