The java test class calls the LDAP server client information for addition, deletion, modification, and query, and the test class ldap

Source: Internet
Author: User

The java test class calls the LDAP server client information for addition, deletion, modification, and query, and the test class ldap

The user information configured by the client is called by the self-written java test class as follows:

Package com; import java. io. unsupportedEncodingException; import java. util. arrays; 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. basicAttribute; import javax. naming. directory. basicAttributes; import javax. naming. Directory. dirContext; import javax. naming. directory. searchControls; import javax. naming. directory. searchResult; import javax. naming. ldap. control; import javax. naming. ldap. initialLdapContext; import javax. naming. ldap. ldapContext; public class UserAuthenticate {private String URL = "ldap: // localhost: 10389"; private String BASEDN = "cn = zhangsan, ou = users, ou = system "; private String FACTORY = "com. sun. jndi. l Dap. ldapCtxFactory "; private LdapContext ctx = null; private Hashtable env = null; private Control [] connCtls = null; @ SuppressWarnings ({" unchecked "," rawtypes "," unused "}) public void LDAP_connect () {env = new Hashtable (); 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 = new InitialLdapContext (env, connCtls);} catch (NamingException e) {e. printStackTrace () ;}} public void getUserDN () {String dn = "zhangsan"; SearchControls controls = new SearchControls (); // restrict the content of the field to be queried. setSearchScope (SearchControls. SUBTREE_SCOPE); // sets the filter condition String filt. Er = "(& (objectClass = top) (objectClass = person) (cn =" + dn + ")"; // sets the returned attribute controls. setReturningAttributes (new String [] {"uid", "userPassword", "displayName", "cn", "sn", "mail", "description "}); try {// control the search condition. If it is null, the default search control is used. If the property to be searched is null, all the objects NamingEnumeration answer = ctx in the target context are returned. search ("ou = system", filter, controls); while (answer. hasMore () {SearchResult result = (SearchResu Lt) answer. next (); NamingEnumeration en = result. getAttributes (). getAll (); if (en = null) {System. out. println ("Have no NamingEnumeration");} if (! En. hasMoreElements () {System. out. println ("Have no element");} // output the query result while (en. hasMore () {Attribute attr = (Attribute) en. next (); System. out. println (attr. getID () + "=" + attr. get () ;}} catch (NamingException e) {e. printStackTrace () ;}} public void testAdd () throws Exception {Attributes attrs = new BasicAttributes (true); Attribute objclass = new BasicAttribute ("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", "abc@126.com"); 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 () {boolean result = true; String uid = "zhangsan"; String userDN = "uid =" + uid + ", "+" ou = system "; Attributes attr = new BasicAttributes (true); attr. put ("mail", "zhangsan@163.com"); try {ctx. modifyAttributes (userDN, DirContext. REPLACE_ATTRIBUTE, attr);} catch (NamingException e) {e. printStackTrace ();} return result;} public static void main (String [] args) {UserAuthenticate test = new UserAuthenticate (); // 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 block e. printStackTrace ();}}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.