2016年3月8日 星期二

JAVA junit測試 AD帳密驗證 + search AD displayName(顯示名稱)

  1. package junit.test.AD;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. import javax.naming.AuthenticationException;
  6. import javax.naming.CommunicationException;
  7. import javax.naming.Context;
  8. import javax.naming.NamingEnumeration;
  9. import javax.naming.NamingException;
  10. import javax.naming.directory.Attributes;
  11. import javax.naming.directory.SearchControls;
  12. import javax.naming.directory.SearchResult;
  13. import javax.naming.ldap.InitialLdapContext;
  14. import javax.naming.ldap.LdapContext;
  15.  
  16. import org.junit.Test;
  17.  
  18.  
  19. public class AdTest {
  20. @Test
  21. public void test() {
  22. // ResourceBundle res = ResourceBundle.getBundle("config");
  23. // String ldapURL = res.getString("Active.directory.ldapURL");
  24. String ldapURL = "ldap://172.23.101.108:389" ;
  25. System.out.println(ldapURL);
  26. String account = "admin";
  27. String password = "p@ssw0rd";
  28. try{
  29. LDAP_AUTH_AD(ldapURL, account, password);
  30. System.out.println("認證成功!");
  31. } catch (Exception e) {
  32. System.out.println(e.getMessage());
  33. }
  34. }
  35. public static void LDAP_AUTH_AD(String ldap_url, String account, String password) throws Exception {
  36. if (account.isEmpty() || password.isEmpty()) throw new Exception("認證失敗!");
  37. Hashtable env = new Hashtable();
  38. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  39. env.put(Context.PROVIDER_URL, ldap_url);
  40. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  41. env.put(Context.SECURITY_PRINCIPAL, account+"@mizuho.com");
  42. env.put(Context.SECURITY_CREDENTIALS, password);
  43. LdapContext ctx = null;
  44. try {
  45. ctx = new InitialLdapContext(env, null);
  46. } catch (AuthenticationException e) {
  47. /**
  48. * error Code 說明 :
  49. * 525 : 用戶沒有找到
  50. * 52e : 證號不正確
  51. * 530 : 此時間不允許登入(not permitted to logon at this time)
  52. * 532 : 密碼期滿
  53. * 533 : 帳號不可用
  54. * 701 : 帳戶期滿
  55. * 773 : 用戶必須重設密碼
  56. * data 後面為錯誤代碼
  57. */
  58. throw new Exception(e.getMessage() + "認證失敗!");
  59. } catch (CommunicationException e) {
  60. throw new Exception("找不到伺服器!");
  61. } catch (Exception e) {
  62. throw new Exception("發生未知的錯誤!");
  63. } finally {
  64. if (ctx != null) {
  65. try {
  66. ctx.close();
  67. } catch (NamingException e) {
  68. }
  69. }
  70. }
  71. }
  72. @Test
  73. public void testQueryAdUser() throws NamingException {
  74. String userId = "admin" ;
  75. String ldapURL = "ldap://172.23.101.108:389" ;
  76. String domainName = "mizuho.com";
  77. String account = "admin";
  78. String password = "p@ssw0rd";
  79. Hashtable env = new Hashtable();
  80. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  81. env.put(Context.PROVIDER_URL, ldapURL);
  82. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  83. env.put(Context.SECURITY_PRINCIPAL, account+"@"+domainName);
  84. env.put(Context.SECURITY_CREDENTIALS, password);
  85. LdapContext ldapContext = new InitialLdapContext(env, null);
  86. SearchControls searchCtls = new SearchControls();
  87. String returnedAtts[] = { "sn", "givenName", "samAccountName","displayName" };
  88. searchCtls.setReturningAttributes(returnedAtts);
  89. searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  90. String searchFilter = "(&(userPrincipalName="+userId+"@"+domainName+"))";
  91. String searchBase = "dc=mizuho,dc=com";
  92. // initialize counter to total the results
  93. int totalResults = 0;
  94. // Search for objects using the filter
  95. NamingEnumeration answer = ldapContext.search(searchBase,
  96. searchFilter, searchCtls);
  97. // Loop through the search results
  98. while (answer.hasMoreElements()) {
  99. SearchResult sr = (SearchResult) answer.next();
  100. totalResults++;
  101. System.out.println(">>>" + sr.getName());
  102. String displayName = sr.getName().replace("CN=", "");
  103. displayName = displayName.split(",")[0] ;
  104. System.out.println("displayName >>" + displayName);
  105. Attributes attrs = sr.getAttributes();
  106. System.out.println(">>>>>>" + attrs.get("samAccountName"));
  107. }
  108. System.out.println("Total results: " + totalResults);
  109. ldapContext.close();
  110. }
  111. }

沒有留言:

張貼留言