import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming. NamingEnumeration; import javax.naming.NamingException;
public class SimpleBindDemo {
public static void main(String[] args) throws NamingException {
if (args.length < 2) { System.err.println("Usage: java SimpleBindDemo <userDN> <password>"); System.exit(1); }
Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple"); //env.put(Context.SECURITY_PRINCIPAL,"cn=Manager, ou=People,dc=example,dc=com"); //env.put(Context.SECURITY_CREDENTIALS,"ldap123"); env.put(Context.SECURITY_PRINCIPAL,args[0]); env.put(Context.SECURITY_CREDENTIALS,args[1]);
try { Context ctx = new InitialContext(env); NamingEnumeration enm = ctx.list("");
while (enm.hasMore()) { System.out.println(enm.next()); }
enm.close(); ctx.close(); } catch (NamingException e) { System.out.println(e.getMessage()); } } }
------------------------------------------------------------------------------------------------------------------------ --
# extended LDIF # # LDAPv3 # base <dc=example,dc=com> with scope subtree # filter: (objectclass=*) # requesting: ALL #
# example.com dn: dc=example,dc=com dc: example objectClass: top objectClass: domain
# People, example.com dn: ou=People,dc=example,dc=com ou: People objectClass: top objectClass: organizationalUnit
# Group, example.com dn: ou=Group,dc=example,dc=com ou: Group objectClass: top objectClass: organizationalUnit
# nagios, People, example.com dn: uid=nagios,ou=People,dc=example,dc=com objectClass: inetOrgPerson objectClass: posixAccount objectClass: shadowAccount uid: nagios sn: nagios givenName: nagios cn: nagios displayName: nagios uidNumber: 500 gidNumber: 500 userPassword:: gecos: nagios loginShell: /bin/bash homeDirectory: /home/nagios shadowExpire: -1 shadowFlag: 0 shadowWarning: 7 shadowMin: 0 shadowMax: 99999 shadowLastChange: 15496
# test1, People, example.com dn: uid=test1,ou=People,dc=example,dc=com objectClass: inetOrgPerson objectClass: posixAccount objectClass: shadowAccount uid: test1 sn: test1 givenName: test1 cn: test1 displayName: test1 uidNumber: 501 gidNumber: 501 userPassword:: gecos: test1 loginShell: /bin/bash homeDirectory: /home/test1 shadowExpire: -1 shadowFlag: 0 shadowWarning: 7 shadowMin: 0 shadowMax: 99999 shadowLastChange: 16447
# test2, People, example.com dn: uid=test2,ou=People,dc=example,dc=com objectClass: inetOrgPerson objectClass: posixAccount objectClass: shadowAccount uid: test2 sn: test2 givenName: test2 cn: test2 displayName: test2 uidNumber: 502 gidNumber: 502 userPassword:: gecos: test2 loginShell: /bin/bash homeDirectory: /home/test2 shadowExpire: -1 shadowFlag: 0 shadowWarning: 7 shadowMin: 0 shadowMax: 99999 shadowLastChange: 16447
# nagios, Group, example.com dn: cn=nagios,ou=Group,dc=example,dc=com objectClass: posixGroup cn: nagios gidNumber: 500
# test1, Group, example.com dn: cn=test1,ou=Group,dc=example,dc=com objectClass: posixGroup cn: test1 gidNumber: 501
# test2, Group, example.com dn: cn=test2,ou=Group,dc=example,dc=com objectClass: posixGroup cn: test2 gidNumber: 502
# search result search: 2 result: 0 Success
# numResponses: 10 # numEntries: 9
On Fri, Jan 23, 2015 at 10:32:18AM +0530, Bharath K wrote:
Subject: I am new to ldap and i dont know much about ldap simple authentication could you plz help me and give some suggestions......and below is the simple code which i tried and ther is also uid test 1&2 which i want to authenticate
It is best to put your question in the body of the message rather than all in the subject line.
env.put(Context.SECURITY_PRINCIPAL,args[0]); env.put(Context.SECURITY_CREDENTIALS,args[1]);
What values did you supply for these? What happened? What did you expect to happen?
dn: uid=nagios,ou=People,dc=example,dc=com ... userPassword::
dn: uid=test1,ou=People,dc=example,dc=com ... userPassword::
dn: uid=test2,ou=People,dc=example,dc=com ... userPassword::
All three accounts appear to have blank passwords. LDAP servers will not normally allow authentication with a blank password. For testing, you can easily add passwords like this:
userPassword: secret
Note the single ':' character. If you use '::' then the data is expected to be in Base 64.
Andrew
openldap-technical@openldap.org