Spring ldap 1.3.1整合Java Enterprise System Directory Server 6

来源:互联网 发布:机器码查询软件 编辑:程序博客网 时间:2024/06/05 17:29

公司统一登录系统重构,其中LDAP原先采用的最原始的方式操作LDAP,这项目是三年前的项目了;现在团队内新项目都用Spring3.11框架,而Spring也提供了针对LDAP的Template模式,而团队的工具包里有对Hibernate、JPA、JDBC等Template的二次封装,二次封装后的更适合队内使用,LDAP也想做二次封装,要求API与Hibernate、JPA、JDBC一致,而在封装的过程碰到一些问题:

一、有关base的配置

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
  <property name="url" value="ldap://192.168.1.18:9010" />

<!-此处配了base在添加、更新、删除和查询也即ldapTemplate.bind、ldapTemplate.modifyAttributes/rebind、ldapTemplate.unbind和ldapTemplate.search

     提供dn时不需要给出全路径

    我的角色全路径是cn=role1,ou=role,dc=test,dc=com,构建dn的方法只要按如下方式写即可:

    final public Name buildDn() {

          //DistinguishedName构造时不要提供base,因为ContextSource已经配置了,后面都会从base下开始找
          DistinguishedName dn = new DistinguishedName();
         dn.add("ou", "role");
         dn.add("cn", id);
         return dn;
 }

->
  <property name="base" value="dc=test,dc=com" />
  <property name="userDn" value="cn=admin" />
  <property name="password" value="pwd" />
  <property name="pooled" value="true" />
 </bean>

二、有关添加时Attributes构造

 1、objectClass初始要求了解节点相应schema否则很难提供准确的父类,这个最好去DSCC看了

  2、必填字段必须提供值否则也无法正确添加,属性的必填与非必填也可以去DSCC查看

最后能完成对LdapTemplate二次封装给团队提供通用的insert、update、delete和selectById/selectAll有点小高兴,因为最近状态实在不佳,脑袋里装的事太多了

这里把核心代码共享下:

public abstract class BaseRepositoryLdapImpl<Id extends Serializable, E extends LdapEntity<Id, ?>>
  implements IBaseRepository<Id, E> {
 protected LdapTemplate ldapTemplate;

 @Override
 public void insertEntity(E e) {
  ldapTemplate.bind(e.buildDn(), null, e.buildAttributes());
 }

 @Override
 public void updateEntity(E e) {
  ldapTemplate.modifyAttributes(e.buildDn(), e.buildModificationItem());
 }

 @Override
 public void deleteEntity(E e) {
  ldapTemplate.unbind(e.buildDn());
 }

 @Override
 public E selectEntityBy(Id id, final LdapEntityUtil<E> lu) {
  SearchControls sc = new SearchControls();
  sc.setReturningAttributes(lu.getAttributes());
  @SuppressWarnings("unchecked")
  List<E> elist = ldapTemplate.search(lu.getBase(), "(cn=" + id + ")",
    sc, new AttributesMapper() {
     @Override
     public E mapFromAttributes(Attributes attributes)
       throws NamingException {
      return lu.attributes2Entity(attributes);
     }
    });
  if (1 == elist.size())
   return elist.get(0);
  return null;
 }

 @SuppressWarnings("unchecked")
 @Override
 public List<E> selectAllEntity(final LdapEntityUtil<E> lu) {
  SearchControls sc = new SearchControls();
  sc.setReturningAttributes(lu.getAttributes());
  List<E> elist = new ArrayList<E>();
  elist.addAll(ldapTemplate.search(lu.getBase(), "(cn=*)",
    sc, new AttributesMapper() {
   @Override
   public E mapFromAttributes(Attributes attributes)
     throws NamingException {
    return lu.attributes2Entity(attributes);
   }
  }));
  return elist;
 }

 public void setLdapTemplate(LdapTemplate ldapTemplate) {
  this.ldapTemplate = ldapTemplate;
 }

}

对了这里参阅了相关贴子:http://jueshizhanhun.blog.51cto.com/4372226/1232271

 

0 0
原创粉丝点击