Java test class calls the LDAP server client information to implement the Add and revise check

Source: Internet
Author: User
Tags ldap

Here is my self-written Java test class calling client-configured user information:;ImportJava.util.Arrays;Importjava.util.Hashtable;ImportJavax.naming.Context;ImportJavax.naming.NamingEnumeration;ImportJavax.naming.NamingException;;;;;;;;ImportJavax.naming.ldap.Control;ImportJavax.naming.ldap.InitialLdapContext;ImportJavax.naming.ldap.LdapContext; Public  class userauthenticate {    PrivateString URL ="ldap://localhost:10389";PrivateString BASEDN ="Cn=zhangsan,ou=users,ou=system";PrivateString FACTORY ="Com.sun.jndi.ldap.LdapCtxFactory";PrivateLdapcontext CTX =NULL;PrivateHashtable env =NULL;Privatecontrol[] Connctls =NULL;@SuppressWarnings({"Unchecked","Rawtypes","Unused"}) Public void Ldap_connect() {env =NewHashtable ();        Env.put (Context.initial_context_factory, FACTORY);        Env.put (Context.provider_url, URL); Env.put (Context.security_authentication,"Simple"); Env.put (Context.security_principal,"Cn=lisi;ou=users;ou=system"); Env.put (Context.security_credentials,"123456");//Env.put ("", "secret");        Try{CTX =NewInitialldapcontext (env, CONNCTLS); }Catch(Namingexception e)        {E.printstacktrace (); }    } Public void Getuserdn() {String DN ="Zhangsan"; Searchcontrols controls =NewSearchcontrols ();//Limit the field content to queryControls.setsearchscope (Searchcontrols.subtree_scope);//Set filter conditionsString filter =(& (Objectclass=top) (Objectclass=person) (cn=)+ DN +"))";//Set the returned attributeControls.setreturningattributes (NewString[] {"UID","UserPassword","DisplayName","cn","SN","Mail","description"});Try{//Control search criteria, or NULL to use the default search control, the property to search for if NULL returns all objects in the target contextNamingenumeration answer = ctx. Search ("Ou=system", filter, controls); while(Answer.hasmore ())                {SearchResult result = (SearchResult) (); Namingenumeration en = Result.getattributes (). GetAll ();if(En = =NULL) {System.out.println ("No namingenumeration"); }if(!en.hasmoreelements ()) {System.out.println ("no element"); }//Output query to the result                 while(En.hasmore ())                    {Attribute attr = (Attribute) (); System.out.println (Attr.getid () +"="+ Attr.get ()); }            }        }Catch(Namingexception e)        {E.printstacktrace (); }    } Public void Testadd()throwsException {Attributes Attrs =NewBasicattributes (true); Attribute objclass =NewBasicAttribute ("objectclass"); String[] Attrobjectclassperson = {"InetOrgPerson","Organizationalperson","Person","Top"}; Arrays.sort (Attrobjectclassperson); for(String Ocp:attrobjectclassperson)        {Objclass.add (OCP);        } attrs.put (Objclass); String uid ="Zhangsan"; String UserDN ="Uid="+ UID +","+"Ou=system"; Attrs.put ("cn", UID); Attrs.put ("SN", UID); Attrs.put ("DisplayName","Zhang San"); Attrs.put ("description","NOT NULL"); Attrs.put ("Mail","[email protected]"); Attrs.put ("UserPassword","11111". GetBytes ("UTF-8"));    Ctx.createsubcontext (UserDN, attrs); } Public void Testdelete() {String uid ="Zhangsan"; String UserDN ="Uid="+ UID +","+"Ou=system";Try{Ctx.destroysubcontext (USERDN); }Catch(Namingexception e)        {E.printstacktrace (); }    } Public Boolean Testedit(){Booleanresult =true; String uid ="Zhangsan"; String UserDN ="Uid="+ UID +","+"Ou=system"; Attributes attr =NewBasicattributes (true); Attr.put ("Mail","[email protected]");Try{ctx.modifyattributes (UserDN, Dircontext.replace_attribute, attr); }Catch(Namingexception e)        {E.printstacktrace (); }returnResult } Public Static void Main(string[] args) {Userauthenticate test =NewUserauthenticate ();//Test.getuserdn ("Zhangsan;ou=users;ou=system");        Try{test. Ldap_connect ();//test.testadd ();Test.getuserdn ();//test.testedit ();            //test.testdelete ();}Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java test class calls the LDAP server client information to implement the Add and revise check

Related Article

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: 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.