package com.wisdombud.unicom.monitor.ldap;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.unboundid.ldap.sdk.Attribute;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
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;
import com.wisdombud.unicom.monitor.listener.MessageAnalyze; public class LdapOper {
private static final Logger LOGGER = LoggerFactory
.getLogger(MessageAnalyze.class);
private LDAPConnection connection = null;
private String bindDN = "cn=root,o=ibm,c=cn"; private int port = 389;
private String password = "db2admin";
private String o = "ibm";
private String ou = "users";
private String ouEntry = "o=ibm,c=cn";
private String oEntry = "o=ibm,c=cn";
private String dcEntry = "o=ibm,c=cn";
private String groupEntry = "cn=permitted,o=ibm,c=cn";
private String LDAP_HOST = "127.0.0.1";
static {
//GlobalValues.LDAP_HOST = "127.0.0.1";
// MonitorConfigBean config = CollectDaoFactory.getInstance()
// .getCollectDao().findConfig();
// if (config != null) {
// GlobalValues.LDAP_HOST = config.getLdapIp();
// } else {
//
// GlobalValues.LDAP_HOST = "127.0.0.1";
// }
} public void RunTest() { // LOGGER.info(this.ldapConfig.getLdapHost());
this.openConnection();
} public void openConnection() {
if (connection == null) {
try {
connection = new LDAPConnection(LDAP_HOST, port,
bindDN, password);
LOGGER.info("connect success");
} catch (Exception e) {
LOGGER.info("连接LDAP出现错误:\n" + e.getMessage());
}
}
} private void createO() {
String entryDN = this.oEntry;
try {
openConnection(); 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", this.o));
attributes.add(new Attribute("o", this.o));
connection.add(entryDN, attributes);
LOGGER.info("创建o" + entryDN + "成功!");
} else {
LOGGER.info("o " + entryDN + "已存在!");
}
} catch (Exception e) {
LOGGER.info("创建DC出现错误:\n" + e.getMessage());
}
} private void createDC(String dc) {
String entryDN = this.dcEntry;
try {
// 连接LDAP
openConnection(); 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));
connection.add(entryDN, attributes);
LOGGER.info("创建DC" + entryDN + "成功!");
} else {
LOGGER.info("DC " + entryDN + "已存在!");
}
} catch (Exception e) {
LOGGER.info("创建DC出现错误:\n" + e.getMessage());
}
} private void createOU() {
String entryDN = this.ouEntry;
try {
// 连接LDAP
openConnection(); 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", this.ou));
connection.add(entryDN, attributes);
LOGGER.info("创建组织单元" + entryDN + "成功!");
} else {
LOGGER.info("组织单元" + entryDN + "已存在!");
}
} catch (Exception e) {
LOGGER.info("创建组织单元出现错误:\n" + e.getMessage());
}
} private void DeleteGroupMember(String userEntry) { try {
SearchResultEntry entry = connection.getEntry(groupEntry);
if (entry != null) {
ArrayList<Modification> md = new ArrayList<Modification>();
md.add(new Modification(ModificationType.DELETE, "member",
userEntry));
connection.modify(groupEntry, md);
LOGGER.info("删除member成功:" + userEntry);
}
} catch (LDAPException e) {
e.printStackTrace();
}
} private void AddGroupMember(String userEntry) { try {
SearchResultEntry entry = connection.getEntry(groupEntry);
if (entry != null) {
ArrayList<Modification> md = new ArrayList<Modification>();
md.add(new Modification(ModificationType.ADD, "member",
userEntry));
connection.modify(groupEntry, md);
LOGGER.info("添加member成功:" + userEntry);
}
} catch (LDAPException e) {
e.printStackTrace();
} } public void createUserEntry(String user, String passwd, String ip) {
String entryDN = "uid=" + user + "," + this.ouEntry;
try {
// 连接LDAP
openConnection(); SearchResultEntry entry = connection.getEntry(entryDN);
if (entry == null) {
// 不存在则创建
ArrayList<Attribute> attributes = new ArrayList<Attribute>(); attributes.add(new Attribute("uid", user));
attributes.add(new Attribute("objectClass", "top",
"organizationalPerson", "inetOrgPerson", "person")); attributes.add(new Attribute("userPassword", passwd));
attributes.add(new Attribute("street", passwd));
attributes.add(new Attribute("sn", user));
attributes.add(new Attribute("cn", user)); connection.add(entryDN, attributes);
LOGGER.info("创建用户" + entryDN + "成功!");
this.AddGroupMember(entryDN);
} else {
LOGGER.info("用户" + entryDN + "已存在!");
}
} catch (Exception e) {
LOGGER.info("创建用户出现错误:\n" + e.getMessage());
}
} public void deleteUserEntry(String user) {
String requestDN = "uid=" + user + "," + this.ouEntry;
try {
// 连接LDAP
openConnection(); SearchResultEntry entry = connection.getEntry(requestDN);
if (entry == null) {
LOGGER.info(requestDN + " user:" + requestDN + "不存在");
return;
}
// 删除
connection.delete(requestDN);
LOGGER.info("删除用户信息成功!");
this.DeleteGroupMember(requestDN); } catch (Exception e) {
LOGGER.info("删除用户信息出现错误:\n" + e.getMessage());
}
} public void queryLdap(String searchDN, String filter) {
try {
// 连接LDAP
openConnection(); // 查询企业所有用户
SearchRequest searchRequest = new SearchRequest(searchDN,
SearchScope.SUB, "(" + filter + ")");
searchRequest.addControl(new SubentriesRequestControl());
SearchResult searchResult = connection.search(searchRequest);
LOGGER.info(">>>共查询到" + searchResult.getSearchEntries().size()
+ "条记录");
int index = 1;
for (SearchResultEntry entry : searchResult.getSearchEntries()) {
LOGGER.info((index++) + "\t" + entry.getDN());
}
} catch (Exception e) {
LOGGER.info("查询错误,错误信息如下:\n" + e.getMessage());
}
} public static void main(String[] args) {
LdapOper loper = new LdapOper();
System.out.println("start to create ldap user");
// loper.createO();
// loper.createOU();
/*
* IFM_XQJZ IFM_JZBYXY IFM_JZBYMC IFM_JZBYCZC
*
* ifm@1234
*/
String password = "ifm@1234";
loper.createUserEntry("IFM_XQJZ", password, "1.1.1.1");
loper.createUserEntry("IFM_JZBYXY", password, "1.1.1.1");
loper.createUserEntry("IFM_JZBYMC", password, "1.1.1.1");
loper.createUserEntry("IFM_JZBYCZC", password, "1.1.1.1");
loper.createUserEntry("INMS_QCHMD", "inms@123", "1.1.1.1");
// INMS_QCHMD这个也没有,密码是inms@123 }
}

