发布时间: 2019-05-25 11:05:35
原理架构
通过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;
}
认证代码实例讲解
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;
}