Springldap operation LDAP example (add, delete, modify, and query)

Source: Internet
Author: User
Tags ldap

There are indeed many examples on the Internet, but I am still not satisfied with the search process, so I will summarize it myself to make it easier for me to refer to it later, as well as to facilitate other children's shoes to find information.

Springldap operation LDAP example (add, delete, modify, and query)

Before reading this article, we 'd better understand the schema file of OpenLDAP, that is, understanding objectclass and attribute and their relationship. Otherwise, it is easy to understand the meaning of the Code and the thrown exceptions.

Entity class:

Package LDAP. entity;/*** the test class person object comes from the core. schema file of the schema file * objectclass is person. The required and optional attributes are also obtained based on this object. * Author: Ding Chengyun */public class person {private string Sn; // mandatory attribute private string CN; // mandatory attribute private string userpassword; // optional attribute private string telephonenumber; // optional attribute private string seealso; // optional attribute private string description; // optional attribute Public String getsn () {return Sn ;} public void setsn (string Sn) {This. sn = Sn;} Public String getcn () {return CN;} public void setcn (string CN) {this.cn = cn;} Public String getuserpassword () {return userpassword ;} public void setuserpassword (string userpassword) {This. userpassword = userpassword;} Public String gettelephonenumber () {return telephonenumber;} public void settelephonenumber (string telephonenumber) {This. telephonenumber = telephonenumber;} Public String getseealso () {return seealso;} public void setseealso (string seealso) {This. seealso = seealso;} Public String getdescription () {return description;} public void setdescription (string description) {This. description = description ;}}

Mapper class:

Package LDAP. mapper; import javax. naming. namingexception; import javax. naming. directory. attributes; import LDAP. entity. person; import Org. springframework. LDAP. core. attributesmapper;/*** this class is used to convert attributes in LDAP to attribute values of the object class, * When querying information, */public class personattributemapper implements attributesmapper {@ overridepublic object mapfromattributes (attributes ATTR) throws namingexception {person = new person () is used (); Person. setsn (string) ATTR. get ("Sn "). get (); person. setcn (string) ATTR. get ("cn "). get (); If (ATTR. get ("userpassword ")! = NULL) {person. setuserpassword (string) ATTR. Get ("userpassword"). Get ();} If (ATTR. Get ("telephonenumber ")! = NULL) {person. settelephonenumber (string) ATTR. Get ("telephonenumber"). Get () ;}if (ATTR. Get ("seealso ")! = NULL) {person. setseealso (string) ATTR. Get ("seealso"). Get () ;}if (ATTR. Get ("Description ")! = NULL) {person. setdescription (string) ATTR. Get ("Description"). Get () ;}return person ;}}

DAO class:

Package LDAP. dao; import Java. util. arraylist; import Java. util. list; import javax. naming. directory. attributes; import javax. naming. directory. basicattribute; import javax. naming. directory. basicattributes; import javax. naming. directory. dircontext; import javax. naming. directory. modificationitem; import LDAP. entity. person; import LDAP. mapper. personattributemapper; import Org. springframework. LDAP. namenotfoundexce Ption; import Org. springframework. LDAP. core. distinguishedname; import Org. springframework. LDAP. core. ldaptemplate; import Org. springframework. LDAP. filter. andfilter; import Org. springframework. LDAP. filter. ipvsfilter; import xhrd. ucenter. LDAP. entity. ucenterldapapplication; import xhrd. ucenter. LDAP. ldapattributemappper. applicationattributemapper;/*** Description: This class is used to add, delete, modify, and query LDAP using spring ldaptemplate. Operation * Author: Ding Chengyun */public class persondao {// inject spring ldaptemplate. Here, you need to configure private ldaptemplate in the spring configuration file; Public ldaptemplate getldaptemplate () {return ldaptemplate;} public void setldaptemplate (ldaptemplate) {This. ldaptemplate = ldaptemplate;}/*** Add a record * @ Param person */Public void createoneperson (person) {basicattribute BA = new basicattribute ("objec Tclass "); BA. add ("person"); // The person here corresponds to the core. objectclass: personattributes ATTR = new basicattributes (); ATTR. put (BA); // required attribute. It cannot be null or an empty string ATTR. put ("cn", person. getcn (); ATTR. put ("Sn", person. getsn (); // The optional field needs to be determined whether it is null. If it is null, you cannot add If (person. getdescription ()! = NULL & person. getdescription (). length ()> 0) {ATTR. put ("Description", person. getdescription ();} If (person. getuserpassword ()! = NULL & person. getuserpassword (). length ()> 0) {ATTR. put ("userpassword", person. getuserpassword ();} If (person. getseealso ()! = NULL & person. getseealso (). length ()> 0) {ATTR. put ("seealso", person. getseealso ();} If (person. gettelephonenumber ()! = NULL & person. gettelephonenumber (). length ()> 0) {ATTR. put ("telephonenumber", person. gettelephonenumber ();} // The bind method adds a record. Ldaptemplate. BIND (getdn (person. getcn (), null, ATTR);}/**/*** query details by DN * @ Param Cn * @ return */Public ucenterldapapplication getpersondetail (string CN) {try {// The lookup method of ldapteplate is to query by DN. This query is highly efficient. ucenterldapapplication UA = (ucenterldapapplication) ldaptemplate. lookup (getdn (CN), new applicationattributemapper (); Return UA;} catch (namenotfoundexception e) {return NULL ;}/ *** query the person Column Based on the custom property value Table * @ Param person * @ return */public list <person> getpersonlist (person) {list <person> List = new arraylist <person> (); // query the filtering condition andfilter = new andfilter (); andfilter. and (New jsonsfilter ("objectclass", "person"); If (person. getcn ()! = NULL & person. getcn (). length ()> 0) {andfilter. and (new pair sfilter ("cn", person. getcn ();} If (person. getsn ()! = NULL & person. getsn (). length ()> 0) {andfilter. and (new pair sfilter ("Sn", person. getsn ();} If (person. getdescription ()! = NULL & person. getdescription (). length ()> 0) {andfilter. and (new vertex sfilter ("Description", person. getdescription ();} If (person. getuserpassword ()! = NULL & person. getuserpassword (). length ()> 0) {andfilter. and (new cipher sfilter ("userpassword", person. getuserpassword ();} If (person. getseealso ()! = NULL & person. getseealso (). length ()> 0) {andfilter. and (new pair sfilter ("seealso", person. getseealso ();} If (person. gettelephonenumber ()! = NULL & person. gettelephonenumber (). length ()> 0) {andfilter. and (New ipvsfilter ("telephonenumber", person. gettelephonenumber ();} // search queries based on filtering conditions. The first parameter is the DN of the parent node, which can be blank, if not empty, the query efficiency is higher. List = ldaptemplate. search ("", andfilter. encode (), new personattributemapper (); return list;}/*** delete a record, according to DN * @ Param Cn */Public void removeoneperson (string CN) {ldaptemplate. unbind (getdn (CN);}/*** modify operation * @ Param person */Public void updateoneperson (person) {If (person = NULL | person. getcn () = NULL | person. getcn (). length () <= 0) {return ;}list <modificationitem> MList = new arraylist <modificationitem> (); MList. add (New modificationitem (dircontext. replace_attribute, new basicattribute ("Sn", person. getsn (); MList. add (New modificationitem (dircontext. replace_attribute, new basicattribute ("Description", person. getdescription (); MList. add (New modificationitem (dircontext. replace_attribute, new basicattribute ("seealso", person. getseealso (); MList. add (New modificationitem (dircontext. replace_attribute, new basicattribute ("telephonenumber", person. gettelephonenumber (); MList. add (New modificationitem (dircontext. replace_attribute, new basicattribute ("userpassword", person. getuserpassword (); If (MList. size ()> 0) {modificationitem [] marray = new modificationitem [MList. size ()]; for (INT I = 0; I <MList. size (); I ++) {marray [I] = MList. get (I);} // The modifyattributes method is used to modify the object. ldaptemplate must be differentiated from the rebind () method. modifyattributes (this. getdn (person. getcn (), marray) ;}}/*** get DN * @ Param Cn * @ return */private distinguishedname getdn (string CN) {// get the root directory, that is, the root directory distinguishedname newcontactdn = new distinguishedname (); // Add CN in the configuration file, even if the DN of this record is "cn = Cn, root directory ", such as" cn = ABC, Dc = testdc, Dc = com "newcontactdn. add ("cn", CN); Return newcontactdn ;}}

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.