Write in front
Since the requirement of a recent project is to read some data from the ad domain and then save it to the database, some research has been done on the LDAP thing.
Thanks for the information provided in the following links:
http://wibiline.iteye.com/blog/1840739
http://aa00aa00.iteye.com/blog/1276936
Http://www.cnblogs.com/forbreak/archive/2012/10/30/2746464.html
http://cgs1999.iteye.com/blog/1574635
Http://www.cnblogs.com/awpatp/archive/2010/02/14/1668097.html
Project requirements
It is known that a node "Cn=authorization2,cn=program Data test,dc=cayzlh,dc=com" needs to get the relevant properties of a node under the node, and then extract it and save it to the database.
Solve the problem
- Get an LDAP connection
- Public class Ldapadhelper {
- Private Final String URL = "ldap://" + "192.168.1.204:389";
- Private Final String adminname = "[email protected]";
- Private Final String AdminPassword = "test123";
- Private Ldapcontext CTX = null;
- Public Static Ldapadhelper getinstance () throws namingexception{
- return New Ldapadhelper ();
- }
- Private Ldapadhelper () throws namingexception {
- this. INITLDAP ();
- }
- /**
- * Initialize LDAP
- * @throws namingexception
- */
- Private void Initldap () throws namingexception {
- //Ad Server
- hashtable<string, string> hashenv = new hashtable<string, string> ();
- Hashenv.put (Context.security_authentication, "simple"); //LDAP access security level
- Hashenv.put (Context.security_principal, this. AdminName); //AD User
- Hashenv.put (context.security_credentials, this. AdminPassword); //AD Password
- Hashenv.put (Context.initial_context_factory, "com.sun.jndi.ldap.LdapCtxFactory"); //LDAP factory class
- Hashenv.put (Context.provider_url, this. URL);
- Try {
- CTX = new initialldapcontext (hashenv, null);
- SYSTEM.OUT.PRINTLN (" Initialize LDAP successfully! ");
- } catch (Namingexception e) {
- E.printstacktrace ();
- System.err.println ("Throw Exception: " + e);
- throw E;
- }
- }
- ....
- }
Calling the GetInstance () method of this class in an external class can get the helper object and initialize the Ldapcontext object
- Querying Object Properties
- Try {
- //domain node
- String searchbase = this. Getbasednforroleobject ("cn= role 1,cn=roleobjectdemo2,cn= Msds-azapplicationtest2,cn=authorization2,cn=program Data test,dc=cayzlh,dc=com");
- //LDAP search filter class
- String searchfilter = "(cn=*)";
- //Create a search controller
- Searchcontrols searchctls = new searchcontrols ();
- //Set search scope
- Searchctls.setsearchscope (Searchcontrols.onelevel_scope);
- String returnedatts[] = {"distinguishedname "}; ///Custom return property, only query role 1 The DistinguishedName property of this object
- Searchctls.setreturningattributes (Returnedatts); //Set return property set
- //Do not set to return all properties
- //Search LDAP based on domain node, filter class, and search controller set to get results
- Namingenumeration<?> answer = Ctx.search (Searchbase, Searchfilter, SEARCHCTLS); //Search
- while (Answer.hasmoreelements ()) {//traverse result set
- //Get the DN that meets the search criteria
- SearchResult sr = (SearchResult) answer.next ();
- //Get a qualifying set of properties
- Attributes attrs = Sr.getattributes ();
- if (attrs! = null) {
- Try {
- if (Attrs.getall (). Hasmore ()) {
- Attribute attr = (Attribute) ne.next (); //Get Next attribute
- System.out.println (Attr.getall (). Next (). ToString ());
- //The output here is the DistinguishedName property of role 1, if you want to get a property with multiple values, you can use the loop and next () method to get all the values of this property
- }
- } catch (Namingexception e) {
- E.printstacktrace ();
- System.err.println ("Throw Exception: " + e);
- }
- }
- }
- } catch (Namingexception e) {
- System.err.println ("Throw Exception: " + e);
- }
Tools
The tools that you use during development are the ADSI Edit to view and edit the corresponding properties in the domain service. Open in the way:
Source
Write a simple demo, is the web version, perhaps some problems, a little change on it. Also need to be able to download to play.
: Http://files.cnblogs.com/files/chenanyu/LdapTest.zip
Long time has not sent a blog, the shortcomings of the excuse.
Summary of LDAP development