java 代码
一共两个java类,加一个配置文件
LDAP.java---------------连接LDAP服务器,判断用户名密码正确与否
UMParas.java----------jDom解析xml配置文件
ldapconfig.xml---------配置文件,里面有服务器的参数信息
LDAP.java 代码
- package ldap2;
- import java.util.Hashtable;
- import javax.naming.AuthenticationException;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.naming.directory.DirContext;
- public class LDAP {
- private Hashtable env = null;
- private DirContext ctx = null;
- private boolean islogin = false;
- StringBuffer url;
- String host;
- String port;
- String admin;
- String password;
- String baseDN;
- public LDAP(String id, String pwd) {
- try {
- host = UMParas.getPara("hostname");
- port = UMParas.getPara("port");
- baseDN = UMParas.getPara("basedn");
- admin = UMParas.getPara("admin");
- password = UMParas.getPara("pwd");
- url = new StringBuffer("LDAP://");
- url.append(host).append(":").append(port);
- url.append("/").append(baseDN);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("");
- }
- // pwd="secret";
- env = new Hashtable();
- env.put("java.naming.factory.initial",
- "com.sun.jndi.ldap.LdapCtxFactory");
- env.put("java.naming.provider.url", url.toString());
- env.put(Context.SECURITY_AUTHENTICATION, "simple");
- env.put("java.naming.security.principal", admin);
- env.put("java.naming.security.credentials", password);
- System.out.println("-------------");
- }
- public boolean checkAd() { //admin用户验证
- try {
- System.out.println("-----ddd--------");
- InitialContext iCnt = new InitialContext(env);
- System.out.println("-------eee------");
- islogin = true;
- } catch (AuthenticationException aue) {
- // aue.printStackTrace();
- islogin = false;
- } catch (NamingException e) {
- e.printStackTrace();
- } catch (Exception eee) {
- eee.printStackTrace();
- } finally {
- try {
- ctx.close();
- } catch (Exception ie) {
- }
- }
- return islogin;
- }
- public boolean userLogin(String userName, String password) { //新建用户验证。
- Hashtable envi = new Hashtable();
- try {
- envi.put("java.naming.factory.initial",
- "com.sun.jndi.ldap.LdapCtxFactory");
- envi.put("java.naming.provider.url", url.toString());
- envi.put(Context.SECURITY_AUTHENTICATION, "simple");
- envi.put("java.naming.security.principal", userName);
- envi.put("java.naming.security.credentials", password);
- InitialContext iCnt = new InitialContext(envi);
- return true;
- } catch (Exception e) {
- //e.printStackTrace();
- return false;
- } finally {
- try {
- ctx.close();
- } catch (Exception ie) {
- }
- }
- }
- }
UMParas.java 代码
- package ldap2;
- import java.io.*;
- import java.util.*;
- import org.jdom.*;
- import org.jdom.input.SAXBuilder;
- public class UMParas {
- private static HashMap prop;
- private static long lastLoadTime;
- private static long interval = 0x186a0L; //refresh per 100 second
- // static Class class$0; /* synthetic field */
- public UMParas() {
- }
- //input an para and return the result
- public static synchronized String getPara(String paras)
- throws IllegalArgumentException {
- if (paras == null || paras.trim().length() == 0)
- throw new IllegalArgumentException("Parameter's value invalid.");
- long currentTime = System.currentTimeMillis();
- if (prop == null || currentTime - lastLoadTime > interval)
- reloadDom();
- Object obj = prop.get(paras);
- if (obj != null)
- return (String) obj;
- else
- return null;
- }
- //load the xml file
- private static synchronized void reloadDom() {
- if (prop == null)
- prop = new HashMap();
- SAXBuilder builder = new SAXBuilder();
- Document read_doc = null;
- try {
- read_doc = builder.build(UMParas.class
- .getResource("ldapconfig.xml"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Element rootElement = read_doc.getRootElement();
- List list = rootElement.getChildren("para");
- for (Iterator i = list.iterator(); i.hasNext();) {
- Element current = (Element) i.next();
- List item = current.getChildren("item");
- Attribute code;
- Attribute value;
- for (Iterator j = item.iterator(); j.hasNext(); prop.put(code
- .getValue(), value.getValue())) {
- Element init = (Element) j.next();
- code = init.getAttribute("code");
- value = init.getAttribute("value");
- }
- }
- System.out.println("load sucess");
- lastLoadTime = System.currentTimeMillis();
- }
- public static void main(String args[]) {
- System.out.println(getPara("hostname"));
- }
- }
ldapconfig.xml 代码
- <?xml version="1.0" encoding="GBK"?>
- <sys_para>
- <para>
- <item code="hostname" value="192.168.1.106" description="LDAP服务器IP"/>
- <item code="port" value="10389" description="服务器端口"/>
- <item code="admin" value="uid=admin,ou=system" description="管理员帐号"/>
- <item code="pwd" value="secret" description="密码"/>
- <item code="basedn" value="ou=system" description="组织名(基准DN)"/>
- </para>
- </sys_para>