這部分的樣本網上的確有很多,但是個人在尋找的過程中還是感到不夠滿意,所以就自己總結一下,方便自己以後查閱,也方便其他童鞋尋找資料。
springLdap 操作ldap樣本(增刪改查)
在看這個文章之前,最好是瞭解了openldap的schema檔案,也就是瞭解objectClass和attribute以及它們的關係。否則很容易不瞭解代碼的含義以及拋出的異常。
實體類:
package ldap.entity;/** * 本測試類別person對象來自schema檔案的core.schema檔案 * objectClass為person,必填屬性和可選屬性也是根據該對象得到的。 * Author:Ding Chengyun */public class Person {private String sn; //必填屬性private String cn; //必填屬性private String userPassword; //可選屬性private String telephoneNumber; //可選屬性private String seeAlso; //可選屬性private String description; //可選屬性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類:
package ldap.mapper;import javax.naming.NamingException;import javax.naming.directory.Attributes;import ldap.entity.Person;import org.springframework.ldap.core.AttributesMapper;/** * 這個類的作用是將ldap中的屬性轉化為實體類的屬性值, * 在查詢資訊的時候會用到 */public class PersonAttributeMapper implements AttributesMapper{@Overridepublic Object mapFromAttributes(Attributes attr) throws NamingException {Person person = new Person();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類:
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.NameNotFoundException;import org.springframework.ldap.core.DistinguishedName;import org.springframework.ldap.core.LdapTemplate;import org.springframework.ldap.filter.AndFilter;import org.springframework.ldap.filter.EqualsFilter;import xhrd.ucenter.ldap.entity.UcenterLdapApplication;import xhrd.ucenter.ldap.ldapAttributeMappper.ApplicationAttributeMapper;/** * Description: 此類的作用是使用spring的 LdapTemplate完成對ldap的增刪改查的操作 * Author:Ding Chengyun */public class PersonDao {//注入spring的LdapTemplate,此處在spring的設定檔中需要配置private LdapTemplate ldapTemplate;public LdapTemplate getLdapTemplate() {return ldapTemplate;}public void setLdapTemplate(LdapTemplate ldapTemplate) {this.ldapTemplate = ldapTemplate;}/** * 添加 一條記錄 * @param person */public void createOnePerson(Person person) {BasicAttribute ba = new BasicAttribute("objectclass");ba.add("person"); //此處的person對應的是core.schema檔案中的objectClass:personAttributes attr = new BasicAttributes();attr.put(ba);//必填屬性,不能為null也不可為空字串attr.put("cn", person.getCn());attr.put("sn", person.getSn());//可選欄位需要判斷是否為空白,如果為空白則不能添加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());}//bind方法即是添加一條記錄。ldapTemplate.bind(getDn(person.getCn()), null, attr);}/**/** * 根據dn查詢詳細資料 * @param cn * @return */public UcenterLdapApplication getPersonDetail(String cn) {try {//ldapTeplate的lookup方法是根據dn進行查詢,此查詢的效率超高UcenterLdapApplication ua = (UcenterLdapApplication) ldapTemplate.lookup(getDn(cn),new ApplicationAttributeMapper());return ua;} catch (NameNotFoundException e) {return null;}}/** * 根據自訂的屬性值查詢person列表 * @param person * @return */public List<Person> getPersonList(Person person) {List<Person> list = new ArrayList<Person>();//查詢過濾條件AndFilter andFilter = new AndFilter();andFilter.and(new EqualsFilter("objectclass", "person"));if (person.getCn() != null&& person.getCn().length() > 0) {andFilter.and(new EqualsFilter("cn", person.getCn()));}if (person.getSn() != null&& person.getSn().length() > 0) {andFilter.and(new EqualsFilter("sn", person.getSn()));}if (person.getDescription() != null&& person.getDescription().length() > 0) {andFilter.and(new EqualsFilter("description", person.getDescription()));}if (person.getUserPassword() != null&& person.getUserPassword().length() > 0) {andFilter.and(new EqualsFilter("userPassword", person.getUserPassword()));}if (person.getSeeAlso() != null&& person.getSeeAlso().length() > 0) {andFilter.and(new EqualsFilter("seeAlso", person.getSeeAlso()));}if (person.getTelephoneNumber() != null && person.getTelephoneNumber().length() > 0) {andFilter.and(new EqualsFilter("telephoneNumber", person.getTelephoneNumber()));}//search是根據過濾條件進行查詢,第一個參數是父節點的dn,可以為空白,不為空白時查詢效率更高list = ldapTemplate.search("", andFilter.encode(),new PersonAttributeMapper());return list;}/** * 刪除一條記錄,根據dn * @param cn */public void removeOnePerson(String cn) {ldapTemplate.unbind(getDn(cn));}/** * 修改操作 * @param person */public void updateOnePerson(Person 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);}//modifyAttributes 方法是修改對象的操作,與rebind()方法需要區別開ldapTemplate.modifyAttributes(this.getDn(person.getCn()), mArray);}}/** * 得到dn * @param cn * @return */private DistinguishedName getDn(String cn) {//得到根目錄,也就是設定檔中配置的ldap的根目錄DistinguishedName newContactDN = new DistinguishedName();// 添加cn,即使得該條記錄的dn為"cn=cn,根目錄",例如"cn=abc,dc=testdc,dc=com"newContactDN.add("cn", cn);return newContactDN;}}