我正在尝试使用kerberos keytab身份验证和jaas配置连接到impala db。
通过将系统属性-“java.security.auth.login.config”设置为jaas配置文件,我已经能够连接到impala db。而且效果很好。以下是工作代码:
System.setProperty("sun.security.krb5.debug", "false");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");
System.setProperty("java.security.krb5.conf", krb5ConfPath);
System.setProperty("java.security.auth.login.config", jaasPath);
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(principal, keyTabPath);
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(DatabaseConfig.class.getClassLoader());
return dataSourceBuilder.build();
问题是jaas config需要keytab文件的绝对路径。在我的例子中,这个路径会因环境而异,我不想为每个环境维护不同的jaas.config。
所以我采用编程方法生成jaas配置。我可以创建“javax.security.auth.login.configuration”对象并使用
javax.security.auth.login.configuration.setconfiguration(jaasconfig);
但当我运行带有此更改的代码时,它无法选择jaas配置,并且失败,出现以下异常:
获取jdbc连接失败;嵌套的异常为java.sql.sqlexception:[simba]impalajdbcdriver使用票证缓存创建登录上下文时出错:无法获取身份验证的主体名称。
有谁能解释一下,当通过系统属性设置配置时,为什么代码运行良好,而当以编程方式设置配置时,为什么代码不工作。
System.setProperty("sun.security.krb5.debug", "false");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");
System.setProperty("java.security.krb5.conf", krb5ConfPath);
javax.security.auth.login.Configuration jaasConfig = createJaasConfig(keyTabPath);
javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(principal, keyTabPath);
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(DatabaseConfig.class.getClassLoader());
return dataSourceBuilder.build();
这就是我创建jaas配置的方式:
private static javax.security.auth.login.Configuration createJaasConfig(final String keyTabPath) throws Exception {
// Create entry options.
Map<String, Object> options = new HashMap<>();
options.put("useTicketCache", "false");
options.put("doNotPrompt", "true");
options.put("useKeyTab", "true");
options.put("debug", "false");
// options.put("storeKey", "true");
options.put("principal", "user@DOMAIN.COM");
options.put("keyTab", keyTabPath);
// Create entries
AppConfigurationEntry[] entries = {
new AppConfigurationEntry(
Krb5LoginModule.class.getCanonicalName(),
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
options
)
};
// Create configuration
return new javax.security.auth.login.Configuration() {
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
return entries;
}
};
}
暂无答案!
目前还没有任何答案,快来回答吧!