java访问LDAP服务器

java 代码

一共两个java类,加一个配置文件

LDAP.java---------------连接LDAP服务器,判断用户名密码正确与否

UMParas.java----------jDom解析xml配置文件

ldapconfig.xml---------配置文件,里面有服务器的参数信息

LDAP.java 代码
  1. package ldap2;   
  2.   
  3. import java.util.Hashtable;   
  4. import javax.naming.AuthenticationException;   
  5. import javax.naming.Context;   
  6. import javax.naming.InitialContext;   
  7. import javax.naming.NamingException;   
  8. import javax.naming.directory.DirContext;   
  9.   
  10. public class LDAP {   
  11.   
  12.     private Hashtable env = null;   
  13.   
  14.     private DirContext ctx = null;   
  15.   
  16.     private boolean islogin = false;   
  17.   
  18.     StringBuffer url;   
  19.   
  20.     String host;   
  21.   
  22.     String port;   
  23.   
  24.     String admin;   
  25.   
  26.     String password;   
  27.   
  28.     String baseDN;   
  29.   
  30.     public LDAP(String id, String pwd) {   
  31.         try {   
  32.             host = UMParas.getPara("hostname");   
  33.             port = UMParas.getPara("port");   
  34.             baseDN = UMParas.getPara("basedn");   
  35.             admin = UMParas.getPara("admin");   
  36.             password = UMParas.getPara("pwd");   
  37.             url = new StringBuffer("LDAP://");   
  38.             url.append(host).append(":").append(port);   
  39.             url.append("/").append(baseDN);   
  40.   
  41.         } catch (Exception e) {   
  42.             e.printStackTrace();   
  43.             System.out.println("");   
  44.         }   
  45.         //      pwd="secret";   
  46.         env = new Hashtable();   
  47.   
  48.         env.put("java.naming.factory.initial",   
  49.                 "com.sun.jndi.ldap.LdapCtxFactory");   
  50.         env.put("java.naming.provider.url", url.toString());   
  51.         env.put(Context.SECURITY_AUTHENTICATION, "simple");   
  52.   
  53.         env.put("java.naming.security.principal", admin);    
  54.         env.put("java.naming.security.credentials", password);   
  55.         System.out.println("-------------");   
  56.     }   
  57.   
  58.     public boolean checkAd() {                      //admin用户验证   
  59.         try {   
  60.             System.out.println("-----ddd--------");   
  61.             InitialContext iCnt = new InitialContext(env);   
  62.             System.out.println("-------eee------");   
  63.             islogin = true;   
  64.         } catch (AuthenticationException aue) {   
  65.             //            aue.printStackTrace();   
  66.             islogin = false;   
  67.   
  68.         } catch (NamingException e) {   
  69.   
  70.             e.printStackTrace();   
  71.         } catch (Exception eee) {   
  72.             eee.printStackTrace();   
  73.   
  74.         } finally {   
  75.             try {   
  76.                 ctx.close();   
  77.             } catch (Exception ie) {   
  78.   
  79.             }   
  80.         }   
  81.         return islogin;   
  82.     }   
  83.   
  84.     public boolean userLogin(String userName, String password) {  //新建用户验证。   
  85.         Hashtable envi = new Hashtable();   
  86.         try {   
  87.             envi.put("java.naming.factory.initial",   
  88.                     "com.sun.jndi.ldap.LdapCtxFactory");   
  89.             envi.put("java.naming.provider.url", url.toString());   
  90.             envi.put(Context.SECURITY_AUTHENTICATION, "simple");   
  91.             envi.put("java.naming.security.principal", userName);   
  92.             envi.put("java.naming.security.credentials", password);   
  93.             InitialContext iCnt = new InitialContext(envi);   
  94.             return true;   
  95.         } catch (Exception e) {   
  96.             //e.printStackTrace();   
  97.             return false;   
  98.         } finally {   
  99.             try {   
  100.                 ctx.close();   
  101.             } catch (Exception ie) {   
  102.   
  103.             }   
  104.         }   
  105.     }   
  106. }  

 

 

UMParas.java 代码
  1. package ldap2;   
  2.   
  3. import java.io.*;   
  4. import java.util.*;   
  5. import org.jdom.*;   
  6. import org.jdom.input.SAXBuilder;   
  7.   
  8. public class UMParas {   
  9.   
  10.     private static HashMap prop;   
  11.   
  12.     private static long lastLoadTime;   
  13.   
  14.     private static long interval = 0x186a0L; //refresh per 100 second   
  15.     //    static Class class$0; /* synthetic field */   
  16.   
  17.     public UMParas() {   
  18.     }   
  19.   
  20.     //input an para and return the result    
  21.     public static synchronized String getPara(String paras)   
  22.             throws IllegalArgumentException {   
  23.         if (paras == null || paras.trim().length() == 0)   
  24.             throw new IllegalArgumentException("Parameter's value invalid.");   
  25.         long currentTime = System.currentTimeMillis();   
  26.         if (prop == null || currentTime - lastLoadTime > interval)   
  27.             reloadDom();   
  28.         Object obj = prop.get(paras);   
  29.         if (obj != null)   
  30.             return (String) obj;   
  31.         else  
  32.             return null;   
  33.     }   
  34.   
  35.     //load the xml file   
  36.     private static synchronized void reloadDom() {   
  37.         if (prop == null)   
  38.             prop = new HashMap();   
  39.         SAXBuilder builder = new SAXBuilder();   
  40.         Document read_doc = null;   
  41.         try {   
  42.             read_doc = builder.build(UMParas.class  
  43.                     .getResource("ldapconfig.xml"));   
  44.         } catch (FileNotFoundException e) {   
  45.             e.printStackTrace();   
  46.         } catch (JDOMException e) {   
  47.             e.printStackTrace();   
  48.         } catch (IOException e) {   
  49.             e.printStackTrace();   
  50.         }   
  51.         Element rootElement = read_doc.getRootElement();   
  52.         List list = rootElement.getChildren("para");   
  53.         for (Iterator i = list.iterator(); i.hasNext();) {   
  54.             Element current = (Element) i.next();   
  55.             List item = current.getChildren("item");   
  56.             Attribute code;   
  57.             Attribute value;   
  58.             for (Iterator j = item.iterator(); j.hasNext(); prop.put(code   
  59.                     .getValue(), value.getValue())) {   
  60.                 Element init = (Element) j.next();   
  61.                 code = init.getAttribute("code");   
  62.                 value = init.getAttribute("value");   
  63.             }   
  64.   
  65.         }   
  66.         System.out.println("load sucess");   
  67.         lastLoadTime = System.currentTimeMillis();   
  68.     }   
  69.   
  70.     public static void main(String args[]) {   
  71.         System.out.println(getPara("hostname"));   
  72.     }   
  73.   
  74. }   

 

ldapconfig.xml 代码
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <sys_para>  
  3.   <para>  
  4.     <item code="hostname"        value="192.168.1.106"      description="LDAP服务器IP"/>  
  5.     <item code="port"            value="10389"                  description="服务器端口"/>  
  6.     <item code="admin"           value="uid=admin,ou=system"                description="管理员帐号"/>  
  7.     <item code="pwd"             value="secret"             description="密码"/>  
  8.     <item code="basedn"          value="ou=system"                  description="组织名(基准DN)"/>  
  9.   </para>  
  10. </sys_para>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值