连接开启kerberos认证hive

611 阅读1分钟

连接kerberos认证的hive其实很简单,主要包含三个内容:

  1. krb5.conf配置文件
  2. 密钥文件.keytab
  3. 认证的principal

认证核心代码:

/**
 * hadoop环境实现kerberos认证
 *
 * @param arDataSource
 */
public static boolean kerberosLoginHive(ArDataSource arDataSource) {
    log.info("Kerberos 登陆验证");
    try {
        // 获取key tab文件的路径
        String keytabPath = arDataSource.getKerberosFilePath();
        log.debug(keytabPath);
        // krb5.conf文件在生产环境中以环境变量的方式进行配置
        // 设置krb配置文件路径,注意一定要放在Configuration前面,不然不生效
        // System.setProperty("java.security.krb5.conf", configPath);
        Configuration conf = new org.apache.hadoop.conf.Configuration();

        // 设置认证模式Kerberos
        conf.set("hadoop.security.authentication", "Kerberos");
        conf.set("keytab.file", keytabPath);
        conf.set("kerberos.principal", arDataSource.getCaPrincipalName());

        UserGroupInformation.setConfiguration(conf);
        // 设置认证用户和krb认证文件路径
        UserGroupInformation.loginUserFromKeytab(arDataSource.getCaPrincipalName(), keytabPath);
        log.info("Kerberos 验证成功");
        return true;
    } catch (Exception e) {
        log.error("Kerberos 验证失败", e);
        return false;
    }
}

认证通过之后,就可以使用jdbc去连接hive,不再需要使用用户名密码,注意连接url后面要加上

;principal=xxx/yyy@zzz.com