通过java连接openLDAP的方法

近期在做关于openLDAP方面的开发,写了一下测试代码,连接openLDAP和添加帐户,如下

package com.test;

import java.util.Hashtable;  

import javax.naming.AuthenticationException;  
import javax.naming.Context;  
import javax.naming.NamingEnumeration;  
import javax.naming.NamingException;  
import javax.naming.directory.BasicAttribute;  
import javax.naming.directory.BasicAttributes;  
import javax.naming.directory.SearchControls;  
import javax.naming.directory.SearchResult;  
import javax.naming.ldap.Control;  
import javax.naming.ldap.InitialLdapContext;  
import javax.naming.ldap.LdapContext;  
    
public class LDAPAuthentication {  
    private final String URL = "ldap://10.10.1.22:389/";  
    private final String BASEDN = "dc=test,dc=local";  // 根据自己情况进行修改  
    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=root,dc=test,dc=local";  //根据自己情况修改  
        env.put(Context.SECURITY_PRINCIPAL, root);   // 管理员  
        env.put(Context.SECURITY_CREDENTIALS, "123456");  // 管理员密码  
           
        try {  
            ctx = new InitialLdapContext(env, connCtls);  
            System.out.println( "连接成功" );   
               
        } catch (javax.naming.AuthenticationException e) {  
            System.out.println("连接失败:");  
            e.printStackTrace();  
        } catch (Exception e) {  
            System.out.println("连接出错:");  
            e.printStackTrace();  
        }  
           
    }  
  private void closeContext(){  
    if (ctx != null) {  
    try {  
        ctx.close();  
    }  
    catch (NamingException e) {  
        e.printStackTrace();  
    }  
  
}  
  }  
    private String getUserDN(String uid) {  
        String userDN = "";  
        LDAP_connect();  
        try {  
            SearchControls constraints = new SearchControls();  
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);  
              
            NamingEnumeration<SearchResult> en = ctx.search("", "uid=" + uid, constraints);  
              
            if (en == null || !en.hasMoreElements()) {  
                System.out.println("未找到该用户");  
            }  
            // maybe more than one element  
            while (en != null && en.hasMoreElements()) {  
                Object obj = en.nextElement();  
                if (obj instanceof SearchResult) {  
                    SearchResult si = (SearchResult) obj;  
                    userDN += si.getName();  
                    userDN += "," + BASEDN;  
                } else {  
                    System.out.println(obj);  
                }  
            }  
        } catch (Exception e) {  
            System.out.println("查找用户时产生异常。");  
            e.printStackTrace();  
        }  
    
        return userDN;  
    }  
    
    public boolean authenricate(String UID, String password) {  
        boolean valide = false;  
        String userDN = getUserDN(UID);  
    
        try {  
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);  
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);  
            ctx.reconnect(connCtls);  
            System.out.println(userDN + " 验证通过");  
            valide = true;  
        } catch (AuthenticationException e) {  
            System.out.println(userDN + " 验证失败");  
            System.out.println(e.toString());  
            valide = false;  
        } catch (NamingException e) {  
            System.out.println(userDN + " 验证失败");  
            valide = false;  
        }  
        closeContext();  
        return valide;  
    }  
    private  boolean addUser(String usr, String pwd) {  
        
        try {  
            LDAP_connect();  
            BasicAttributes attrsbu = new BasicAttributes();  
            BasicAttribute objclassSet = new BasicAttribute("objectclass");  
            objclassSet.add("inetOrgPerson");  
            attrsbu.put(objclassSet);  
            attrsbu.put("sn", usr);  
            attrsbu.put("cn", usr);  
            attrsbu.put("uid", usr);  
            attrsbu.put("userPassword", pwd);  
            ctx.createSubcontext("uid="+usr, attrsbu);  
  
            return true;  
        } catch (NamingException ex) {  
           ex.printStackTrace();  
        }  
        closeContext();  
        return false;  
    }   
    public static void main(String[] args) {  
        LDAPAuthentication ldap = new LDAPAuthentication();  
          
        ldap.LDAP_connect();  
//        ldap.getUserDN("yorker");
   
//        if(ldap.authenricate("yorker", "secret") == true){  
//   
//            System.out.println( "该用户认证成功" );  
//   
//        }  
        ldap.addUser("czj333","123456");  
          
    }  
}  


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值