LDAP学习踩坑

Spring Data LDAP官网链接,收录很全

什么是LDAP?

(一)在介绍什么是LDAP之前,我们先来复习一个东西:“什么是目录服务?

    1. 目录服务是一个特殊的数据库,用来保存描述性的、基于属性的详细信息,支持过滤功能。

    2. 是动态的,灵活的,易扩展的。

    如:人员组织管理,电话簿,地址簿。

(二)了解完目录服务后,我们再来看看LDAP的介绍:

            LDAP(Light Directory Access Portocol),它是基于X.500标准的轻量级目录访问协议。

            目录是一个为查询、浏览和搜索而优化的数据库,它成树状结构组织数据,类似文件目录一样。

            目录数据库和关系数据库不同,它有优异的读性能,但写性能差,并且没有事务处理、回滚等复杂功能,不适于存储修改频繁的数据。所以目录天生是用来查询的,就好象它的名字一样。

            LDAP目录服务是由目录数据库和一套访问协议组成的系统。

(三)为什么要使用

            LDAP是开放的Internet标准,支持跨平台的Internet协议,在业界中得到广泛认可的,并且市场上或者开源社区上的大多产品都加入了对LDAP的支持,因此对于这类系统,不需单独定制,只需要通过LDAP做简单的配置就可以与服务器做认证交互。“简单粗暴”,可以大大降低重复开发和对接的成本。

//测试连接使用LdapTemplate   base 项目中只配置 一处就好 配多容易查不出数据
public class Connection {

    private static final LdapTemplate ldapTemplate;
    
    //dc ,dc 是根目录
    public static final String BASE_DN = "dc=?,dc=?";
    //ou 相当于叶子结点
    public static final String BASE_OU = "?";

    protected Name buildDn(Person p) {
        return LdapNameBuilder.newInstance()
                .add("ou",BASE_OU)
                .add("cn", p.getCn())
                .build();
    }
    static {
        LdapContextSource cs = new LdapContextSource();
        cs.setCacheEnvironmentProperties(false);
        cs.setUrl(?);
        cs.setBase();
        cs.setAuthenticationSource(
                new AuthenticationSource() {
                    @Override
                    //psw
                    public String getCredentials() {
                        return ?;
                    }
                     //组织路径
                    @Override
                    public String getPrincipal() {
                        return ?;
                    }
                });
        //spring 封住提供的操作ldap的类 
        ldapTemplate = new LdapTemplate(cs);
    }
    //set 对应的实体属性,将查询返回的结果映射成对象
    private  class PersonAttributesMapper implements AttributesMapper<Person> {
        public Person mapFromAttributes(Attributes attrs) throws NamingException {
            Person person = new Person();
            person.setUidNumber(?);
            return person;
        }
    }

    //查询所有信息
    public List search(){
        return ldapTemplate.findAll(Person.class);
    }
    
    //精确查找
    public Object searchOne(String position){
        return  ldapTemplate.lookup(position);
    }
    
    //只查询某个对象
    public Object searchParam(){
         List<Person> person = (List<Person>) ldapTemplate.search(
                query().base(?)
                .attributes(?)
                .where("objectclass").is("person"),
                new PersonAttributesMapper());
         return person.get(0).get??();
    }

    //每当在LDAP树中找到一个条目时,Spring LDAP就会使用其属性和专有名称(DN)来构造一个DirContextAdapter。
    // 这使我们可以使用a ContextMapper代替an AttributesMapper 来转换找到的值
    private static class PersonContextMapper implements ContextMapper {
        public Object mapFromContext(Object ctx) {
            DirContextAdapter context = (DirContextAdapter)ctx;
            Person p = new Person();
            p.setCn(context.getStringAttribute("cn"));
            。。。
            return p;
        }
    }
    //新建角色
    public void create(Person p) {
        Name dn = buildDn(p);

        DirContextAdapter context = new DirContextAdapter(dn);
        //自定义属性使用setAttributeValues
        context.setAttributeValues("objectclass", new String[] {"", "","",""});
        contest.set(所有属性)

        ldapTemplate.bind(context);
    }
    //删除信息
    public void delete(Person p) {
        Name dn = buildDn(p);
        ldapTemplate.unbind(dn);
    }
    //修改信息
    public void update(Person p) {
        Name dn = buildDn(p);
        DirContextOperations context = ldapTemplate.lookupContext(dn);
        。。。。。
        context.setAttributeValue("","");
        。。。。。
        ldapTemplate.modifyAttributes(context);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值