ubuntu在安装LDAP服务器,在windows客户端下查看,并使用java操作LDAP
来源:互联网 发布:淘宝的特点和定位 编辑:程序博客网 时间:2024/05/18 15:24
1.在ubuntu上安装LDAP服务器
sudo apt-get install slapd ldap-utils
重新配置:
sudo dpkg-reconfigure slapd
Omit OpenLDAP server configuration? No
DNS domain name?
这将会创建基本的目录树结构
理论上是可以随便填的,如果你有域名可以用域名
这里使用 chenjr.cc
Organization name? 也是随便填,这里使用 zju
Administrator password?
Database backend to use? HDB
Remove the database when slapd is purged? No
Move old database? Yes
Allow LDAPv2 protocol? No
2.windows客户端连接服务器
下载LDAP admin软件,打开之后配置连接信息即可
玩玩的就变成这样的:
3.编写代码
打开eclipse:
1、Java客户端API(UnboundID LDAP SDK for Java)
下载地址 http://sourceforge.net/projects/ldap-sdk/files/
2、LDAP客户端(Apache Directory Studio)
下载地址 http://directory.apache.org/studio/
配置一下连接如下:
编写代码开始:
目录结构:
代码如下:
package cn.creditease.ldapproject.util;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import com.unboundid.ldap.sdk.Attribute;import com.unboundid.ldap.sdk.LDAPConnection;import com.unboundid.ldap.sdk.Modification;import com.unboundid.ldap.sdk.ModificationType;import com.unboundid.ldap.sdk.SearchRequest;import com.unboundid.ldap.sdk.SearchResult;import com.unboundid.ldap.sdk.SearchResultEntry;import com.unboundid.ldap.sdk.SearchScope;import com.unboundid.ldap.sdk.controls.SubentriesRequestControl;public class LDAPOperator {// 当前配置信息private static String ldapHost = "192.168.59.128";private static int ldapPort = 389;private static String ldapBindDN = "cn=admin,dc=chenjr,dc=cc";;private static String ldapPassword = "admin";private static LDAPConnection connection = null;/** 连接LDAP */public static void openConnection() {if (connection == null) {try {connection = new LDAPConnection(ldapHost, ldapPort, ldapBindDN,ldapPassword);} catch (Exception e) {System.out.println("连接LDAP出现错误:\n" + e.getMessage());}}}/** 创建DC */public static void createDC(String baseDN, String dc) {String entryDN = "dc=" + dc + "," + baseDN;try {// 连接LDAPopenConnection();SearchResultEntry entry = connection.getEntry(entryDN);if (entry == null) {// 不存在则创建ArrayList<Attribute> attributes = new ArrayList<Attribute>();attributes.add(new Attribute("objectClass", "top","organization", "dcObject"));attributes.add(new Attribute("dc", dc));attributes.add(new Attribute("o", dc));//System.out.println("Reach There");connection.add(entryDN, attributes);System.out.println("创建DC" + entryDN + "成功!");} else {System.out.println("DC " + entryDN + "已存在!");}} catch (Exception e) {System.out.println("创建DC出现错误:\n" + e.getMessage());}}/** 创建组织 */public static void createO(String baseDN, String o) {String entryDN = "o=" + o + "," + baseDN;try {// 连接LDAPopenConnection();SearchResultEntry entry = connection.getEntry(entryDN);if (entry == null) {// 不存在则创建ArrayList<Attribute> attributes = new ArrayList<Attribute>();attributes.add(new Attribute("objectClass", "top","organization"));attributes.add(new Attribute("o", o));connection.add(entryDN, attributes);System.out.println("创建组织" + entryDN + "成功!");} else {System.out.println("组织" + entryDN + "已存在!");}} catch (Exception e) {System.out.println("创建组织出现错误:\n" + e.getMessage());}}/** 创建组织单元 */public static void createOU(String baseDN, String ou) {String entryDN = "ou=" + ou + "," + baseDN;try {// 连接LDAPopenConnection();SearchResultEntry entry = connection.getEntry(entryDN);if (entry == null) {// 不存在则创建ArrayList<Attribute> attributes = new ArrayList<Attribute>();attributes.add(new Attribute("objectClass", "top","organizationalUnit"));attributes.add(new Attribute("ou", ou));connection.add(entryDN, attributes);System.out.println("创建组织单元" + entryDN + "成功!");} else {System.out.println("组织单元" + entryDN + "已存在!");}} catch (Exception e) {System.out.println("创建组织单元出现错误:\n" + e.getMessage());}}/** 创建用户 */public static void createEntry(String baseDN, String uid) {String entryDN = "uid=" + uid + "," + baseDN;try {// 连接LDAPopenConnection();SearchResultEntry entry = connection.getEntry(entryDN);if (entry == null) {// 不存在则创建ArrayList<Attribute> attributes = new ArrayList<Attribute>();attributes.add(new Attribute("objectClass", "top", "account"));attributes.add(new Attribute("uid", uid));connection.add(entryDN, attributes);System.out.println("创建用户" + entryDN + "成功!");} else {System.out.println("用户" + entryDN + "已存在!");}} catch (Exception e) {System.out.println("创建用户出现错误:\n" + e.getMessage());}}/** 修改用户信息 */public static void modifyEntry(String requestDN, Map<String,String> data) {try {// 连接LDAPopenConnection();SearchResultEntry entry = connection.getEntry(requestDN);if (entry == null) {System.out.println(requestDN + " user:" + requestDN + " 不存在");return;}// 修改信息ArrayList<Modification> md = new ArrayList<Modification>();for(String key : data.keySet()) {md.add(new Modification(ModificationType.REPLACE, key, data.get(key)));}connection.modify(requestDN, md);System.out.println("修改用户信息成!");} catch (Exception e) {System.out.println("修改用户信息出现错误:\n" + e.getMessage());}}/** 删除用户信息 */public static void deleteEntry(String requestDN) {try {// 连接LDAPopenConnection();SearchResultEntry entry = connection.getEntry(requestDN);if (entry == null) {System.out.println(requestDN + " user:" + requestDN + "不存在");return;}// 删除connection.delete(requestDN);System.out.println("删除用户信息成!");} catch (Exception e) {System.out.println("删除用户信息出现错误:\n" + e.getMessage());}}/** 查询 */public static void queryLdap(String searchDN, String filter) {try {// 连接LDAPopenConnection();// 查询企业所有用户SearchRequest searchRequest = new SearchRequest(searchDN,SearchScope.SUB, "(" + filter + ")");searchRequest.addControl(new SubentriesRequestControl());SearchResult searchResult = connection.search(searchRequest);System.out.println(">>>共查询到"+ searchResult.getSearchEntries().size() + "条记录");int index = 1;for (SearchResultEntry entry : searchResult.getSearchEntries()) {System.out.println((index++) + "\t" + entry.getDN());}} catch (Exception e) {System.out.println("查询错误,错误信息如下:\n" + e.getMessage());}}public static void main(String[] args) {String root = "cc";String secondRoot = "chenjr";String dc = "truesens";String o = "kedacom";String ou = "people";String uid = "admin";String filter = "objectClass=account";String rootPath = "dc=" + secondRoot + ",dc=" + root;createDC(rootPath,dc);createO("dc=" + dc + "," + rootPath, o);createOU("o=" + o + ",dc=" + dc + "," + rootPath, ou);createEntry("ou=" + ou + ",o=" + o + ",dc=" + dc + "," + rootPath, uid);queryLdap("ou=" + ou + ",o=" + o + ",dc=" + dc + "," + rootPath, filter);HashMap<String, String> data = new HashMap<String, String>(0);data.put("userid", uid);modifyEntry("uid=" + uid + ",ou=" + ou + ",o=" + o + ",dc=" + dc+ "," + rootPath, data);deleteEntry("uid=" + uid + ",ou=" + ou + ",o=" + o + ",dc=" + dc+ "," + rootPath);queryLdap("ou=" + ou + ",o=" + o + ",dc=" + dc + "," + rootPath, filter);}}运行结果:
参考网站:
http://cgs1999.iteye.com/blog/1574635
http://segmentfault.com/a/1190000002890435
0 0
- ubuntu在安装LDAP服务器,在windows客户端下查看,并使用java操作LDAP
- ubuntu 10.04下安装LDAP服务器
- LDAP在windows下配置及安装&入门教程
- 在WINDOWS中使用LDAP开发
- WINDOWS下搭建LDAP服务器
- WINDOWS下搭建LDAP服务器
- Windows下LDAP服务器配置
- Windows下LDAP服务器配置
- Windows下LDAP服务器配置
- Windows 下搭建LDAP服务器
- WINDOWS下搭建LDAP服务器
- Windows下LDAP服务器配置
- 在 Java CAPS 中使用 LDAP
- 使用 Python-LDAP 操作 LDAP
- ldap在项目中的使用
- 使用Java操作LDAP案例
- 原创 WINDOWS下搭建LDAP服务器收藏
- 如何使用Java操作LDAP之LDAP连接(一)
- Objective-C语言_不可变字典
- Mac—删除默认英文输入法
- oracle简单触发器
- ios、java之间的RSA加解密
- jvm学习笔记2:jvm垃圾收集器与垃圾收集算法
- ubuntu在安装LDAP服务器,在windows客户端下查看,并使用java操作LDAP
- android开发之colores.xml----常用颜色
- http以post方式上传一个文件,构造其请求头和消息报文 application/x-www-form-urlencoded multipart/form-data
- ios常用设计模式详解
- 互联网+,加什么?
- LeetCode Linked List Cycle II
- SVN commit:remains in tree-conflict解决方案
- 解决Xcode7.1上传成功但developer账户一直没有显示的问题
- Softmax回归