#JAVA操作LDAP的更多相关文章

  1. 配置OpenLDAP,Java操作LDAP,DBC-LDAP进访问

    LDAP快速入门 1. LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的 ...

  2. JAVA操作LDAP总结

    一.LDAP概念 LDAP的全称为Lightweight Directory Access Protocol(轻量级目录访问协议), 基于X.500标准, 支持 TCP/IP. LDAP目录为数据库, ...

  3. JAVA操作LDAP的详解(JLDAP)

    最近两周由于要学习测试LDAP,所以对于用脚本操作LDAP很感兴趣,所以就做了一些脚本,都是比较简单的脚本吧. 废话不多说了哈.直接上教程 首先声明:我使用的是JLDAP操作LDAP,所以需要从官网下 ...

  4. JAVA使用Ldap操作AD域

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

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

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

  6. springLdap 操作ldap示例(增删改查)

    转自:http://blog.csdn.net/sundenskyqq/article/details/9002440 这部分的示例网上的确有很多,但是个人在查找的过程中还是感到不够满意,所以就自己总 ...

  7. Java操作Sqlite数据库-jdbc连接

    Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...

  8. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  9. JAVA 通过LDAP获取AD域用户及组织信息

    因为工作需求近期做过一个从客户AD域获取数据实现单点登录的功能,在此整理分享. 前提:用户可能有很多系统的情况下,为了方便账号的统一管理使用AD域验证登录,所以不需要我们的系统登录,就需要获取用户的A ...

随机推荐

  1. 面试题_89_to_92_单元测试 JUnit 面试题

    89)如何测试静态方法?(答案)可以使用 PowerMock 库来测试静态方法. 90)怎么利用 JUnit 来测试一个方法的异常?(答案) 91)你使用过哪个单元测试库来测试你的 Java 程序?( ...

  2. hdu 4973 A simple simulation problem. (线段树)

    题目链接 题意: 给定n长的序列 m个操作 序列默认为 1, 2, 3···n 操作1:D [l,r] 把[l,r]区间增长 :( 1,2,3,4 进行 D [1,3]变成 1,1,2,2,3,3,4 ...

  3. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...

  4. 【Java集合框架】规则集--Set

    集合: Java主要支持三种: 1.规则集(Set) 用于存储一组不重复的元素 2.线性表(List) 用于存储一个由元素构成的有序集合 3.队列(Queue) 同与数据结构中的队列,存储用先进先出的 ...

  5. 图片鼠标滑过图片半透明(jquery特效)

    在做瑞祥之旅的过程,有一个部分是材料体系,材料体系下面.预览效果

  6. Windows环境自动获取AWR报告

    1.双击awr.cmd,通过cmd窗口运行awr.sql cmd.exe /c sqlplus lcam_1230/zcpzg1z_1230@54_orcl @awr.sql awr.cmd 2.aw ...

  7. HDU 5742 It's All In The Mind

    It's All In The Mind Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. iOS 7 自定义Back按钮 与 Pop interactive gesture 问题

    1.自定义Back按钮 iOS中很多时候我们都会自定义返回按钮,也是一件easy的事,类似如下: // 返回按钮 1 - (void)showNavBackButton { UIButton *bac ...

  9. leveldb源码笔记

    关于KV数据库leveldb的介绍,网上已经太多了,这里只是自己再学习源码过程中,整理的笔记,磁盘存储和内存存储的结构用了伪代码表示出来了,首先是内存中存储结构,然后是log文件存储结构和磁盘数据ss ...

  10. SPOJ DISUBSTR Distinct Substrings 后缀数组

    题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...