JAVA访问LDAP(openldap)的示例
来源:互联网 发布:mac能玩iphone手游 编辑:程序博客网 时间:2024/06/05 07:16
1,安装openldap,下载openldap: http://download.csdn.net/detail/kkdelta/8090881
修改c:\openldap\slapd.conf
本示例中的suffix 和rootdn修改为如下:
database bdb
#suffix "dc=maxcrc,dc=com"
#rootdn "cn=Manager,dc=maxcrc,dc=com"
suffix "o=tt,c=cn"
rootdn "cn=Manager,o=tt,c=cn"
2,启动openldap,CMD 进入到 c:\openldap 下,运行命令 slapd -d 1 -f .\slapd.conf
3,用LdapBrowser测试连接,下载地址:http://download.csdn.net/detail/kkdelta/8090907
详细步骤参照:
http://topinking.iteye.com/blog/223334
http://hi.baidu.com/chiefleo/item/90355698eb162c4ff0421551
常见错误
http://blog.csdn.net/techchan/article/details/5440775
JAVA代码:
修改c:\openldap\slapd.conf
本示例中的suffix 和rootdn修改为如下:
database bdb
#suffix "dc=maxcrc,dc=com"
#rootdn "cn=Manager,dc=maxcrc,dc=com"
suffix "o=tt,c=cn"
rootdn "cn=Manager,o=tt,c=cn"
2,启动openldap,CMD 进入到 c:\openldap 下,运行命令 slapd -d 1 -f .\slapd.conf
3,用LdapBrowser测试连接,下载地址:http://download.csdn.net/detail/kkdelta/8090907
详细步骤参照:
http://topinking.iteye.com/blog/223334
http://hi.baidu.com/chiefleo/item/90355698eb162c4ff0421551
常见错误
http://blog.csdn.net/techchan/article/details/5440775
JAVA代码:
package com.test.ldap;import java.util.Hashtable;import javax.naming.AuthenticationException;import javax.naming.Context;import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.BasicAttribute;import javax.naming.directory.BasicAttributes;import javax.naming.directory.SearchControls;import javax.naming.directory.SearchResult;import javax.naming.ldap.Control;import javax.naming.ldap.InitialLdapContext;import javax.naming.ldap.LdapContext; public class LDAPAuthentication { private final String URL = "ldap://127.0.0.1:389/"; private final String BASEDN = "o=tt,c=cn"; // 根据自己情况进行修改 private final String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; private LdapContext ctx = null; private final Control[] connCtls = null; private void LDAP_connect() { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY); env.put(Context.PROVIDER_URL, URL + BASEDN); env.put(Context.SECURITY_AUTHENTICATION, "simple"); String root = "cn=manager,o=tt,c=cn"; //根据自己情况修改 env.put(Context.SECURITY_PRINCIPAL, root); // 管理员 env.put(Context.SECURITY_CREDENTIALS, "secret"); // 管理员密码 try { ctx = new InitialLdapContext(env, connCtls); System.out.println( "连接成功" ); } catch (javax.naming.AuthenticationException e) { System.out.println("连接失败:"); e.printStackTrace(); } catch (Exception e) { System.out.println("连接出错:"); e.printStackTrace(); } } private void closeContext(){ if (ctx != null) { try { ctx.close(); } catch (NamingException e) { e.printStackTrace(); }} } private String getUserDN(String uid) { String userDN = ""; LDAP_connect(); try { SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<SearchResult> en = ctx.search("", "uid=" + uid, constraints); if (en == null || !en.hasMoreElements()) { System.out.println("未找到该用户"); } // maybe more than one element while (en != null && en.hasMoreElements()) { Object obj = en.nextElement(); if (obj instanceof SearchResult) { SearchResult si = (SearchResult) obj; userDN += si.getName(); userDN += "," + BASEDN; } else { System.out.println(obj); } } } catch (Exception e) { System.out.println("查找用户时产生异常。"); e.printStackTrace(); } return userDN; } public boolean authenricate(String UID, String password) { boolean valide = false; String userDN = getUserDN(UID); try { ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN); ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password); ctx.reconnect(connCtls); System.out.println(userDN + " 验证通过"); valide = true; } catch (AuthenticationException e) { System.out.println(userDN + " 验证失败"); System.out.println(e.toString()); valide = false; } catch (NamingException e) { System.out.println(userDN + " 验证失败"); valide = false; } closeContext(); return valide; } private boolean addUser(String usr, String pwd) { try { LDAP_connect(); BasicAttributes attrsbu = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute("objectclass"); objclassSet.add("inetOrgPerson"); attrsbu.put(objclassSet); attrsbu.put("sn", usr); attrsbu.put("cn", usr); attrsbu.put("uid", usr); attrsbu.put("userPassword", pwd); ctx.createSubcontext("uid=yorker", attrsbu); return true; } catch (NamingException ex) { ex.printStackTrace(); } closeContext(); return false; } public static void main(String[] args) { LDAPAuthentication ldap = new LDAPAuthentication(); //ldap.LDAP_connect(); if(ldap.authenricate("yorker", "secret") == true){ System.out.println( "该用户认证成功" ); } //ldap.addUser("yorker","secret"); }}
0 0
- JAVA访问LDAP(openldap)的示例
- Java访问LDAP服务器认证代码示例
- ldap - 设置一个基本的OpenLDAP Server
- ldap - 设置一个基本的OpenLDAP Server
- JAVA操作Ldap示例
- Java JNDI对 LDAP数据库的访问
- JAVA访问ldap
- Java访问LDAP
- LDAP ACL(OpenLDAP)
- ldap(二)-openldap
- openDS & LDAP & openLDAP
- PHP连接LDAP(OpenLDAP和Wins Active Directory)的用法
- GitLab集成LDAP登录并解决OpenLDAP的memberOf问题
- alfresco ldap配置openldap server 和 window的AD域
- JNDI访问LDAP服务简单应用示例
- java ldap 访问 Active Directory
- Linux下openldap的配置的访问
- Linux下openldap的配置的访问
- 第26章 Linux核心编译与管理
- androidのActionbar
- 三角形面积
- Android软键盘状态的切换及其强制隐藏
- Learning jQuery one
- JAVA访问LDAP(openldap)的示例
- 查看安装的软件
- Understanding RPO and RTO
- 【JavaScritp】cookies详解
- 使用openssl库实现RSA、AES数据加密
- apache-tomcat-7.0.33 在win7 上配置使用
- 事件、属性及数据类型
- Android开发面试题汇总
- Thinkphp常用系统配置大全