Summary of LDAP development

Source: Internet
Author: User
Tags ldap ldap search filter ad server adsi edit

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
  1. Get an LDAP connection
    1. Public class Ldapadhelper {
    2. Private Final String URL = "ldap://" + "192.168.1.204:389";
    3. Private Final String adminname = "[email protected]";
    4. Private Final String AdminPassword = "test123";
    5. Private Ldapcontext CTX = null;
    6. Public Static Ldapadhelper getinstance () throws namingexception{
    7. return New Ldapadhelper ();
    8. }
    9. Private Ldapadhelper () throws namingexception {
    10. this. INITLDAP ();
    11. }
    12. /**
    13. * Initialize LDAP
    14. * @throws namingexception
    15.     */
    16. Private void Initldap () throws namingexception {
    17. //Ad Server
    18. hashtable<string, string> hashenv = new hashtable<string, string> ();
    19. Hashenv.put (Context.security_authentication, "simple"); //LDAP access security level
    20. Hashenv.put (Context.security_principal, this. AdminName); //AD User
    21. Hashenv.put (context.security_credentials, this. AdminPassword); //AD Password
    22. Hashenv.put (Context.initial_context_factory, "com.sun.jndi.ldap.LdapCtxFactory"); //LDAP factory class
    23. Hashenv.put (Context.provider_url, this. URL);
    24. Try {
    25. CTX = new initialldapcontext (hashenv, null);
    26. SYSTEM.OUT.PRINTLN (" Initialize LDAP successfully! ");
    27. } catch (Namingexception e) {
    28. E.printstacktrace ();
    29. System.err.println ("Throw Exception: " + e);
    30. throw E;
    31. }
    32. }
    33. ....
    34. }

Calling the GetInstance () method of this class in an external class can get the helper object and initialize the Ldapcontext object

  1. Querying Object Properties
    1. Try {
    2. //domain node
    3. String searchbase = this. Getbasednforroleobject ("cn= role 1,cn=roleobjectdemo2,cn= Msds-azapplicationtest2,cn=authorization2,cn=program Data test,dc=cayzlh,dc=com");
    4. //LDAP search filter class
    5. String searchfilter = "(cn=*)";
    6. //Create a search controller
    7. Searchcontrols searchctls = new searchcontrols ();
    8. //Set search scope
    9. Searchctls.setsearchscope (Searchcontrols.onelevel_scope);
    10. String returnedatts[] = {"distinguishedname "}; ///Custom return property, only query role 1 The DistinguishedName property of this object
    11. Searchctls.setreturningattributes (Returnedatts); //Set return property set
    12. //Do not set to return all properties
    13. //Search LDAP based on domain node, filter class, and search controller set to get results
    14. Namingenumeration<?> answer = Ctx.search (Searchbase, Searchfilter, SEARCHCTLS); //Search
    15. while (Answer.hasmoreelements ()) {//traverse result set
    16. //Get the DN that meets the search criteria
    17. SearchResult sr = (SearchResult) answer.next ();
    18. //Get a qualifying set of properties
    19. Attributes attrs = Sr.getattributes ();
    20. if (attrs! = null) {
    21. Try {
    22. if (Attrs.getall (). Hasmore ()) {
    23. Attribute attr = (Attribute) ne.next (); //Get Next attribute
    24. System.out.println (Attr.getall (). Next (). ToString ());
    25. //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
    26. }
    27. } catch (Namingexception e) {
    28. E.printstacktrace ();
    29. System.err.println ("Throw Exception: " + e);
    30. }
    31. }
    32. }
    33. } catch (Namingexception e) {
    34. System.err.println ("Throw Exception: " + e);
    35. }
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

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.