1.准备过程
1.1 在windows server 2008 R2操作系统下搭建AD域,不懂得看链接地址:http://99532720.blog.51cto.com/2194488/696611
1.2. 可以使用LDAP客户端(Apache Directory Studio) 直接连接AD域,下载地址 http://directory.apache.org/studio/
1.3. java语句要用到的jar包,下载地址:https://sourceforge.net/projects/ldap-sdk/
2.java操作过程(我这里只做同步查询,负责查询ldap数据,然后同步到我的数据库)
2.1 定义参数
- // 当前配置信息
- private static String ldapHost = "172.16.160.196";
- private static int ldapPort = 389; //默认端口
- private static String ldapBindDN = "cn=zhangsan,rou=销售部一,ou=销售部,ou=哇哈哈公司,dc=tian,dc=com";
- private static String ldapPassword = "******"; //张三的密码
- private static LDAPConnection connection = null;
dc就是你搭建AD域的时候创建的域名,就是如图
ldapBindDN的值:cn=zhangsan,rou=销售部一,ou=销售部,ou=哇哈哈公司,dc=tian,dc=com
这里的zhangsan是名字为张三的账号
2.1 创建Ldap连接
/** 连接LDAP */
public static void openConnection() {
if (connection == null) {
try {
connection = new LDAPConnection(ldapHost, ldapPort, ldapBindDN, ldapPassword);
System.out.println("连接成功!");
} catch (Exception e) {
System.out.println("连接LDAP出现错误:
" + e.getMessage());
}
}
}
2.2 先查询出来ldap数据
- /** 查询 */
- public static 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);
- System.out.println(">>>共查询到" + searchResult.getSearchEntries().size() + "条记录");
- int index = 1;
- for (SearchResultEntry entry : searchResult.getSearchEntries()) {
- System.out.println((index++) + " " + entry.getDN());
- }
- } catch (Exception e) {
- System.out.println("查询错误,错误信息如下: " + e.getMessage());
- }
- }
2.2 测试
- public static void main(String[] args) {
- String searchBase="ou=销售部,ou=哇哈哈公司,dc=tian,dc=com";
- String filter = "objectClass=organizationalUnit"; //这是查询所有部门的
- //objectClass=person这是查询所有用户的,objectClass=group这是查询部门下所有的讨论组的,objectClass=*这是查询所有
- queryLdap(searchBase, filter);
- }
2.3 贴一点同步用户用到的地方
保存用户的类根据具体情况自己设置属性
/** 先从ldap里查询 然后同步用户到数据库用的*/
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);
System.out.println(">>>共查询到" + searchResult.getSearchEntries().size() + "条记录");
int index = 1;
Connection con=null;
UserDate user=null;
con=dbUtil.getCon();
for (SearchResultEntry entry : searchResult.getSearchEntries()) {
System.out.println((index++) + " " + entry.getDN());
System.out.println(entry.toString());
//遍历entry,获取有效数据
Collection<Attribute> ca=entry.getAttributes();
Iterator<Attribute> itr=ca.iterator();
user=new UserDate();
String uuid = UUID.randomUUID().toString();
user.setId(uuid);
user.setAccountType(0);
Date date=new Date();
user.setCreateTime(date);
while(itr.hasNext()){
Attribute ab=itr.next();
String name=ab.getName();
/*String value=ab.getValue();
System.out.println("=="+value);
System.out.println("--"+name);*/
if(name.equals("sAMAccountName")){ //用户登录名
user.setAccount(ab.getValue());
}
if(name.equals("mail")){ //电子邮件
user.setEmail(ab.getValue());
}
if(name.equals("mobile")){ //手机号
user.setMobile(ab.getValue());
}
if(name.equals("displayName")){ //姓名
user.setName(ab.getValue());
}
if(name.equals("postalCode")){
user.setZipCode(ab.getValue());
}
if(name.equals("l")){//市
user.setLocationAddress(ab.getValue());
}
if(name.equals("st")){//省
String address1=user.getLocationAddress();
user.setLocationAddress(ab.getValue()+address1);
}
if(name.equals("co")){//国家
String address2=user.getLocationAddress();
user.setLocationAddress(ab.getValue()+address2);
}
if(name.equals("streetAddress")){//街道
String address2=user.getLocationAddress();
user.setLocationAddress(address2+ab.getValue());
}
}
System.out.println("用户信息:"+user);
int result=userDao.saveUser(con, user);
if(result>0){
System.out.println("保存用户成功!");
}else{
System.out.println("保存用户失败!");
}
System.out.println("*******************************");
}
} catch (Exception e) {
System.out.println("查询错误,错误信息如下:
" + e.getMessage());
}
}