Spring-ldap 操作LDAP

来源:互联网 发布:手机黑客攻击软件 编辑:程序博客网 时间:2024/05/23 01:13

在看这个文章之前,最好是了解了openldap的schema文件,也就是了解objectClass和attribute以及它们的关系。否则很容易不了解代码的含义以及抛出的异常。

实体类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package ldap.entity;  
  2.   
  3. /** 
  4.  * 本测试类person对象来自schema文件的core.schema文件 
  5.  * objectClass为person,必填属性和可选属性也是根据该对象得到的。 
  6.  * Author:Ding Chengyun 
  7.  */  
  8. public class Person {  
  9.   
  10.     private String sn; //必填属性  
  11.     private String cn; //必填属性  
  12.       
  13.     private String userPassword; //可选属性  
  14.     private String telephoneNumber; //可选属性  
  15.     private String seeAlso; //可选属性  
  16.     private String description;  //可选属性  
  17.     public String getSn() {  
  18.         return sn;  
  19.     }  
  20.     public void setSn(String sn) {  
  21.         this.sn = sn;  
  22.     }  
  23.     public String getCn() {  
  24.         return cn;  
  25.     }  
  26.     public void setCn(String cn) {  
  27.         this.cn = cn;  
  28.     }  
  29.     public String getUserPassword() {  
  30.         return userPassword;  
  31.     }  
  32.     public void setUserPassword(String userPassword) {  
  33.         this.userPassword = userPassword;  
  34.     }  
  35.     public String getTelephoneNumber() {  
  36.         return telephoneNumber;  
  37.     }  
  38.     public void setTelephoneNumber(String telephoneNumber) {  
  39.         this.telephoneNumber = telephoneNumber;  
  40.     }  
  41.     public String getSeeAlso() {  
  42.         return seeAlso;  
  43.     }  
  44.     public void setSeeAlso(String seeAlso) {  
  45.         this.seeAlso = seeAlso;  
  46.     }  
  47.     public String getDescription() {  
  48.         return description;  
  49.     }  
  50.     public void setDescription(String description) {  
  51.         this.description = description;  
  52.     }  
  53.       
  54. }  

mapper类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package ldap.mapper;  
  2.   
  3. import javax.naming.NamingException;  
  4. import javax.naming.directory.Attributes;  
  5.   
  6. import ldap.entity.Person;  
  7.   
  8. import org.springframework.ldap.core.AttributesMapper;  
  9.   
  10. /** 
  11.  * 这个类的作用是将ldap中的属性转化为实体类的属性值, 
  12.  * 在查询信息的时候会用到 
  13.  */  
  14. public class PersonAttributeMapper implements AttributesMapper{  
  15.   
  16.     @Override  
  17.     public Object mapFromAttributes(Attributes attr) throws NamingException {  
  18.         Person person = new Person();  
  19.         person.setSn((String)attr.get("sn").get());  
  20.         person.setCn((String)attr.get("cn").get());  
  21.           
  22.         if (attr.get("userPassword") != null) {  
  23.             person.setUserPassword((String)attr.get("userPassword").get());  
  24.         }  
  25.         if (attr.get("telephoneNumber") != null) {  
  26.             person.setTelephoneNumber((String)attr.get("telephoneNumber").get());  
  27.         }  
  28.         if (attr.get("seeAlso") != null) {  
  29.             person.setSeeAlso((String)attr.get("seeAlso").get());  
  30.         }  
  31.         if (attr.get("description") != null) {  
  32.             person.setDescription((String)attr.get("description").get());  
  33.         }  
  34.         return person;  
  35.     }  
  36.   
  37. }  

