Pages

Ads 468x60px

Wednesday, July 25, 2012

Java: Add User in IBM Tivoli Directory Server (TDS) LDAP using JNDI

It will help you to add user or other entries in IBM Tivoli Directory Server (TDS) using JNDI. I used default values and my own user information, so you have to change it according your requirements. Also the user attributes will depend upon your directory server schema. For connection with  IBM Tivoli Directory Server (TDS)/ LDAP visit: http://usmanali112.blogspot.com/2012/07/java-authentication-and-connection-with.html



import javax.naming.ldap.InitialLdapContext;
import javax.naming.directory.*;

//this class perform all LDAP actions for the user
public class LDAPService {

private String base = "c=us"; // base for LDAP
private String realm = "cn=realm1"; // LDAP realm for user

// User information
private String surname = "Ali"; // user surname
private String givenname = "Usman"; // user given name or first name
private String userpassword = "********"; // user password
private String email = "usmanalibutt9393@gmail.com"; // user email
private String distinguish_name = "sn=" + surname + "," + realm + "," + base;

// to add user in LDAP (TDS)
public void addUser() {
try {
InitialLdapContext context = LDAPConnection.InitializeContext();

Attributes attributes = new BasicAttributes();
Attribute objectClass = new BasicAttribute("objectClass");
objectClass.add("inetOrgPerson");
attributes.put(objectClass);

// Define User attributes
attributes.put("sn", surname);
attributes.put("givenName", givenname);
attributes.put("userPassword", userpassword);
attributes.put("mail", email);

// call API to add user
context.createSubcontext(distinguish_name, attributes);

// Close LDAP Context
LDAPConnection.CloseContext();

} catch (Exception ex) {
ex.getMessage();
}
}
}

No comments:

Post a Comment