Trying to authenticate user in LDAP. Already did it with Spring LDAP1.3RC1 & it is working properly. The code of Spring LDAP 1.3 is given below:
But want to implement it with Spring LDAP 1.2.1. I am able to get UserDn but not able to authenticate.Reason being getContext(uid, password) method that exists in Spring LDAP 1.3 but not in 1.2.1. I have the code part that help me to get UserDn & it is:Code:public class UsernameImpl implements Username { private LdapTemplate ldapTemplate; private ContextSource contextSource; /** * Set method. * * @param ldapTemplate * set. */ public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } /** * Set method. * * @param contextSource * set. */ public void setContextSource(ContextSource contextSource) { this.contextSource = contextSource; } /** * Authenticating password. * * @param userName * user. * @param password * password. * @return true/false. */ public boolean authenticate(String userName, String password) { System.out.println("In Athentication method"); EqualsFilter filter = new EqualsFilter("uid", userName); // Actual filter will differ depending on LDAP Server and schema List < String > results = ldapTemplate.search("", filter.toString(), new DnContextMapper()); if (results.size() != 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } DirContext ctx = null; try { String uid = results.get(0); System.out.println(uid); ctx = contextSource.getContext(uid, password); return true; } catch (Exception e) { return false; } finally { LdapUtils.closeContext(ctx); } } private static final class DnContextMapper extends AbstractParameterizedContextMapper < String > { @Override protected String doMapFromContext(DirContextOperations ctx) { return ctx.getNameInNamespace(); } } public void returnMethod() { List res = getUserNameList(); System.out.println("In Return method"); Iterator ir = res.iterator(); while (ir.hasNext()) { String s1 = (String) ir.next(); System.out.println(s1); } boolean result = authenticate("telephone", "infy12"); System.out.println(result); } /** * Get UserName List. * * @return username. */ public List getUserNameList() { // LdapContextSource ldapContextSource = new LdapContextSource(); // AuthenticationSource res = // ldapContextSource.getAuthenticationSource(); // DistinguishedName dn = ldapContextSource.getBaseLdapPath(); System.out.println("In getUserNameList Method"); return ldapTemplate.search("", "uid=telephone", new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { System.out.println("In mapFromAttributes Method"); return attrs.get("mail").get(); } }); } }
Code:public boolean authenticate(String userName, String password) { System.out.println("In Athentication method"); EqualsFilter filter = new EqualsFilter("uid", userName); // Actual filter will differ depending on LDAP Server and schema List results = ldapTemplate.search("", filter.toString(), new DnContextMapper()); if (results.size() != 1) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } DirContext ctx = null; try { String uid = (String) results.get(0); System.out.println(uid); //Getting uid here & wants to authenicate by binding (need some method ) System.out.println("Authenticated"); return true; } catch (Exception e) { return false; } finally { LdapUtils.closeContext(ctx); } } private class DnContextMapper extends AbstractContextMapper { @Override protected Object doMapFromContext(DirContextOperations ctx) { // TODO Auto-generated method stub return ctx.getNameInNamespace(); } }
Any kind of help is appreciated.
Thanks


Reply With Quote