路人

导航

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿

随笔档案(39)

相册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

java 连接 ladp(sun one)

package com.hz.ldap;

import java.util.Hashtable;
import javax.naming.NamingException;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class LDAPConnection
{
  private String connectionName = "";

  private LdapContext ctx = null;
  static Control[] initcontrols;

  public LDAPConnection(String url, String username, String password, String authenSchema)
  {
    this(url, username, password, authenSchema, false);
  }

  public LDAPConnection(String url, String username, String password, String authenSchema, boolean isSSL, int ldapType)
  {
    Hashtable<String,String> env = new Hashtable<String,String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");

    env.put("java.naming.security.authentication", authenSchema);
    env.put("java.naming.provider.url", url);
    env.put("java.naming.security.principal", username);
    env.put("java.naming.security.credentials", password);
    env.put("com.sun.jndi.ldap.connect.pool", "true");

    env.put("com.sun.jndi.ldap.connect.pool.timeout", "300000");
    env.put("com.sun.jndi.ldap.connect.pool.prefsize", "50");
    env.put("com.sun.jndi.ldap.connect.pool.maxsize", "90");
    env.put("java.naming.referral", "follow");
    if (isSSL) {
      env.put("java.naming.security.protocol", "ssl");
      switch (ldapType) {
      case 1:
        String jrePath = System.getProperty("java.home");
        String keystore = "";
        if (jrePath.indexOf("\\") != -1)
          keystore = jrePath + "\\lib\\security\\cacerts";
        else {
          keystore = jrePath + "/lib/security/cacerts";
        }
        System.setProperty("javax.net.ssl.trustStore", keystore);
        break;
      default:
        env.put("java.naming.ldap.factory.socket", "cn.com.chinautrust.idm.connector.ldap.ssl.AdvancedSocketFactory");
      }

    }

    try
    {
      this.ctx = new InitialLdapContext(env, null);
      env = null;
      if (initcontrols == null)
        initcontrols = this.ctx.getRequestControls();
    }
    catch (NamingException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public LDAPConnection(String url, String username, String password, String authenSchema, boolean isSSL)
  {
    this(url, username, password, authenSchema, false, 2);
  }

  public void close()
  {
    if (this.ctx != null)
      try {
        this.ctx.close();
        this.ctx = null;
      } catch (NamingException e) {
        try {
          this.ctx.close();
        } catch (NamingException ex) {
          ex.printStackTrace();
        }
        e.printStackTrace();
      }
  }

  public void reset()
  {
    try
    {
      this.ctx.setRequestControls(initcontrols);
    } catch (NamingException ex) {
      ex.printStackTrace();
    }
  }

  public LdapContext getLdapContext()
  {
    return this.ctx;
  }

  public void setLdapContext(LdapContext ctx)
  {
    this.ctx = ctx;
  }

  public String getConnectionName()
  {
    return this.connectionName;
  }

  public void setConnectionName(String connectionName)
  {
    this.connectionName = connectionName;
  }
}



package com.hz.ldap;

import java.security.MessageDigest;

import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;

public class LDAPTest {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        LdapContext ctx = null;

        LDAPConnection conn = null;

        String adminName = "cn=Directory Manager";

        String adminPassword = "password12345";

        String ladpURL = "ldap://192.168.1.135:389";
        String authoenSchema = "simple";

        conn = new LDAPConnection(ladpURL, adminName, adminPassword,
                authoenSchema);

        try {
            ctx = conn.getLdapContext();
            SearchControls sc = new SearchControls();
            sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

            String searchFilter = "(objectclass=person)";
            String searchBase = "ou=person,ou=utrust,dc=chinautrust,dc=com";

            String rs[] = { "uid", "utrustpwd" };
            sc.setReturningAttributes(rs);

            NamingEnumeration anser = ctx.search(searchBase, searchFilter, sc);

            while (anser.hasMoreElements()) {

                SearchResult sr = (SearchResult) anser.next();
                Attributes attrs = sr.getAttributes();
                attrs.get("uid");

                String uid = attrs.get("uid").get(0).toString();
                String pwd = attrs.get("utrustpwd").get(0).toString();
                System.out.println(uid + "\t" + pwd);

            }

            ctx.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        System.out.println(MD5("111").length());
    }

    public final static String MD5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

posted on 2012-09-28 17:42 山岗 阅读(1368) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。