Bonsoir,
J’essaie de récupérer des données dans un ldap avec spring mais j'ai quelques problèmes dans mon code que je n'arrive pas à résoudre.
Mon code :
-Un fichier ContextSource :
- un fichier ressource :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import javax.naming.Context; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.support.LdapContextSource; public class LdapContextSourceFactory { public static ContextSource getLdapContextSource() throws Exception { LdapContextSource ldapContextSource = new LdapContextSource(); ldapContextSource.setUrl("ldap://xxxxxxxxxxxxxxxxxxxx:389"); ldapContextSource.setUserDn("anonymous"); ldapContextSource.setPassword(""); ldapContextSource.setBase("cn=admin, dc=xxxxxxx, dc=fr, ou=people"); ldapContextSource.afterPropertiesSet(); return ldapContextSource; } }
-un fichier ressourceDao :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 import java.util.Hashtable; public class Ressources { //pour strocker nos donnees Hashtable ht = new Hashtable(); }
-Un fichier testLdap :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 import java.util.List; import javax.naming.Name; import javax.naming.directory.Attributes; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; public class RessourcesDao { private static class RessourcesAttributMapper implements AttributesMapper { public Ressources mapFromAttributes(Attributes attrs) throws javax.naming.NamingException { Ressources p = new Ressources(); p.ht.put("uid", attrs.get("uid").toString()); p.ht.put("mail", attrs.get("mail").toString()); p.ht.put("givenName", attrs.get("givenName").toString()); p.ht.put("sn", attrs.get("sn").toString()); return p; } } private LdapTemplate ldapTemplate; public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } private Name buildDn(String uid) { DistinguishedName dn = new DistinguishedName(); dn.add("ou", "people"); dn.add("uid", uid); return dn; } public Ressources findByPrimaryKey(String uid) { Name dn = buildDn(uid); return (Ressources) ldapTemplate.lookup(dn, new RessourcesAttributMapper()); } }
Lors du Clean & Build je n'ai aucun problème, mais lorsque je run le projet j'ai l'erreur suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import java.util.Enumeration; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.LdapTemplate; public class TestLdap { public static void main(String[] args) { ContextSource ldapContextSource = null; try { ldapContextSource = LdapContextSourceFactory.getLdapContextSource(); } catch (Exception e) { System.out.println("Impossible to get a LdapContextSource."); e.printStackTrace(); } LdapTemplate ldapTemplate = new LdapTemplate(); ldapTemplate.setContextSource(ldapContextSource); RessourcesDao dao = new RessourcesDao(); dao.setLdapTemplate(ldapTemplate); System.out.println("test"); Ressources r = dao.findByPrimaryKey("un uid"); Enumeration e = r.ht.elements(); while(e.hasMoreElements()) { System.out.println(e.nextElement()); } } }Si j'ai bien compris, je récupère un mauvais dn dans la fonction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 Exception in thread "main" org.springframework.ldap.InvalidNameException: [LDAP: error code 34 - invalid DN]; nested exception is javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN] at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:128) at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:285) at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:119) at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:138) at org.springframework.ldap.core.LdapTemplate.executeReadOnly(LdapTemplate.java:791) at org.springframework.ldap.core.LdapTemplate.lookup(LdapTemplate.java:846) at com.mycompany.ldap.RessourcesDao.findByPrimaryKey(RessourcesDao.java:49) at com.mycompany.ldap.TestLdap.main(TestLdap.java:32) Caused by: javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN] at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3028) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2835) at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2749) at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:316) at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:193) at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:211) at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154) at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:84) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) at javax.naming.InitialContext.init(InitialContext.java:242) at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:153) at org.springframework.ldap.core.support.LdapContextSource.getDirContextInstance(LdapContextSource.java:43) at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:273)Mais si le problème vient bien de là je ne le vois pas, donc si quelqu'un a une idée, je suis preneur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 private Name buildDn(String uid) { DistinguishedName dn = new DistinguishedName(); dn.add("ou", "people"); dn.add("uid", uid); return dn; }
Merci d'avance.
Partager