Bonjour,
Je dois faire une classe qui accepte ou refuse la connexion d'utilisateurs qui fournissent leur mot de passe et leur cssDisplayNameDefault, en vérifiant le mot de passe auprès d'un OpenLDAP.
L'algo a mettre en place est donc celui-ci :
- on se connecte à l'OpenLDAP
- on cherche le cn d'après le cssDisplayNameDefault
- on vérifie si la combinaison cn/mot de passe est correcte
Dans une précédente version de la procédure, j'arrivais à faire le 1) et le 3). Néanmoins, l'introduction du 2) me provoque l'erreur suivante sur la ligne qui fait la recherche :
Je précise que je fais du Java depuis un mois
Code : Sélectionner tout - Visualiser dans une fenêtre à part [LDAP: error code 32 - No Such Object];... donc je commence à comprendre à peu près comment ça marche, même s'il reste encore beaucoup de mystères. Je suis totalement nul en LDAP.
Un consultant tiers m'a fourni un bout de code censé faire cela, qu'il a constitué en picorant dans Google, sans le compiler ni le tester. J'ai passé les premières couches de debugging, et j'obtiens ça (je limite aux étapes 1 et 2) :
ça produit ça (le message de la première ligne correspond au catch du try de la ligne en gras) :
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
42
43
44
45
46 class ldaptest2 { public static void main(String[] args) { //user à tester : String my_user = "franck"; String my_password ="franckpwd"; String my_server = "localhost:58089"; String my_ldap ="ldap://" + my_server + "/dc=css,dc=hyperion,dc=com" ; // Set up the environment for creating the initial context Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, my_ldap); env.put(Context.SECURITY_CREDENTIALS, "cn=root,dc=css,dc=hyperion,dc=com"); env.put(Context.SECURITY_PROTOCOL, "password"); try { // Create initial context DirContext ctx = new InitialDirContext(env); // Specify the ids of the attributes to return String[] attrIDs = {"cn"}; // Specify the attributes to match Attributes matchAttrs = new BasicAttributes(true); // ignore case matchAttrs.put(new BasicAttribute("cssDisplayNameDefault", my_user)); // Search for objects that have those matching attributes // l'erreur intervient ici NamingEnumeration answer = ctx.search("ou=People,dc=css,dc=hyperion,dc=com", matchAttrs, attrIDs); // Close the context when we're done ctx.close(); if ( !answer.hasMoreElements() ) { System.out.println("the User does not exist !!!"); answer.close(); System.exit(0); } try { SearchResult sr = (SearchResult)answer.next(); String my_cn=sr.getAttributes().get().get("cn").toString(); answer.close() // Print the answer System.out.println(">>>Name:" + sr.getName()); // ou getID() System.out.println(">>>Value: + my_cn ;
Par ailleurs, j'ai un LDAPExplorer dans lequel j'ai pu valider mes paramètres de connexion...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 Something went wrong! with ctx javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'ou=People,dc=css,dc=hyperion,dc=com' at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3010) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2931) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2737) at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1808) at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1731) at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1723) at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:344) at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:299) at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:281) at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:226) at testldap2.main(testldap2.java:41)
Merci d'avance pour toute piste !
Partager