Can‘t get Kerberos realm
近期封装Ozone客户端,需要走kerberos登陆,遇到了一个问题,花了很长时间解决。记录一下,跟网上能搜到的解决方案不大一样。
先看有问题的代码
Configuration config = new Configuration();
ConfigurationUtils.setHadoopConfig(config, basePath);
config.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(config);
System.setProperty("java.security.krb5.conf", basePath + ozoneProperties.getKrb5Conf());
// 登录Kerberos
UserGroupInformation.loginUserFromKeytab(ozoneProperties.getPrincipal(), basePath + ozoneProperties.getKeytab());
// UserGroupInformation.loginUserFromKeytab(ozoneProperties.getPrincipal(), "D:\\gitee\\bigdata-eis-platform\\eis-framework\\eis-framework-starter-ozone-oss\\src\\test\\resources\\prd\\ic.bigdatamgr.keytab");
// 配置Ozone客户端
OzoneConfiguration conf = new OzoneConfiguration();
conf.addResource(new Path(basePath + ozoneProperties.getOzoneSite()));
OzoneClient ozClient = OzoneClientFactory.getRpcClient(conf);
return ozClient;
运行的时候报入下错误:
java.lang.IllegalArgumentException: Can't get Kerberos realm
at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:71)
at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:315)
at org.apache.hadoop.security.UserGroupInformation.setConfiguration(UserGroupInformation.java:366)
at com.byd.bigdata.eis.framework.ozone.oss.autoconfig.OzoneAutoConfiguration.initOzoneClient(OzoneAutoConfiguration.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:921)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
at com.byd.bigdata.eis.module.datareportsystem.DatareportSystemApplication.main(DatareportSystemApplication.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Caused by: java.lang.IllegalArgumentException: KrbException: Cannot locate default realm
at javax.security.auth.kerberos.KerberosPrincipal.<init>(KerberosPrincipal.java:154)
at org.apache.hadoop.security.authentication.util.KerberosUtil.getDefaultRealm(KerberosUtil.java:120)
at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:69)
36 common frames omitted
错误原因:hadoop的配置加载放在了kerberos的配置的前面,导致hadoop加载配置找不到kerberos的相关配置,所以报这个错误
解决办法:把加载kerberos配置的代码放在最前面即可