LDAP密码认证(验证)讲解与实践

2 篇文章 0 订阅

简述

  前阵子因为项目需要用到 LDAP ,发现从百度搜到的结果很少专一针对认证LDAP密码认证代码例子,废了很多功夫,很多的例子都是只是单单连接LDAP服务器(ctx = new InitialDirContext(env);)就完事了。所以特意奉上一段代码,对密码认证的。当然主要是对 获取某个DN下面的属性值进行对比 这样的一个操作。理论的话就不多说了,如果想了解 LDAP 以及想去了解调用LDAP接口的可以移步到我上一条转载的博客 【Java LDAP操作】http://blog.csdn.net/charlven/article/details/78032221 。

实现方式:

方式一:最简单的账号密码认证方式

直接拿到用户的账号密码即可认证,没什么可描述的,直接上代码!

代码示例

try {
        String username=email.split("@")[0];   //uid
        String bindUserDN = "uid="+username+",ou=people,dc=department";  //用户 DN
        String bindPassword = "*****"; //用户密码
        String url = "ldap://127.0.0.1:389/";  //ldap服务器IP
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(javax.naming.Context.PROVIDER_URL, url);
        env.put(javax.naming.Context.SECURITY_AUTHENTICATION, "simple");
        env.put(javax.naming.Context.SECURITY_PRINCIPAL, bindUserDN);
        env.put(javax.naming.Context.SECURITY_CREDENTIALS, bindPassword);
        javax.naming.directory.DirContext ctx = new javax.naming.directory.InitialDirContext(env);
        return "OK";
    } catch (Exception e) {
        // todo log
        return "FAILED";
    }

方式二:手动获取节点下的属性值进行校验

这种方式有点复杂,首先我们来整理一下获取LDAP密码认证的整体逻辑思路:

  1. 收集LDAP服务器信息 : 连接LDAP服务器账号和密码(连接每一个服务器都是需要账号密码的,就想连接MySql也需要密码),LDAP服务器 IP 地址 , 端口(Port) ,密码存在于 LDAP 服务器中的DN 、以及密码在该 DN 下的属性名 Attr

    String bindLdapUser;
    String bindLdapPassword;
    String baseDN;
    String ldapIp;
    String port;
    String mapAttr;
    
  2. 收集了以上信息之后开始对进行编码连接 LDAP 进行验证了。
    (1) 建立连接 ctx = new InitialDirContext(env);
    (2) 定义 SearchControls();调用 DirContext.search 方法。
    (3) 对search出来的结果NamingEnumeration进行遍历,因为search是获取当前节点下的所有支节点,不单单只是获取一个对象,如果想获取一个对象,可以调用lookup()方法。当然如果search()方法中依靠filter可以定位到一个对象节点,效果和lookup也是一样的。
    (4) 遍历过程中获取相应的Attributes。这个Attributes就是当前节点的所有属性,比如说当前节点是DN : sid=admin,ou=People,o=test ,那么获取出来的Attributes里面的属性将会是在此DN下你增加的所有属性比如(username=admin,password=123456,partment=xx,age=16…)。这里我们获取其中的密码所以是

    Attribute attr = attrs.get("password");            
    String ldapPassword = (String)attr.get();    	//获取出来的 ldapPassword 就是123456
    

    (5) 获取出来之后再对你传入的 requestPwd 和 ldap 获取出来的 ldapPassword 进行对比

    requestPwd.equals(ldapPassword);
    

    代码示例

    接下来直接贴上详细的代码,因为当时做的是认证,所以直接写成一个认证方法:

    private Map<String, Object> auth(String password, String userName) throws NamingException {
        String bindLdapUser = "root"; // 连接LDAP服务器的管理员账号密码
        String bindLdapPwd = "123456";
        String ldapBaseDN = "ou=People,o=test"; // 这个根据自己需要更改
        String attrName = "password";   // 获取DN下面的属性名
        String port = "389"; // ldap 服务器占用的端口号
        String ldapIp = "192.168.120.222"; // ldap 服务器的IP地址
    
        String url = "ldap://" + ldapIp + ":" + port + '/' + ldapBaseDN;
        System.console().printf("[auth ldap] ldap url : " + url);
    
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_CREDENTIALS, bindLdapPwd);
        env.put(Context.SECURITY_PRINCIPAL, bindLdapUser);
        env.put(Context.PROVIDER_URL, url);
        DirContext ctx = null;
    
        try {
            // Ldap link
            ctx = new InitialDirContext(env);
            System.console().printf("[auth ldap linked] InitialDirContext success");
    
            // Ldap search
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    		String filter = "sid" + userName;// 加上filter之后DN就变成sid={userName},ou=People,o=test
            NamingEnumeration<?> nameEnu = ctx.search("", filter, searchControls);
            System.console().printf("[step1 ladp linked] begin to search, filter :" + filter);
    
            if (nameEnu == null) {
                // User Not Found
                System.console().printf("[step error] DirContext.search() return null. filter : " + filter);
                return createResult(104, 2001, "User Not Found");
            } else {
                System.console().printf("[step2 ldap] Begin to print elements");
                String ldapUserPwd = null;
    
                while (nameEnu.hasMore()) {
                    System.console().printf("[step3 ldap] nameEnu element is not null");
                    Object obj = nameEnu.nextElement();
                    System.console().printf(obj.toString());
                    if (obj instanceof SearchResult) {
                        System.console().printf("[step4 ldap] obj instanceof SearchResult");
                        SearchResult result = (SearchResult) obj;
                        Attributes attrs = result.getAttributes();
                        if (attrs == null) {
                            // Password Error
                            System.console().printf("[step error] SearchResult.getAttrbutes() is null. ");
                            return createResult(102, 4003, "Password Error");
                        } else {
                            System.console().printf("[step5 ldap] begin to get attribute : " + attrName);   // attrName就是属性名
                            Attribute attr = attrs.get(attrName);
                            if (attr != null) {
                                System.console().printf("[step6 ldap] attribute is not null.");
                                ldapUserPwd = (String) attr.get();
                                System.console().printf("[step7 print ldapPwd] the ldap password is : *********");
                                System.console().printf("[step7 print ldapPwd] the request password is : *********");
                                if (password.equalsIgnoreCase(ldapUserPwd)) {
                                    // OK
                                    System.console().printf("[step8 ldap] equals password , success .");
                                    return createResult(0, 0, "OK");
                                } else {
                                    // Password Error
                                    System.console().printf("[step9 ldap] equals password , failure . password is not same .");
                                    return createResult(102, 4003, "Password Error");
                                }
                            }
                        }
                    }
                }
                System.console().printf("[step error] while end . ldapUserPwd is null , can't find password from ldap .");
                return createResult(102, 4003, "Password Error");
            }
        } catch (NamingException e) {
            e.printStackTrace();
            System.console().printf("[ldap link failed] message :" + e.getMessage());
            throw e;
        } finally {
            if (ctx != null) {
                ctx.close();
            }
        }
    }
    
    private Map<String, Object> createResult(int ret, int code, String msg) {
      Map<String, Object> result = WmsvrFactory.createParamsObj();
      result.put("ret", ret);
      result.put("code", code);
      result.put("msg", msg);
      return result;
    }
    
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Charlven

你的鼓励将是我最大的动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值