PDA

View Full Version : Problem with List getAllPersonNames()



SPENAPOLO
Jun 1st, 2007, 12:07 PM
:confused: My problem is the personal function getAllPersonNames(), in the traditional form is ok, but with Spring LDAP KO!!

BEANS
------
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="locations">
<list>
<value>classpath:com/bluestarenergy/provisioning/ldap.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound"><value>true</value></property>
</bean>

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextS ource">
<property name="url">
<value>${ldap.connection.url}</value>
</property>
<property name="base">
<value>${ldap.connection.base}</value>
</property>
</bean>

<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapAuthenticationDAO" class="com.bluestarenergy.provisioning.security.dao.LdapA uthenticationDAOImpl">
<property name="ldapTemplate" ref ="ldapTemplate" />
</bean>



CLASS
------
public class LdapAuthenticationDAOImpl implements LdapAuthenticationDAO {

private LdapTemplate ldapTemplate;

public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}


public List getAllPersonNames() {
return ldapTemplate.search(
"", "(objectclass=person)",
new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("cn").get();
}
});
}
}

TEST
-----
public void testgetAllPersonNames(){
try {
Iterator it=(Iterator)ldapAuthenticationDAO.getAllPersonNam es().iterator();
System.out.println("************Resultados testgetAllPersonNames**************");
while(it.hasNext()){
System.out.println(">>>LDAPOPEN>>>>> "+it.next());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

ulsa
Jun 2nd, 2007, 06:12 AM
It would be helpful to see the stacktrace and the values of url and base in your ldap.properties file. Also, please put the
tag around your code or configuration.

SPENAPOLO
Jun 4th, 2007, 10:18 AM
Hi Ulsa, thank for your help. sorry for my english.

The properties file, in the traditional form don't problem with the file.


#ldap
ldap.connection.url=ldap://0.0.0.0:389
ldap.connection.base=dc=bluestarenergy,dc=com
ldap.connection.username=cn=Manager
ldap.connection.password=secret


I have a debug page in eclipse console


INFO 2007-06-01 18:08:57,305 [main] com.bluestarenergy.provisioning.eisaccess.dao.Ldap AuthenticationDAOimplTest: Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransa ctionManager@130998]; default rollback = true
************INICIO testgetAllPersonNames SPING LDAP**************
DEBUG 2007-06-01 18:08:57,305 [main] org.springframework.ldap.core.support.LdapContextS ource: Principal: ''
DEBUG 2007-06-01 18:08:57,305 [main] org.springframework.ldap.core.support.LdapContextS ource: Got Ldap context on server 'ldap://0.0.0.0:389/dc=bluestarenergy,dc=com'
************Resultados testgetAllPersonNames**************
DEBUG 2007-06-01 18:08:57,305 [main] org.springframework.orm.hibernate3.HibernateTransa ctionManager: Triggering beforeCompletion synchronization
DEBUG 2007-06-01 18:08:57,305 [main] org.springframework.orm.hibernate3.HibernateTransa ctionManager: Initiating transaction rollback
DEBUG 2007-06-01 18:08:57,305 [main] org.springframework.orm.hibernate3.HibernateTransa ctionManager: Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@194df96]


I think the problem is


DEBUG 2007-06-01 18:08:57,305 [main] org.springframework.ldap.core.support.LdapContextS ource: Principal: ''


but, where i configure that?

rasky
Jun 4th, 2007, 11:08 AM
Right, you're not setting the username and password properties on your ContextSource, that might cause some problems.

SPENAPOLO
Jun 4th, 2007, 11:59 AM
Hi Rasky. thank for your help.

I put the userName and password in the contextSource


<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextS ource">
<property name="url">
<value>${ldap.connection.url}</value>
</property>
<property name="base">
<value>${ldap.connection.base}</value>
</property>
<property name="userName">
<value>${ldap.connection.username}</value>
</property>
<property name="password">
<value>${ldap.connection.password}</value>
</property>
</bean>


but, my problem is now......


INFO 2007-06-04 11:33:20,468 [main] com.bluestarenergy.provisioning.eisaccess.dao.Ldap AuthenticationDAOimplTest: Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransa ctionManager@53abbb]; default rollback = true
************INICIO testgetDatos() SPING LDAP**************
DEBUG 2007-06-04 11:33:20,468 [main] org.springframework.ldap.core.support.LdapContextS ource: Principal: 'cn=Manager'
org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]; nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]
Caused by: javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknow n Source)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknow n Source)



I don't understand, my userName and password it's correct

mmmmm, in the traditional form :


Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=mycompany,dc=com");
DirContext ctx;


in Spring-LDAP?????

rasky
Jun 4th, 2007, 02:02 PM
Well, the error message states that the credentials are invalid, so I guess that's your problem. Either the user DN or the password is incorrect.

Note that the dn supplied to the userName property of ContextSource (cn=Manager in your case) needs to have the full DN of the user - the base context is not applied to this. That means that if your manager is located at cn=Manager, dc=mycompany, dc=com you need to specify that full DN to the userName property, ev.en though you are setting the base context to dc=mycompany, dc=com.

Then again, from your test code with plain JNDI I get the impression that you don't supply the security.principal and security.credential properties in that code. Is that correct? In that case you should try setting the ContextSource in anonymous mode by setting the 'anonymousReadOnly' property of your ContextSource to true.

MikeNereson
Jun 10th, 2007, 11:27 AM
I think depending on how your connection is setup or how your Active Directory is setup, you may need to provide more information.

For example, in my organization, I have to pass a username of username@domain.company.com. In my last organization I had to pass DOMAIN\username.

Try those.