如何在运行 Java 应用程序时修复 Keberos 身份验证提示



我写了一个Java应用程序来连接hive-metastore。它工作正常,但是当我在 Linux 上运行 jar 文件时,它会要求输入 Kerberos 用户名和密码.我已经在代码中指定了 Kerberos 主体和密钥表文件。而且我不想使用额外的jass.config文件。有什么办法可以解决这个问题吗?

这是我的源代码

    HiveConf conf = new HiveConf();
    MetastoreConf.setVar(conf, ConfVars.THRIFT_URIS, "thrift://HOSTNAME:9083");
    MetastoreConf.setBoolVar(conf, ConfVars.USE_THRIFT_SASL, true);
    MetastoreConf.setVar(conf, ConfVars.KERBEROS_PRINCIPAL, "hive/HOSTNAME@example.com");
    MetastoreConf.setVar(conf, ConfVars.KERBEROS_KEYTAB_FILE, "hive.keytab");
    System.setProperty("java.security.krb5.realm", "EXAMPLE.COM");
    System.setProperty("java.security.krb5.kdc", "HOSTNAME");
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
    HiveMetaStoreClient client = new HiveMetaStoreClient(conf);
    client.close();     

预期成果-它应该成功验证连接

实际结果-

java -jar Application

Kerberos 用户名 [root]: Kerberos password [root]:

我想绕过此 kerberos 终端提示身份验证。有什么办法可以做到这一点吗?

          final String user = "hive/HOSTNAME@EXAMPLE.COM";
          final String keyPath = "hive.keytab";
          Configuration conf = new Configuration();
          System.out.println("In case of kerberos authentication");
          conf.set("hadoop.security.authentication", "kerberos");
          System.setProperty("java.security.krb5.kdc", "HOSTNAME");
          System.setProperty("java.security.krb5.realm", "EXAMPLE.COM");
          UserGroupInformation.setConfiguration(conf);
          UserGroupInformation.loginUserFromKeytab(user, keyPath);
          HiveConf conf1 = new HiveConf(); MetastoreConf.setVar(conf1,
          ConfVars.THRIFT_URIS, "thrift://HOSTNAME:9083"); HiveMetaStoreClient
          client = new HiveMetaStoreClient(conf1);
          client.close();

现在工作正常

相关内容

最新更新