---
title: "AD域的Java开发操作"
author: "Alex_hyts"
date: "2016-05-06T15:52:15Z"
tags: []
categories: ["编程语言"]
slug: "ad-java"
draft: false
lastmod: "2016-05-06T15:52:15Z"
id: 2296355
url: "https://alex4java.iteye.com/blog/2296355"
---

001. import java.util.ArrayList;
002. import java.util.HashMap;
003. import java.util.Hashtable;
004. import java.util.List;
005. import java.util.Map;
006. import javax.naming.Context;
007. import javax.naming.NamingEnumeration;
008. import javax.naming.directory.Attributes;
009. import javax.naming.directory.SearchControls;
010. import javax.naming.directory.SearchResult;
011. import javax.naming.ldap.InitialLdapContext;
012. import javax.naming.ldap.LdapContext;
013. /\**
014. * LDAP 连接
015. \*/
016. public class LDAPConnection {
017. private String baseDN;
018. private String filter;
019. private String\[] attributes;
020. private Hashtable&lt;String, String&gt; env = null;
021. private static LDAPConnection lc;
022. private LDAPConnection() {
023. //搜索根节点
024. baseDN = "\*\*\*\*\*\*";
025. //要查询的属性列
026. attributes = new String\[]{"cn"};
027. //过滤条件
028. if ((filter == null) || (filter == ""))
029. filter = "objectclass=\*";
030. }
031. public static LDAPConnection getInstance() {
032. if (lc == null) {
033. lc = new LDAPConnection();
034. }
035. return lc;
036. }
037. /\**
038. * 建立LDAP连接
039. * @return boolean
040. \*/
041. private LdapContext getLdapContext() {
042. env = new Hashtable&lt;String, String&gt;();
043. env.put(Context.INITIAL\_CONTEXT\_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
044. env.put(Context.PROVIDER\_URL, "ldap://\*\*\*\*\*:389");
045. env.put(Context.SECURITY\_AUTHENTICATION, "simple");
046. env.put(Context.SECURITY\_CREDENTIALS, "123456");
047. env.put(Context.SECURITY\_PRINCIPAL, "\*\*\*\*");
048. try {
049. return new InitialLdapContext(env, null);
050. } catch (Exception e) {
051. System.out.println("连接服务器失败！");
052. e.printStackTrace();
053. }
054. return null;
055. }
056. /\**
057. * 获取用户信息
058. * @return List&lt;Map&gt;
059. \*/
060. public List&lt;Map&gt; getUsers() {
061. LdapContext ctx = getLdapContext();
062. if(ctx == null){
063. return null;
064. }
065. List&lt;Map&gt; list = new ArrayList&lt;Map&gt;();
066. try {
067. SearchControls constraints = new SearchControls();
068. constraints.setReturningAttributes(attributes);
069. constraints.setSearchScope(SearchControls.SUBTREE\_SCOPE);
070. NamingEnumeration&lt;?&gt; en = ctx.search(baseDN, filter, constraints);
071. while (en != null && en.hasMoreElements()) {
072. Object obj = en.nextElement();
073. if (obj instanceof SearchResult) {
074. SearchResult si = (SearchResult) obj;
075. Attributes attrs = si.getAttributes();
076. Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
077. for (int i = 0; i &lt; attributes.length; i++) {
078. String attributeName = attributes\[i];
079. if(attrs.get(attributeName) == null){
080. map.put(attributeName, attrs.get(attributeName));
081. }else{
082. map.put(attributeName, attrs.get(attributeName).get());
083. }
084. }
085. System.out.println(map);
086. list.add(map);
087. } else {
088. System.out.println(obj);
089. }
090. }
091. } catch (Exception e) {
092. e.printStackTrace();
093. }
094. System.out.println("总符合条件记录数："+list.size());
095. return list;
096. }
097. public static void main(String arg\[]) {
098. LDAPConnection.getInstance().getUsers();
099. }
100. }