dao类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package ldap.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.naming.directory.Attributes;  
  7. import javax.naming.directory.BasicAttribute;  
  8. import javax.naming.directory.BasicAttributes;  
  9. import javax.naming.directory.DirContext;  
  10. import javax.naming.directory.ModificationItem;  
  11.   
  12. import ldap.entity.Person;  
  13. import ldap.mapper.PersonAttributeMapper;  
  14.   
  15. import org.springframework.ldap.NameNotFoundException;  
  16. import org.springframework.ldap.core.DistinguishedName;  
  17. import org.springframework.ldap.core.LdapTemplate;  
  18. import org.springframework.ldap.filter.AndFilter;  
  19. import org.springframework.ldap.filter.EqualsFilter;  
  20.   
  21. import xhrd.ucenter.ldap.entity.UcenterLdapApplication;  
  22. import xhrd.ucenter.ldap.ldapAttributeMappper.ApplicationAttributeMapper;  
  23.   
  24. /** 
  25.  * Description: 此类的作用是使用spring的 LdapTemplate完成对ldap的增删改查的操作 
  26.  * Author:Ding Chengyun 
  27.  */  
  28. public class PersonDao {  
  29.   
  30.     //注入spring的LdapTemplate,此处在spring的配置文件中需要配置  
  31.     private LdapTemplate ldapTemplate;  
  32.   
  33.     public LdapTemplate getLdapTemplate() {  
  34.         return ldapTemplate;  
  35.     }  
  36.     public void setLdapTemplate(LdapTemplate ldapTemplate) {  
  37.         this.ldapTemplate = ldapTemplate;  
  38.     }  
  39.       
  40.     /** 
  41.      * 添加 一条记录 
  42.      * @param person 
  43.      */  
  44.     public void createOnePerson(Person person) {  
  45.         BasicAttribute ba = new BasicAttribute("objectclass");  
  46.         ba.add("person"); //此处的person对应的是core.schema文件中的objectClass:person  
  47.         Attributes attr = new BasicAttributes();  
  48.         attr.put(ba);  
  49.         //必填属性,不能为null也不能为空字符串  
  50.         attr.put("cn", person.getCn());  
  51.         attr.put("sn", person.getSn());  
  52.   
  53.         //可选字段需要判断是否为空,如果为空则不能添加  
  54.         if (person.getDescription() != null  
  55.                 && person.getDescription().length() > 0) {  
  56.             attr.put("description", person.getDescription());  
  57.         }  
  58.   
  59.         if (person.getUserPassword() != null  
  60.                 && person.getUserPassword().length() > 0) {  
  61.             attr.put("userPassword", person.getUserPassword());  
  62.         }  
  63.         if (person.getSeeAlso() != null  
  64.                 && person.getSeeAlso().length() > 0) {  
  65.             attr.put("seeAlso", person.getSeeAlso());  
  66.         }  
  67.         if (person.getTelephoneNumber() != null   
  68.                 && person.getTelephoneNumber().length() > 0) {  
  69.             attr.put("telephoneNumber", person.getTelephoneNumber());  
  70.         }  
  71.           
  72.         //bind方法即是添加一条记录。  
  73.         ldapTemplate.bind(getDn(person.getCn()), null, attr);  
  74.     }  
  75.   
  76.     /** 
  77.      
  78.     /** 
  79.      * 根据dn查询详细信息 
  80.      * @param cn 
  81.      * @return 
  82.      */  
  83.     public UcenterLdapApplication getPersonDetail(String cn) {  
  84.         try {  
  85.             //ldapTeplate的lookup方法是根据dn进行查询,此查询的效率超高  
  86.             UcenterLdapApplication ua = (UcenterLdapApplication)   
  87.                 ldapTemplate.lookup(getDn(cn),  
  88.                         new ApplicationAttributeMapper());  
  89.             return ua;  
  90.         } catch (NameNotFoundException e) {  
  91.             return null;  
  92.         }  
  93.     }  
  94.   
  95.     /** 
  96.      * 根据自定义的属性值查询person列表 
  97.      * @param person 
  98.      * @return 
  99.      */  
  100.     public List<Person> getPersonList(  
  101.             Person person) {  
  102.         List<Person> list = new ArrayList<Person>();  
  103.         //查询过滤条件  
  104.         AndFilter andFilter = new AndFilter();  
  105.         andFilter.and(new EqualsFilter("objectclass""person"));  
  106.           
  107.           
  108.         if (person.getCn() != null  
  109.                 && person.getCn().length() > 0) {  
  110.             andFilter.and(new EqualsFilter("cn", person.getCn()));  
  111.         }  
  112.         if (person.getSn() != null  
  113.                 && person.getSn().length() > 0) {  
  114.             andFilter.and(new EqualsFilter("sn", person.getSn()));  
  115.         }  
  116.   
  117.         if (person.getDescription() != null  
  118.                 && person.getDescription().length() > 0) {  
  119.             andFilter.and(new EqualsFilter("description", person.getDescription()));  
  120.         }  
  121.   
  122.         if (person.getUserPassword() != null  
  123.                 && person.getUserPassword().length() > 0) {  
  124.             andFilter.and(new EqualsFilter("userPassword", person.getUserPassword()));  
  125.         }  
  126.         if (person.getSeeAlso() != null  
  127.                 && person.getSeeAlso().length() > 0) {  
  128.             andFilter.and(new EqualsFilter("seeAlso", person.getSeeAlso()));  
  129.         }  
  130.         if (person.getTelephoneNumber() != null   
  131.                 && person.getTelephoneNumber().length() > 0) {  
  132.             andFilter.and(new EqualsFilter("telephoneNumber", person.getTelephoneNumber()));  
  133.         }  
  134.         //search是根据过滤条件进行查询,第一个参数是父节点的dn,可以为空,不为空时查询效率更高  
  135.         list = ldapTemplate.search("", andFilter.encode(),  
  136.                 new PersonAttributeMapper());  
  137.         return list;  
  138.     }  
  139.   
  140.     /** 
  141.      * 删除一条记录,根据dn 
  142.      * @param cn 
  143.      */  
  144.     public void removeOnePerson(String cn) {  
  145.         ldapTemplate.unbind(getDn(cn));  
  146.     }  
  147.   
  148.     /** 
  149.      * 修改操作 
  150.      * @param person 
  151.      */  
  152.     public void updateOnePerson(Person person) {  
  153.         if (person == null || person.getCn() == null   
  154.                 || person.getCn().length() <= 0) {  
  155.             return;  
  156.         }  
  157.         List<ModificationItem> mList = new ArrayList<ModificationItem>();  
  158.           
  159.         mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,  
  160.                 new BasicAttribute("sn",person.getSn())));  
  161.         mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,  
  162.                 new BasicAttribute("description",person.getDescription())));  
  163.         mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,  
  164.                 new BasicAttribute("seeAlso",person.getSeeAlso())));  
  165.         mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,  
  166.                 new BasicAttribute("telephoneNumber",person.getTelephoneNumber())));  
  167.         mList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,  
  168.                 new BasicAttribute("userPassword",person.getUserPassword())));  
  169.           
  170.         if (mList.size() > 0) {  
  171.             ModificationItem[] mArray = new ModificationItem[mList.size()];  
  172.             for (int i = 0; i < mList.size(); i++) {  
  173.                 mArray[i] = mList.get(i);  
  174.             }  
  175.             //modifyAttributes 方法是修改对象的操作,与rebind()方法需要区别开  
  176.             ldapTemplate.modifyAttributes(this.getDn(person.getCn()), mArray);  
  177.         }  
  178.     }  
  179.     /** 
  180.      * 得到dn 
  181.      * @param cn 
  182.      * @return 
  183.      */  
  184.     private DistinguishedName getDn(String cn) {  
  185.         //得到根目录,也就是配置文件中配置的ldap的根目录  
  186.         DistinguishedName newContactDN = new DistinguishedName();  
  187.         // 添加cn,即使得该条记录的dn为"cn=cn,根目录",例如"cn=abc,dc=testdc,dc=com"  
  188.         newContactDN.add("cn", cn);  
  189.         return newContactDN;  
  190.     }  
  191. }  
0 0