Java HDFS Kerberos 认证
程序员文章站
2022-03-24 11:41:24
...
Kerberos 认证
代码如下:
package com.gridsum.datasocket;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
public class Kerberos {
public static final String USER_KEY = "[email protected]";
public static final String KEY_TAB_PATH = "test.keytab";
static Configuration conf = new Configuration();
static {
System.setProperty("java.security.krb5.conf", "D:\\krb5.conf");
conf.set("hadoop.security.authentication", "kerberos");
conf.set("fs.defaultFS", "hdfs://192.168.20.1:8020");
try {
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(USER_KEY, KEY_TAB_PATH);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
FileSystem fs = FileSystem.get(conf);
RemoteIterator<LocatedFileStatus> it = fs.listFiles(new Path("/user/"), true);
while (it.hasNext()){
LocatedFileStatus next = it.next();
System.out.println(next.getPath().getName());
}
}
}
主要是3行
System.setProperty("java.security.krb5.conf", "D:\\krb5.conf");//配置krb5.conf的路径
conf.set("hadoop.security.authentication", "kerberos"); //配置认证方式,有kerberos和simple两种
conf.set("fs.defaultFS", "hdfs://192.168.20.1:8020");//namenode的地址和端口
System.setProperty("java.security.krb5.conf", "D:\\krb5.conf")
这一行必须要有,否则会报以下错误
java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException: Can't get Kerberos realm
at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:65)
at org.apache.hadoop.security.UserGroupInformation.initialize(UserGroupInformation.java:277)
at org.apache.hadoop.security.UserGroupInformation.setConfiguration(UserGroupInformation.java:313)
at com.gridsum.datasocket.Kerberos.<clinit>(Kerberos.java:26)
Caused by: java.lang.reflect.InvocationTargetException
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.apache.hadoop.security.authentication.util.KerberosUtil.getDefaultRealm(KerberosUtil.java:84)
at org.apache.hadoop.security.HadoopKerberosName.setConfiguration(HadoopKerberosName.java:63)
... 3 more
Caused by: KrbException: Cannot locate default realm
at sun.security.krb5.Config.getDefaultRealm(Config.java:1029)
... 9 more
Exception in thread "main"
上一篇: GCC 将改用 C++ 实现
下一篇: Maven 功能简介
推荐阅读
-
hdfs/hbase 程序利用Kerberos认证超过ticket_lifetime期限后异常
-
Flink整合oozie shell Action 提交任务 带kerberos认证
-
Python confluent kafka客户端配置kerberos认证流程详解
-
[Hadoop]HDFS:java API
-
java大数据最全课程学习笔记(4)--HDFS NN,2NN,DN及HDFS2.x新特性
-
Hadoop:本地文件(window系统)定时获取文件并上传至HDFS文件(两个虚拟机)系统 Java 实现
-
Java程序中不通过hadoop jar的方式访问hdfs
-
Java Springboot 微信第三方支付 证书认证绑定 Docker导入CER证书
-
Java实现自定义认证功能(基于session)
-
Hadoop学习(2)-java客户端操作hdfs及secondarynode作用