教培参考
教育培训行业知识型媒体
发布时间: 2024年11月23日 20:37
原理架构
步骤原理说明:通过HDFS提供的接口获取(具体接口信息可以参考HDFS二次开发指南)。
private Boolean login(Configuration conf){
boolean flag = false;
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(conf.get(PRINCIPAL),conf.get(KEYTAB));
System.out.println("UserGroupInformation.isLoginKeytabbased():" +UserGroupInformation.isLoginKeytabbased());
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
通过客户端shell命令以kinit方式获取,具体使用方式可参考Shell操作维护命令说明书。认证代码实例讲解
private void init() throws IOException {
Configuration conf = new Configuration();
// conf file
conf.addResource(new Path(System.getProperty("user.dir")
+ File.separator + "conf" + File.separator + "hdfs-site.xml"));
conf.addResource(new Path(System.getProperty("user.dir")
+ File.separator + "conf" + File.separator + "core-site.xml"));
// security mode
if ("kerberos".equalsIgnoreCase(conf
.get("hadoop.security.authentication"))) {
// 注[1]
conf.set(PRINCIPAL,"hdfstest@HADOOP.COM");
// keytab file
conf.set(KEYTAB,System.getProperty("user.dir") + File.separator
+ "conf" + File.separator + "user.keytab");
// kerberos path
String krbfilepath = System.getProperty("user.dir")
+ File.separator + "conf" + File.separator + "krb5.conf";
System.setProperty("java.security.krb5.conf",krbfilepath);
login(conf); //注[2]
}
// get filesystem
try {
fSystem = FileSystem.get(conf); //注[3]
} catch (IOException e) {
throw new IOException("Get fileSystem failed.");
}
}
private Boolean login(Configuration conf){
boolean flag = false;
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(conf.get(PRINCIPAL),conf.get(KEYTAB));
System.out.println("UserGroupInformation.isLoginKeytabbased(): " +UserGroupInformation.isLoginKeytabbased());
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}