我在Ubuntu 16上编写了代码,并尝试连接到Windows Server 2012虚拟机上的ActiveDirectory。 用户名是:siwar 用户密码为:siwarmp 域名是:squeezer.celtron.com VM主机地址(Windows Server 2012):192.168.1.115 以下代码无效并生成了身份验证:
package ldap;
import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.SizeLimitExceededException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class LdapMain {
static DirContext ctx = null;
static String userLog = "cn=siwar,ou=users,dc=squeezer,dc=celtron,dc=com";
// static String userLog =
// "cn=siwar,cn=users,dc=squeezer,dc=celtron,dc=com";
static String userMP = "siwarmp";
public static void main(String args[]) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.1.115:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=squeezer,dc=celtron,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "ldap");
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
getGroup(env, 500);
getRole(env, "readonly");
validateLogin(env, userLog, userMP);
}
private static SearchControls getSimpleSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(30000);
// String[] attrIDs = {"objectGUID"};
// searchControls.setReturningAttributes(attrIDs);
return searchControls;
}
public static Boolean validateLogin(Hashtable<String, String> env, String userName, String userPassword) {
NamingEnumeration<SearchResult> results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setCountLimit(1);
controls.setTimeLimit(5000);
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, userPassword);
ctx = new InitialDirContext(env);
results = ctx.search("ou=users,dc=celtron,dc=com", "(objectclass=inetOrgPerson)",
getSimpleSearchControls());
// results = ctx.search("dc=celtron,dc=com",
// "(objectclass=inetOrgPerson)", getSimpleSearchControls());
results = ctx.search(userName, "(objectclass=*)", getSimpleSearchControls());
System.out.println(results);
while (results.hasMore()) {
SearchResult result = (SearchResult) results.next();
Attributes attrs = result.getAttributes();
Attribute dnAttr = attrs.get("cn");
String dn = (String) dnAttr.get();
System.out.println(dn);
Attribute gidAttr = attrs.get("gidNumber");
String gid = (String) gidAttr.get();
System.out.println(gid);
// User Exists, Validate the Password
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, userPassword);
return true;
}
return false;
} catch (AuthenticationException e) { // Invalid Login
return false;
} catch (NameNotFoundException e) { // The base context was not found.
return false;
} catch (SizeLimitExceededException e) {
throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e);
} catch (NamingException e) {
throw new RuntimeException(e);
} finally {
try {
if (results != null) {
results.close();
}
if (ctx != null) {
ctx.close();
}
} catch (Exception e) { /* Do Nothing */
}
}
}
public static Boolean getRole(Hashtable<String, String> env, String roleName) {
NamingEnumeration<SearchResult> results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setCountLimit(1);
controls.setTimeLimit(5000);
ctx = new InitialDirContext(env);
results = ctx.search("cn=readonly,ou=roles,dc=celtron,dc=com", "(objectclass=organizationalRole)",
getSimpleSearchControls());
while (results.hasMore()) {
SearchResult result = (SearchResult) results.next();
Attributes attrs = result.getAttributes();
Attribute dnAttr = attrs.get("roleOccupant");
String dn = (String) dnAttr.get();
System.out.println(dn);
return true;
}
return false;
} catch (AuthenticationException e) { // Invalid Login
System.out.println("Auth failed");
return false;
} catch (NameNotFoundException e) { // The base context was not found.
return false;
} catch (SizeLimitExceededException e) {
throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e);
} catch (NamingException e) {
throw new RuntimeException(e);
} finally {
try {
if (results != null) {
results.close();
}
if (ctx != null) {
ctx.close();
}
} catch (Exception e) {
}
}
}
public static String getGroup(Hashtable<String, String> env, int gid) {
NamingEnumeration<SearchResult> results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setCountLimit(1);
controls.setTimeLimit(5000);
ctx = new InitialDirContext(env);
results = ctx.search("ou=groups,dc=celtron,dc=com", "(gidNumber=500)", getSimpleSearchControls());
while (results.hasMore()) {
SearchResult result = (SearchResult) results.next();
Attributes attrs = result.getAttributes();
Attribute dnAttr = attrs.get("cn");
String dn = (String) dnAttr.get();
System.out.println(dn);
return dn;
}
return "";
} catch (AuthenticationException e) {
return "";
} catch (NameNotFoundException e) {
return "";
} catch (SizeLimitExceededException e) {
throw new RuntimeException("LDAP Query Limit Exceeded, adjust the query to bring back less records", e);
} catch (NamingException e) {
throw new RuntimeException(e);
} finally {
try {
if (results != null) {
results.close();
}
if (ctx != null) {
ctx.close();
}
} catch (Exception e) {
}
}
}
}