I am new to LDAP and AD. Please help me understand what I am doing wrong.
I have used the above code and modified to my own server, domain, etc.
It seems I am missing something. I am trying to non-anonymously connect to the AD. So, in detail:
1 - I have a jsp and a servlet. I have a class with the login for ldap. Servlet calls ldap to connect to AD.
2 - In my ldap class, I have the credentials supplied, and I am searching for a username and a password. As of right now, I have creds supplied for testing. I would like to eventually be able to go all the way to non-anon.
3 - Problem: username and password are hard-coded in and matched in the page prompt. I get an error saying username is null. I have debugged and, sure enough, it's null. It should be auto-set considering it was hardcoded in.
4 - another problem is the ldap bind. There is a problem binding since I can't access the active directory.
Here is the code I have for the LDAP class. Please help me.
private transient String userLogin;
private transient String userPassword;
User user;
Hashtable<String, String> envGC = new Hashtable<String, String>();
Hashtable<String, String> envDC = new Hashtable<String, String>();
public LDAPCrap() throws NamingException {
// envGC = new Hashtable();
// envDC = new Hashtable();
String urlGC = "ldap://ssc-gc-01.mycompany.com:3268";
String urlDC = "ldap://ssc-dc-01.ssc.mycompany.com:389";
envGC.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
envDC.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
envGC.put(Context.PROVIDER_URL, urlGC);
envDC.put(Context.PROVIDER_URL, urlDC);
}
public void setUserLogin(String uid) {
this.userLogin = uid;
}
public void setUserPass(String password) {
this.userPassword = password;
}
public boolean isAuth() throws NamingException {
DirContext ctxGC = new InitialDirContext(envGC);
DirContext ctxDC = new InitialDirContext(envDC);
boolean auth = false;
try {
ctxDC.getNameInNamespace();
ctxGC.getNameInNamespace();
// String userDN = getDN(this.userLogin);
envGC.put(Context.SECURITY_AUTHENTICATION, "simple");
envGC.put(Context.SECURITY_PRINCIPAL,
"cn=username,cn=Users,dc=ssc,dc=mycompany,dc=com");
// envGC.put(Context.SECURITY_PRINCIPAL, "cn=" + this.userLogin
// + "cn=Users,dc=ssc,dc=mycompany,dc=com");
envGC.put(Context.SECURITY_CREDENTIALS, this.userPassword);
envDC.put(Context.SECURITY_AUTHENTICATION, "simple");
envDC.put(Context.SECURITY_PRINCIPAL,
"cn=username,cn=Users,dc=ssc,dc=mycompany,dc=com");
// envDC.put(Context.SECURITY_PRINCIPAL, "cn=" + this.userLogin
// + "cn=Users,dc=ssc,dc=mycompany,dc=com");
envDC.put(Context.SECURITY_CREDENTIALS, "password");
envDC.put(Context.REFERRAL, "follow");
envGC.put(Context.REFERRAL, "follow");
auth = true;
} catch (AuthenticationException e) {
auth = false;
} catch (NamingException e) {
auth = false;
System.out.println("Error in Authentication " + e);
}
String base = "";
int totalResults = 0;
String filter = "(objectclass=*)";
ctxDC.bind(userLogin, user);
ctxGC.bind(userLogin, user);
SearchControls controls = new SearchControls();
// may be a scope change
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> answer = ctxGC.search(base, filter, controls);
// ... process attributes ...
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
System.out.println("RootDSE: " + sr.getName());
if (attrs != null) {
try {
System.out.println(" Naming Context: "
+ attrs.get("defaultNamingContext").get());
System.out.println(" Schema Context: "
+ attrs.get("schemaNamingContext").get());
System.out.println(" DNS: "
+ attrs.get("dnsHostName").get());
System.out.println(" Server Name: "
+ attrs.get("serverName").get());
System.out
.println(" name(GC) Context: "
+ attrs.get("givenName").get() + ""
+ attrs.get("sn").get());
System.out.println(" mail(GC) Context: "
+ attrs.get("mail").get());
} catch (NullPointerException e) {
System.err
.println("Problem listing attributes from Global Catalog: "
+ e);
}
}
Attributes DCattrs = ctxDC.getAttributes(sr.getName());
try {
System.out.println(" Web(DC_):"
+ DCattrs.get("wWWHomePage").getID());
System.out.println(" Fax(DC):"
+ DCattrs.get("facsimileTelphoneNumber").getID());
// } catch (NamingException e) {
//
// System.out.println("Problem retrieving RootDSE: " + e);
} catch (NullPointerException e) {
System.err.println("Problem listing attributes from Domain "
+ " Controller:" + e);
}
try {
System.out.println("Total Results:" + totalResults);
ctxDC.unbind(userLogin);
ctxGC.unbind(userLogin);
ctxDC.close();
ctxGC.close();
} catch (Exception e) {
System.out.println("FAILED: " + e.getLocalizedMessage());
}
}
return (auth);
}
Any direction is appreciated.