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

时间:2019-04-24 05:30:53

标签: kerberos java-security spring-security-kerberos hive-metastore

我编写了一个Java应用程序以与hive-metastore连接。它工作正常,但是当我在Linux上运行jar文件时,它要求输入Kerberos用户名和密码。 。我已经在代码中指定了Kerberos主体和keytab文件。而且我不想使用其他的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应用程序

  

Kerberos用户名[root]:   Kerberos密码[root]:

我想绕过此kerberos终端提示身份验证。有什么办法吗?

1 个答案:

答案 0 :(得分:0)

          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();

现在可以正常使用