package ldap.pojo;

import java.util.List;

/**
 * @author 张亮 
 * ldap用户属性信息数据类
 */
public class LdapPersonInfo {

    // ldap中用户的uid属性
    private String uid;

    // ldap中用户的givenName属性
    private String firstName;

    // ldap中用户的sn属性

    private String lastName;

    // ldap中用户的cn属性

    private List cn;

    // ldap中用户的telephonenumber属性
    private String telephone;

    // ldap中用户的facsimiletelephonenumber属性
    private String fax;

    // ldap中用户的mail属性
    private String mail;

    public LdapPersonInfo() {

    }

    public LdapPersonInfo(String uid, String firstName, String lastName,
            List cn, String telephone, String fax, String mail) {
        this.uid = uid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.cn = cn;
        this.telephone = telephone;
        this.fax = fax;
        this.mail = mail;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public List getCn() {
        return cn;
    }

    public void setCn(List cn) {
        this.cn = cn;
    }
}

UserDaoLdapImpl for jdk1.42

; i < dnlist.size(); i++) {
            if (loginCheack(dnlist.get(i).toString(), password) == true) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查询用户user dn
     * 
     * @param uid
     *            用户uid
     * 
     * @return 用户dn列表,当前目录节点下可能存在多个相同uid的多个user dn
     */
    public List getUserDnByUid(String uid) {
        // 获取DirContext对象
        DirContext ctx = ldapTemplate.getContextSource().getReadOnlyContext();
        // 存储用户dn
        ArrayList dn = new ArrayList();
        try {
            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration en = ctx.search("", "uid=" + uid, constraints);
            // 查询所有用户

            while (en != null && en.hasMoreElements()) {
                Object obj = en.nextElement();
                if (obj instanceof SearchResult) {
                    SearchResult si = (SearchResult) obj;
                    // 获取dn并添加到查询列表
                    si.getName();
                    Hashtable ht = new Hashtable();
                    // 获取已有的登陆信息
                    ht = (Hashtable) ctx.getEnvironment();
                    // 设置用户
                    String str = ht.get(this.BASEDN_KEY).toString();
                    dn.add(si.getName()+","+str);
                }
            }
            ctx.close();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                ctx.close();
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
        return dn;
    }

    /**
     * ldap用户信息数据填充类 将获取的属性信息封装为LdapObject对象
     * 
     */
    private class LdapObjectAttributesMapper implements AttributesMapper {
        public Object mapFromAttributes(Attributes attrs)
                throws NamingException {
            LdapPersonInfo LdapObject = new LdapPersonInfo();
            try {
                // 获取并封装uid属性

                LdapObject.setUid((String) getAttribute(attrs, "uid"));
                // 获取并封装givenname属性

                LdapObject.setFirstName((String) getAttribute(attrs,
                        "givenname"));
                // 获取并封装sn属性

                LdapObject.setLastName((String) getAttribute(attrs, "sn"));
                // 获取并封装cn属性

                LdapObject.setCn(getMoreSameAttributes(attrs, "cn"));
                // 获取并封装telephonenumber属性

                LdapObject.setTelephone((String) getAttribute(attrs,
                        "telephonenumber"));
                // 获取并封装facsimiletelephonenumber属性

                LdapObject.setFax((String) getAttribute(attrs,
                        "facsimiletelephonenumber"));
                // 获取并封装mail属性

                LdapObject.setMail((String) getAttribute(attrs, "mail"));
            } catch (NamingException n) {
                n.printStackTrace();
            }
            // 返回封装后的用户对象
            return LdapObject;
        }

        /**
         * 从属性列表中获取指定的属性
         * 
         * 
         * @param attrs
         *            属性列表
         * 
         * @param attrName
         *            需要获取的属性
         * 
         * @return 返回获取的属性值
         * 
         * @throws NamingException
         */
        private String getAttribute(Attributes attrs, String attrName)
                throws NamingException {
            Attribute attr = attrs.get(attrName);
            // 若没有指定的属性返回空字符串

            if (attr == null) {
                return "";
            } else {
                return (String) attr.get();
            }
        }

        /**
         * 从属性列表中获取指定的属性的所有属性值
         * 
         * 
         * @param attrs
         *            属性列表
         * 
         * @param attrName
         *            需要获取的属性
         * 
         * @return 返回获取的属性值
         * 
         * @throws NamingException
         */
        private List getMoreSameAttributes(Attributes attrs, String attrName)
                throws NamingException {

            Attribute attr = attrs.get(attrName);
            List elelist = new ArrayList();
            // 若没有指定的属性返回null
            if (attr == null) {
                return null;
            } else {
                // 获取当前属性的所有值,添加到返回列表中
                Enumeration ent = attr.getAll();
                while (ent.hasMoreElements())
                    elelist.add(ent.nextElement().toString());
                return elelist;
            }
        }
    }

    private void dispPerson(LdapPersonInfo temp) {
        System.out.println("-----------------------------");
        System.out.println("User(uid: " + temp.getUid() + ") listing...");
        System.out.println("First Name: " + temp.getFirstName());
        System.out.println("Last Name: " + temp.getLastName());
        System.out.println("Common Name: " + temp.getCn());
        System.out.println("User ID: " + temp.getUid());
        System.out.println("E-Mail: " + temp.getMail());
        System.out.println("Phone: " + temp.getTelephone());
        System.out.println("Fax: " + temp.getFax());
        System.out.println("List completed.");
        System.out.println("-----------------------------n");
    }


}

java对Ldap操作2的更多相关文章

  1. JAVA使用Ldap操作AD域

    项目上遇到的需要在集成 操作域用户的信息的功能,第一次接触ad域,因为不了解而且网上其他介绍不明确,比较费时,这里记录下. 说明: (1). 特别注意:Java操作查询域用户信息获取到的数据和域管理员 ...

  2. java对Ldap操作1

    package ldap;import java.util.List;import ldap.pojo.LdapPersonInfo;/** * access Ldap *  * @author 张亮 ...

  3. java对Ldap操作4

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE  ...

  4. Java下LDAP操作的资料

    话说LDAP真是个诡异的protocol(或者数据库,或者服务,whatever...),没有一个特别形象的spec.这里列出一些筛选出的还可以的文档,都是oracle的: https://docs. ...

  5. java对Ldap操作3

    "));    }}

  6. OpenLDAP使用疑惑解答及使用Java完成LDAP身份认证

    导读 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读取,浏览 ...

  7. 利用LDAP操作AD域

    LDAP操作代码样例  初始化LDAP 目录服务上下文 该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ld ...

  8. Java Spring mvc 操作 Redis 及 Redis 集群

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...

  9. Java的JDBC操作

    Java的JDBC操作 [TOC] 1.JDBC入门 1.1.什么是JDBC JDBC从物理结构上来说就是java语言访问数据库的一套接口集合,本质上是java语言根数据库之间的协议.JDBC提供一组 ...

随机推荐

  1. C# 虚方法 抽象方法 接口

    虚方法:virtu 注意的几点: 1,父类中如果有方法让子类重写,则可以将该方法标记为virtual 2.虚方法在父类中必须有实现,哪怕是空实现 3虚方法子类可以重写,也可以不重写 4.如果类是抽象类 ...

  2. Java stackoverflow error

    本文想记录一下尝试产生stackoverflow的程序 1 -Xss=1k, 设置stack大小1024个字节,产生515个long,想把stack撑爆. 2 嵌套调用 3 创建大量线程 1 -Xss ...

  3. 导出文本、表格、图像到PDF格式文件中(学习整理)

    1.测试例子: 需要导入的外部jar包: 相关API http://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/pack ...

  4. 打印出不同顺序的字符串&单引号和双引号的差异

    发现一个很好玩的打印顺序 package com.liaojianya.chapter1; /** * This program demonstrates the string. * @author ...

  5. 洛谷 U2878 小R的分数比赛(fraction)

    题目提供者 2015c07 标签 数论(数学相关) 高精度 难度 尚无评定 通过/提交 0/29 提交该题 记录 题目背景 P5难度系数:★★★☆☆ 小R再次挑战你. 这次的挑战又会是什么呢? 题目描 ...

  6. print,print_r,echo,var_dump,var_export比较

    print string 1个参数 返回1 语言结构echo 多个string 无返回 语言结构 print_r array 如果想捕捉 print_r() 的输出,可使用 return 参数.若此参 ...

  7. 那些年被我坑过的Python——邂逅与初识(第一章)

    第一问:为什么学习Python? 虚妖说:为了还债,还技术债,很早接触编程,却一直徘徊,也码了很多代码,却从未真真学会编程! 第二问:什么是Python 是一种以简洁.优雅著称的解释型.动态.强类型的 ...

  8. c++清除输入缓冲区之 sync() vs ignore()

    最近在写程序的时候总是不注意输入缓冲区内是否还有东西,导致出现了一些异常,调试了半天.所以来上一贴,学习注意,引以为戒! http://blog.chinaunix.net/uid-21254310- ...

  9. ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller

    ASP.NET MVC中的Model(数据模型)主要包括定义数据结构.数据库读写.数据验证等等和对象处理相关的工作. 在解决方案资源管理器中找到Model文件夹,点击右键,添加一个新类,名为“Mess ...

  10. 【转】Spring注解@Component、@Repository、@Service、@Controller区别

    http://blog.csdn.net/zhang854429783/article/details/6785574 很长时间没做web项目都把以前学的那点框架知识忘光了,今天把以前做的一个项目翻出 ...