Je dois vérifier si le nom d'utilisateur et mot de passe donné par l'utilisateur correspondent bien au serveur LDAP. J'utilise deux connexions, dans le premier je récupère dn de l'uid et dans la seconde je me connecte à Ldap avec dn et mot de passe. J'ai un problème avec dn récupéré, il n'a pas les bons champs. Il retourneVérifiez le nom d'utilisateur et mot de passe dans le serveur Ldap par le biais de Java
cn=Lu Ca+sn=Ca+uid=luca+userPassword={SSHA}OiMBVTTZBPqnohYch9\+ISeVv\+5ucgxMR: null:null:No attributes
et non
cn=Lu Ca+sn=Ca+uid=luca+userPassword={SSHA}OiMBVTTZBPqnohYch9\+ISeVv\+5ucgxMR,ou=people,dc=example,dc=com
Comme vous pouvez le voir, et dc ous ne sont pas renvoyés donc ma deuxième requête échoue. Ceci est mon code
@Override
public void isAuthenticated(String username, String password) throws LdapException{
String dn;
Hashtable<String, Object> ldapEnv = new Hashtable<String, Object>();
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, env.getRequiredProperty(PROPERTY_NAME_LDAP_URL));
ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapEnv.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
ldapEnv.put(Context.SECURITY_CREDENTIALS, "secret");
String[] returnAttribute = {"dn"};
DirContext ctx = null;
NamingEnumeration<SearchResult> results = null;
try {
ctx = new InitialDirContext(ldapEnv);
SearchControls controls = new SearchControls();
controls.setReturningAttributes(returnAttribute);
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "uid=" + username ;
results = ctx.search(env.getRequiredProperty(PROPERTY_NAME_LDAP_USERSEARCHBASE), filter, controls);
if (results.hasMore())
dn = results.nextElement().toString();
else throw new LdapException("Wrong username. Please retry!");
} catch (Exception e) {
throw new LdapException(e);
} finally {
try{
if (results != null)
results.close();
if (ctx != null)
ctx.close();
}catch(Exception e){
throw new LdapException(e);
}
}
Hashtable<String, Object> authEnv = new Hashtable<String, Object>();
authEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
authEnv.put(Context.PROVIDER_URL, env.getRequiredProperty(PROPERTY_NAME_LDAP_URL));
authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
authEnv.put(Context.SECURITY_PRINCIPAL, dn);
authEnv.put(Context.SECURITY_CREDENTIALS, password);
try {
new InitialDirContext(authEnv);
} catch (AuthenticationException authEx) {
throw new LdapException("Authentication error. Password was wrong");
} catch(Exception e){
throw new LdapException(e);
}
}
avec ces paramètres
ldap.url=ldap://127.0.0.1:10389/dc=example,dc=com
ldap.userSearchBase=ou=people
J'uing également cette valeur pour l'authentification du printemps mais j'ai une méthode (envoyer gros fichier) qui ne parvient que si j'utilise l'authentification si Je voudrais essayer de m'authentifier avec Java et non pas à travers le printemps Savez-vous pourquoi j'ai ce problème? grâce
MISE À JOUR: avec
dn = results.nextElement().getNameInNamespace();
cela fonctionne, est mes codes robustes?