I encountered an issue related to Kerberos authentication in my project. My code is as follows.
private void kerberosAuthentication() throws IOException {
String keyTabFIle = "";
String krbConf = "";
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] keyTabResources = resolver.getResources(DATA_PLATFORM_KEYTAB_RELATIVE_PATH);
Resource tabResource = keyTabResources[0];
InputStream tabResourceInputStream = tabResource.getInputStream();
File keyTabOutFile = new File(DATA_PLATFORM_KEYTAB_ABSOLUTE_PATH);
FileCopyUtils.copy(tabResourceInputStream, new FileOutputStream(keyTabOutFile));
keyTabFIle = keyTabOutFile.getAbsolutePath();
Resource[] confResources = resolver.getResources(KRB_KRB_5_CONF_RELATIVE_PATH);
Resource confResource = confResources[0];
InputStream confResourceInputStream = confResource.getInputStream();
File confFile = new File(DATA_APPDATAS_KRB_5_CONF_ABSOLUTE_PATH);
FileCopyUtils.copy(confResourceInputStream, new FileOutputStream(confFile));
krbConf = confFile.getAbsolutePath();
log.info("HiveJDBCHelper keyTabUrl:{}\n krbConfUrl:{}", keyTabFIle, krbConf);
conf.set("keytab.file", keyTabFIle);
conf.set("kerberos.principal", PRINCIPAL);
System.setProperty("java.security.krb5.conf", krbConf);
System.setProperty("hadoop.home.dir", "/");
try {
log.info("HiveJDBCHelper success load config");
UserGroupInformation.setConfiguration(conf);
log.info("HiveJDBCHelper success setConfiguration");
UserGroupInformation.loginUserFromKeytab(PRINCIPAL, keyTabFIle);
log.info("HiveJDBCHelper success loginUserFromKeytab");
} catch (Exception e) {
log.info("Kerberos authentication failed");
e.printStackTrace();
}
}
I have also generated keytab and conf files in the server directory. However, I'm still encountering an error:
java.lang.IllegalArgumentException: Can't get Kerberos realm
2023-12-11 21:17:23 at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:65)
2023-12-11 21:17:23 at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:309)
2023-12-11 21:17:23 at org.apache.hadoop.security.UserGroupInformation.setConfiguration(UserGroupInformation.java:355)
2023-12-11 21:17:23 at com.yupaopao.personas.utils.HiveJdbc.kerberosAuthentication(HiveJdbc.java:72)
2023-12-11 21:17:23 at com.yupaopao.personas.utils.HiveJdbc.getConnection(HiveJdbc.java:89)
2023-12-11 21:17:23 at com.yupaopao.personas.dao.HiveDaoImpl.queryUserNum(HiveDaoImpl.java:50)
2023-12-11 21:17:23 at com.yupaopao.personas.dao.HiveDaoImpl$$FastClassBySpringCGLIB$$313bc044.invoke(<generated>)
2023-12-11 21:17:23 at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
2023-12-11 21:17:23 at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
2023-12-11 21:17:23 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
2023-12-11 21:17:23 at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
2023-12-11 21:17:23 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
2023-12-11 21:17:23 at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
2023-12-11 21:17:23 at com.yupaopao.personas.dao.HiveDaoImpl$$EnhancerBySpringCGLIB$$3f558dc4.queryUserNum(<generated>)
2023-12-11 21:17:23 at com.yupaopao.personas.service.UserPortraitServiceImpl.queryUserNumAndTagDetailsV2(UserPortraitServiceImpl.java:1420)
2023-12-11 21:17:23 at com.yupaopao.personas.service.UserPortraitServiceImpl.lambda$createOrEditUserPortraitV2$9(UserPortraitServiceImpl.java:889)
2023-12-11 21:17:23 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
2023-12-11 21:17:23 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2023-12-11 21:17:23 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2023-12-11 21:17:23 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2023-12-11 21:17:23 at java.lang.Thread.run(Thread.java:748)
2023-12-11 21:17:23 Caused by: java.lang.reflect.InvocationTargetException
2023-12-11 21:17:23 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2023-12-11 21:17:23 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2023-12-11 21:17:23 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2023-12-11 21:17:23 at java.lang.reflect.Method.invoke(Method.java:498)
2023-12-11 21:17:23 at org.apache.hadoop.security.authentication.util.KerberosUtil.getDefaultRealm(KerberosUtil.java:110)
2023-12-11 21:17:23 at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:63)
2023-12-11 21:17:23 ... 20 more
2023-12-11 21:17:23 Caused by: KrbException: Cannot locate default realm
2023-12-11 21:17:23 at sun.security.krb5.Config.getDefaultRealm(Config.java:1029)
2023-12-11 21:17:23 ... 26 more
I hope you can help me solve this. Thank you!