J
John English
I'm trying to figure out how to authenticate a username and password
against an LDAP server in Java. This is how I would do it in PHP (with
error checking removed for simplicity):
$ldap = ldap_connect("ldap.bton.ac.uk");
ldap_bind($ldap);
$srch = ldap_search($ldap,"ou=people,dc=brighton,dc=ac,dc=uk",
"uid=$user",array("uid"));
$dn = ldap_get_dn($ldap,ldap_first_entry($ldap,$srch));
ldap_unbind($ldap);
$ldap = ldap_connect("ldap.bton.ac.uk");
if (@ldap_bind($ldap,$dn,$password)) {
print "$user authenticated";
}
else {
print "$user: authentication failed";
}
In Java I have tried this:
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
"ldap://ldap.bton.ac.uk:389/cn="
+ user + ",ou=people,dc=brighton,dc=ac,dc=uk");
env.put(Context.SECURITY_AUTHENTICATION,
"simple");
env.put(Context.SECURITY_CREDENTIALS,
password);
ctx = new InitialDirContext(env);
This obviously ain't right; I get back an InitialDirContext no matter
what password I specify. Can anyone give me a word-of-one-syllable
explanation of how I can acheive the equivalent of my PHP snippet
above?
TIA,
-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
Dept. of Computing | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
against an LDAP server in Java. This is how I would do it in PHP (with
error checking removed for simplicity):
$ldap = ldap_connect("ldap.bton.ac.uk");
ldap_bind($ldap);
$srch = ldap_search($ldap,"ou=people,dc=brighton,dc=ac,dc=uk",
"uid=$user",array("uid"));
$dn = ldap_get_dn($ldap,ldap_first_entry($ldap,$srch));
ldap_unbind($ldap);
$ldap = ldap_connect("ldap.bton.ac.uk");
if (@ldap_bind($ldap,$dn,$password)) {
print "$user authenticated";
}
else {
print "$user: authentication failed";
}
In Java I have tried this:
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
"ldap://ldap.bton.ac.uk:389/cn="
+ user + ",ou=people,dc=brighton,dc=ac,dc=uk");
env.put(Context.SECURITY_AUTHENTICATION,
"simple");
env.put(Context.SECURITY_CREDENTIALS,
password);
ctx = new InitialDirContext(env);
This obviously ain't right; I get back an InitialDirContext no matter
what password I specify. Can anyone give me a word-of-one-syllable
explanation of how I can acheive the equivalent of my PHP snippet
above?
TIA,
-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
Dept. of Computing | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------