巧妙解决:access denied (javax.management.MBeanTrust...

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

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

permission javax.management.MBeanTrustPermission "register"

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

源代码如下:

  Policy.setPolicy(new AWSPolicy()); // 启动方法里设置Policy

  private static class AWSPolicy extends Policy {
    private final Policy defaultPolicy;


    public AWSPolicy() {
      super();


      defaultPolicy = Policy.getPolicy();
    }


    @Override
    public boolean implies(ProtectionDomain domain, Permission permission) {
      if (permission instanceof javax.management.MBeanTrustPermission) {
        return true;
      } else {
        return defaultPolicy.implies(domain, permission);
      }
    }
  }

 
 
 
 
 
  
  
  
  

你可能感兴趣的:(java,oracle,jdbc,policy)