KerberosUtil.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.primeton.dsp.datarelease.data.bdata;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.security.UserGroupInformation;
  5. import java.io.IOException;
  6. /**
  7. *
  8. * Kerberos 认证
  9. *
  10. * @author zhaopx
  11. */
  12. @Slf4j
  13. public class KerberosUtil {
  14. /**
  15. * 开始登陆,如果登陆失败抛出 SecurityException
  16. * @param conf
  17. * @param principal
  18. * @param userKeytabFile
  19. * @param krb5File
  20. */
  21. public static void loginKerberos(Configuration conf, String principal, String userKeytabFile, String krb5File) {
  22. System.setProperty("java.security.krb5.conf", krb5File);
  23. conf.setBoolean("hadoop.security.authorization", true);
  24. conf.set("hadoop.security.authentication", "kerberos");
  25. try {
  26. UserGroupInformation.setConfiguration(conf);
  27. UserGroupInformation.loginUserFromKeytab(principal, userKeytabFile);
  28. log.info("kerberos 认证成功!");
  29. } catch (IOException e) {
  30. log.error("kerberos 认证失败!", e);
  31. throw new SecurityException("kerberos 认证失败!", e);
  32. }
  33. }
  34. }