IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

recupérer des données ldap avec spring.


Sujet :

Spring Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    63
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 63
    Points : 57
    Points
    57
    Par défaut recupérer des données ldap avec spring.
    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 :
    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 ressource :
    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 ressourceDao :
    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());
    	}
    }
    -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
     
     
    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());
                    }
    	}
    }
    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
     
    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)
    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
     
            private Name buildDn(String uid) {
    		DistinguishedName dn = new DistinguishedName();
    		dn.add("ou", "people");
    		dn.add("uid", uid);
    		return dn;
    	}
    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.

    Merci d'avance.

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    63
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 63
    Points : 57
    Points
    57
    Par défaut
    J'ai finalement réussi à trouver d’où venait le problème. Mais je me demandai si il était possible de passer ce code en un version spring security (Juste à ajouter des tests pour bloquer l'accès à certaines pages ??) ou si il faut tout recommencer ??

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MVC] recupération des données avec ajax
    Par hedibox dans le forum Spring Web
    Réponses: 1
    Dernier message: 20/05/2014, 12h21
  2. Réponses: 2
    Dernier message: 17/01/2014, 10h44
  3. Réponses: 8
    Dernier message: 05/02/2007, 13h28
  4. [TComPort] Analyse des données reçues avec ReadStr
    Par chourmo dans le forum Langage
    Réponses: 4
    Dernier message: 22/06/2005, 14h12
  5. Réponses: 2
    Dernier message: 20/01/2005, 15h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo