Ldap Java同步用户

Java操作LDAP有几种方法,这里主要介绍的是JNDI包,就是它:

           com.sun.jndi.ldap.LdapCtxFactory

 LDAP默认情况下使用的是BDB数据库,所以呢,操作LDAP就和操作数据库一样,要分几步走:

1、先导入所需的包,如下:

import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchResult;
import javax.naming.NamingException;
import com.sun.NET.ssl.internal.ssl.Debug;

2、连接LDAP,得到连接对象,如下:

DirContext ctx = null;                //这个就是LDAP的连接对象

Hashtable env = new Hashtable();                   //定义一个哈希表来存连接信息

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");   //记录工JNDI工厂
env.put(Context.PROVIDER_URL, ldap://192.168.0.1:389);        //LDAP的地址,要根据LDAP服务器IP进行修改,389是LDAP的默认端口
env.put(Context.SECURITY_AUTHENTICATION, "simple");    //这个是默认授权类型,一般不用改
env.put(Context.SECURITY_PRINCIPAL, "");               //LDAP的账户名,一般是这样的格式:(cn=admin,)dc=cs,dc=hunan,dc=com ,根据LDAP的配置情况来
env.put(Context.SECURITY_CREDENTIALS, "123456");       //对应上面账户的密码
          
try
{
      ctx = new InitialDirContext(env);         //初始化LDAP连接,连接成功后就可以用ctx来操作LDAP了
}
catch(NamingException e)
{
     e.printStackTrace();
}

这里建议大家把以上的代码写成一个函数,返回DirContext 这个对象,方便使用。

3、连接LDAP后同步某一个组下面的用户,如下:

Map<String,String> map = new HashMap<String, String>();

try {
                    if(ctx != null){
                        NamingEnumeration<NameClassPair> list = ctx.list("ou=Group,dc=cs,dc=hunan,dc=com“);
                        while(list.hasMore()){
                            NameClassPair ncp = list.next();
                            String cn = ncp.getName();
                            if(StringUtil.isNotBlank(cn) && cn.indexOf("=") != -1){
                                int index = cn.indexOf("=");
                                cn = cn.substring(index + 1,cn.length());
                                map.put(cn, ncp.getNameInNamespace());
                            }
                        }
                    }
                } catch (NamingException e) {
                    e.printStackTrace();
                    return;
                }
                
                try {
                    if(ctx != null)
                        ctx.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }

//遍历用户

Iterator<Entry<String,String>> it = map.entrySet().iterator();

while(it.hasNext()){

         Entry<String,String> entry = it.next();

         System.out.println(entry.getKey())

         System.out.println(entry.getValue())

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的JNDI(Java Naming and Directory Interface)API来连接LDAP服务器,并执行添加用户的操作。以下是一个简单的Java代码示例: ```java import javax.naming.*; import javax.naming.directory.*; public class LDAPAddUserExample { public static void main(String[] args) { // LDAP服务器信息 String ldapURL = "ldap://localhost:389"; String ldapUser = "cn=admin,dc=mydomain,dc=com"; String ldapPassword = "adminpassword"; // 用户信息 String username = "newuser"; String userPassword = "password"; String userFullName = "New User"; String userDescription = "A new user account"; try { // 创建LDAP连接 Hashtable<String, Object> env = new Hashtable<String, Object>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, ldapUser); env.put(Context.SECURITY_CREDENTIALS, ldapPassword); DirContext ctx = new InitialDirContext(env); // 创建用户对象 Attributes attrs = new BasicAttributes(); attrs.put("objectClass", "top"); attrs.put("objectClass", "person"); attrs.put("objectClass", "organizationalPerson"); attrs.put("objectClass", "inetOrgPerson"); attrs.put("uid", username); attrs.put("cn", userFullName); attrs.put("sn", username); attrs.put("userPassword", userPassword); attrs.put("description", userDescription); // 添加用户 ctx.createSubcontext("uid=" + username + ",ou=users,dc=mydomain,dc=com", attrs); // 关闭LDAP连接 ctx.close(); System.out.println("User added successfully!"); } catch (NamingException e) { e.printStackTrace(); } } } ``` 需要替换代码中的LDAP服务器信息和用户信息为实际值,并确保LDAP服务器已启动并可用。此代码将在LDAP服务器的“ou=users,dc=mydomain,dc=com”组织单元中创建一个名为“newuser”的用户

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值