教培參考
教育培訓行業(yè)知識型媒體
發(fā)布時間: 2025年01月11日 10:14
原理架構
?
步驟原理說明:通過HDFS提供的接口獲取(具體接口信息可以參考HDFS二次開發(fā)指南)。
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;
}
微信掃碼關注公眾號
獲取更多考試熱門資料