Java验证Ldap账号和读取Ldap中所有用户

一、验证Ldap用户

public class LDAPAuthentication{    
    private final String URL = "ldap://192.168.1.205:389/";
    private final String BASEDN = "cn=demo1,dc=sys,dc=com";  // 根据自己情况进行修改
    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=demo1,dc=sys,dc=com";  // 根,根据自己情况修改
        env.put(Context.SECURITY_PRINCIPAL, root);   // 管理员
        env.put(Context.SECURITY_CREDENTIALS, "123456");  // 管理员密码
         
        try {
            ctx = new InitialLdapContext(env, connCtls);
            System.out.println( "认证成功" ); 
            System.out.println(ctx);
             
        } catch (javax.naming.AuthenticationException e) {
            System.out.println("认证失败:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("认证出错:");
            e.printStackTrace();
        }
         
        if (ctx != null) {
            try {
                ctx.close();
            }
            catch (NamingException e) {
                e.printStackTrace();
            }
 
        }
    }
}

二、读取Ldap用户信息

public class demo {

	public static void main(String[] args) {
		readLdap();
	}
	
	public static void readLdap(){

		Hashtable<String, String> env = new Hashtable<String, String>();
		DirContext ctx = null;
		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://" + "192.168.1.205" + ":" + "389");
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, "CN=" + "admin" + "," + "dc=sys,dc=com");
		env.put(Context.SECURITY_CREDENTIALS, "123456");
		try {
			ctx = new InitialDirContext(env);
		} catch (NamingException e) {
			e.printStackTrace();
			return;
		}
		
		Map<String,String> map = new HashMap<String, String>();
		try {
			if(ctx != null){
				NamingEnumeration<NameClassPair> list = ctx.list("dc=sys,dc=com");
				while(list.hasMore()){
					NameClassPair ncp = list.next();
					String cn = ncp.getName();
					if(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("Key:"+entry.getKey());
			System.out.println("Value:"+entry.getValue());
		}
	}
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值