Windows ad information is available through the Java Native API, and you want to know what Windows Active Directory is, and you can access the relevant information, and you may not have an ad viewer in your Windows system after you install it. We need to download the software to update, download the address: Https://www.technipages.com/windows-install-active-directory-users-and-computers, After installing the ad we can see the following information through the Ad viewer, and we can view the ad information through some tools such as Ldapsoft LDAP browse:
The image above is the ad information that my Windows computer displays, mine is the WIN10 system, and the properties shown above can be obtained from the Java API
To get ldapcontext information, to obtain the Ldapcontext information, we need to provide the 389-port LDAP protocol computer IP, domain, username and password information, or get the error:
public class Ldapcontextfactory {private static ldapcontext ldapcontext; public static Ldapcontext getldapcontext (string url, string domain, string username, string password) {if (ldapcontext = = null) {try {hashtable<string, string> environment = getactivedirectoryenvironment (URL, domain, username,
password);
Ldapcontext = new Initialldapcontext (environment, NULL);
catch (Namingexception e) {e.printstacktrace ();
} return ldapcontext; private static hashtable<string, string> getactivedirectoryenvironment (string url, string domain, string usernam
E, String password) {hashtable<string, string> environment = new hashtable<> ();
Environment.put ("Java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
Environment.put ("Java.naming.security.authentication", "simple");
Environment.put ("Java.naming.provider.url", url);
Environment.put ("Java.naming.security.principal", username + "@" + domain); EnvironmeNt.put ("Java.naming.security.credentials", password);
return environment; }
}
Get all property information
public static void Getallattribute (Ldapcontext ldapcontext) {try {System.out.println ("Validate success:" + Ldapcon
Text);
Name name = new LDAPName ("dc=centmfademo,dc=com");
Attributes allattrs = ldapcontext.getattributes (name);
if (null = = Allattrs) {System.out.println ("no Attributes");
Return for (namingenumeration<?> attrs = Allattrs.getall (); Attrs.hasmore ();)
{Attribute attr = (attribute) attrs.next ();
System.out.println ("attr:" + Attr.getid ()); for (Namingenumeration<?> values = Attr.getall (); Values.hasmore ();)
{System.out.println ("\tvalue:" + values.next ());
} System.out.println ("-------------------------------------------");
catch (Authenticationexception e) {e.printstacktrace ();
catch (Namingexception e) {e.printstacktrace ();
System.out.println ("Validate faliure:" + e);
Finally {try {ldapcontext.close ();
catch (Namingexception e) {e.printstacktrace (); }
}
}
Get custom Attribute information:
public static void Getspecialattribute (Ldapcontext ldapcontext) {try{Searchcontrols searchcontrols = new Searchcont
Rols ();
Searchcontrols.setsearchscope (Searchcontrols.subtree_scope); String returnedatts[] = {"MemberOf", "Dscorepropagationdata"};//Custom return property/*string returnedatts[] = {"url", "whenchanged", "EmployeeID", "name", "userPrincipalName", "physicalDeliveryOfficeName", "Departmentnumber", "telephonenumber", "Hom Ephone "," mobile "," department "," sAMAccountName "," whenchanged "," Mail "}; Custom return Properties/Searchcontrols.setreturningattributes (Returnedatts); Sets the return property set String searchbase = "dc=centmfademo,dc=com"; Specify the Base for the search//Search domain node//(& (Objectclass=user))//(& (Objectclass=user) (samaccountname= dev001))//(& (Objectclass=user) (samaccountname=dev001) (Givenname=dev)) String searchfilter = "Objectclass=user" ; Specify the LDAP search filter//string searchfilter = "Objectclass=organizationalunit";//specIfy the LDAP search filter namingenumeration<searchresult> searchresults = Ldapcontext.search (searchbase, search
Filter,searchcontrols);
while (Searchresults.hasmoreelements ()) {SearchResult SearchResult = Searchresults.next ();
System.out.println (Searchresult.getname ());
Attributes allattrs = Searchresult.getattributes ();
System.out.println (Allattrs.get ("Mail"));
if (null = = Allattrs) {System.out.println ("no Attributes");
Return for (namingenumeration<?> attrs = Allattrs.getall (); Attrs.hasmore ();)
{Attribute attr = (attribute) attrs.next ();
System.out.println ("attr:" + Attr.getid ());
System.out.println ("attr get:" + attr.get (). toString ()); for (Namingenumeration<?> values = Attr.getall (); Values.hasmore ();)
{System.out.println ("\tvalue:" + values.next ());
}/*enumeration<?> values = Attr.getall ();
if (values!= null) {while (values.hasmoreelements ()) { System.out.println ("attributevalues=" + values.nextelement ());
'}*/}} ' catch (Authenticationexception e) {e.printstacktrace ();
catch (Namingexception e) {e.printstacktrace ();
System.out.println ("Validate faliure:" + e);
Finally {try {ldapcontext.close ();
catch (Namingexception e) {e.printstacktrace (); }
}
}
Write our test class for testing
public static void Main (string[] args) {
String url = "ldap://127.0.0.1:389";//19.201.
String domain = "testademo.com";
String username = "Administrator"; User name
String password = "Testhello";//password
ldapcontext ldapcontext = ldapcontextfactory.getldapcontext (URL, Domain, username, password);
Getallattribute (ldapcontext);
Getspecialattribute (Ldapcontext);
}