Java obtains AD domain user information

Source: Internet
Author: User
Tags ldap search filter ad server
How does java obtain AD domain user information? Http://aa00aa00.iteye.com/blog/1276936http://blog.sina.com.cn/s/blog_6ef2c4540100nuvq.html package com. webservice. message; import java. util. hashtable; import javax. naming. context; import javax. naming. namingEnumeration; import javax. naming. namingException; import javax. naming. directory. attribute; import javax. naming. directory. attributes; import javax. naming. directory. searchControls; import javax. naming. dir Ecloud. searchResult; import javax. naming. ldap. initialLdapContext; import javax. naming. ldap. ldapContext; public class ADOperTest {public ADOperTest () {} public String GetADInfo (String name) {String userName = name; // user name if (userName = null) {userName = "" ;}string company = ""; String host = "10.10.10.21"; // AD server String port = "389 "; // port String url = new String ("ldap: //" + host + ":" + port); Hashtable HashEnv = new Hashtable (); // String adminName = "CN = oyxiaoyuanxy, CN = Users, DC = Hebmc, DC = com "; // AD username String adminName = "cq \ administrator"; // note the username Syntax: domain \ User String adminPassword = "prd @"; // password HashEnv. put (Context. SECURITY_AUTHENTICATION, "simple"); // LDAP access security level HashEnv. put (Context. SECURITY_PRINCIPAL, adminName); // AD User HashEnv. put (Context. SECURITY_CREDENTIALS, adminPasswor D); // AD Password HashEnv. put (Context. INITIAL_CONTEXT_FACTORY, "com. sun. jndi. ldap. ldapCtxFactory "); // LDAP factory class HashEnv. put (Context. PROVIDER_URL, url); try {LdapContext ctx = new InitialLdapContext (HashEnv, null); // domain node String searchBase = "OU = Chongqing tobacco, DC = cq, DC = tobacco, DC = com, DC = cn "; // LDAP search filter class String searchFilter =" objectClass = User "; // search controller SearchControls searchCtls = new SearchControls ();// Create the // search // controls // Create a search controller searchCtls. setSearchScope (SearchControls. SUBTREE_SCOPE); // Specify // the // search // scope // set the search range // searchCtls. setSearchScope (SearchControls. OBJECT_SCOPE); // Specify the search scope to Set the search range // String returnedAtts [] = {"memberOf", "distinguishedName", // "Pwd-Last-Set ", "User-Password", "cn"}; // custom return attribute String returnedAtts [] = {"company"}; // set Returns the property // String returnedAtts [] = {"url", "whenChanged", "employeeID", // "name", "userPrincipalName", "physicalDeliveryOfficeName ", // "departmentNumber", "telephoneNumber", "homePhone", // "mobile", "department", "sAMAccountName", "whenChanged", // "mail "}; // customize the returned property searchCtls. setReturningAttributes (returnedAtts); // set the returned attribute set. // search for LDAP Based on the configured domain node, filter class, and search controller. The result is NamingEnumeration answer = ctx. searc H (searchBase, searchFilter, searchCtls); // Search for objects using the filter // the initial Search result is 0 int totalResults = 0; // Specify the attributes to return int rows = 0; while (answer. hasMoreElements () {// traverse the result set SearchResult sr = (SearchResult) answer. next (); // get the DN System that meets the search criteria. out. println (++ rows + "********************************* ***************"); string dn = sr. getName (); System. out. println (dn ); String match = dn. split ("CN =") [1]. split (",") [0]; // The return format is generally CN = ptyh, OU = monopoly System. out. println (match); if (userName. equals (match) {Attributes Attrs = sr. getAttributes (); // obtain the if (Attrs! = Null) {try {for (NamingEnumeration ne = Attrs. getAll (); ne. hasMore ();) {Attribute Attr = (Attribute) ne. next (); // get the next attribute System. out. println ("AttributeID = property name:" + Attr. getID (). toString (); // read the attribute value for (NamingEnumeration e = Attr. getAll (); e. hasMore (); totalResults ++) {company = e. next (). toString (); System. out. println ("AttributeValues = property value:" + company);} System. out. println ("---------------") ;}} catch (NamingException e) {System. err. println ("Throw Exception:" + e) ;}// if }// while System. out. println ("************************************* ***********"); system. out. println ("Number:" + totalResults); ctx. close ();} catch (NamingException e) {e. printStackTrace (); System. err. println ("Throw Exception:" + e);} return company;} public static void main (String args []) {// instantiate ADOperTest ad = new ADOperTest (); ad. getADInfo ("shz ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.