Reputation: 551
I need to write a piece of code in Java which will make a connection to LDAP and retrieve a few values from there.
I need to know what details I need to establish the connection to LDAP.
Upvotes: 3
Views: 8411
Reputation: 1775
Here is the code for LDAP connection..
public Connection()
{
try
{
System.setProperty("javax.net.ssl.trustStore", TRUST_STORE);
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, "ldap://localhost:389");
ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapEnv.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL + BASE_NAME);
ldapEnv.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS);
ldapContext = new InitialDirContext(ldapEnv);
catch (Exception e)
{
System.out.println(" bind error: " + e);
e.printStackTrace();
System.exit(-1);
}
}
Upvotes: 1
Reputation: 23352
This Spring LDAP configuration tutorial will help you implement a Java client for LDAP. It contains piece of code to connect to LDAP and do operations.
Upvotes: 0
Reputation: 10658
Use jldap
Here is the example code:
int ldapVersion = LDAPConnection.LDAP_V3;
try
{
if(conn == null)
conn = new LDAPConnection();
// connect to the server
if(conn.isConnected() == false)
conn.connect(hostName, port);
// bind to the server
if(authType.equals("Anonymous"))
{
conn.bind("Anonymous" ,null);
}
else
{
conn.bind(ldapVersion, login, password.getBytes("UTF8"));
}
Logs.write("LDAP CONNECTION Established ");
return true;
}
catch (LDAPException ex) {
Logs.write("CONNECTION ERROR "+ex.toString());
return false;
}
catch (IllegalArgumentException ex)
{
Logs.write("CONNECTION ERROR "+ex.toString());
return false;
}
Upvotes: 0
Reputation: 8167
Here you got some snippets of code to show how it looks to implement a change password operation, you could use it as a starting point to learn more about LDAP connection from Java. Check the method getCtx()...
package so;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
public class DemoLdap4SO {
private void changePassword(String principal, String oldPassword, String newPassword)
throws NamingException {
InitialDirContext ctx = getCtx(principal, oldPassword);
if (ctx == null || newPassword == null || newPassword.equals("")) {
throw new NamingException();
}
BasicAttribute attr = new BasicAttribute("userpassword", newPassword);
ModificationItem mi = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
ModificationItem[] items = new ModificationItem[1];
items[0] = mi;
ctx.modifyAttributes(getUserDN(principal), items);
}
private String getUserDN(String user) {
String m_usersDn = "cn=Users,your realm";
String usrDn = "cn=" + user + "," + m_usersDn;
return usrDn;
}
private InitialDirContext getCtx(String user, String pswd) throws NamingException {
String ldapUrl = "put your ldap url here";
String ldapRealm = "put your realm here";
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
ht.put(Context.PROVIDER_URL, ldapUrl);
ht.put(Context.SECURITY_AUTHENTICATION, "simple");
ht.put(Context.SECURITY_PRINCIPAL, getUserDN(user));
ht.put(Context.SECURITY_CREDENTIALS, pswd);
try {
return new InitialDirContext(ht);
} catch (NamingException exc) {
// log error
}
return null;
}
}
Upvotes: 4
Reputation: 10994
Java uses JNDI as a means to interface with a LDAP directory server. There is a great JNDI tutorial provided by Oracle. That will detail the JNDI API and explain how it relates to LDAP operations. It is replete with code examples on how to connect, authenticate, and query a directory.
Upvotes: 4