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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package interfaces;
import beans.Etudiant;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import ldapauthenticate.LdapAuthenticate;
//import javax.naming.directory.SchemaViolationException;
/**
*
* @author Wanki
*/
public class LdapAuthentication implements IAuthenticate {
private static final String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private String base;
private String dn;
private String ldapUrl;
private String securityType;
private Hashtable<String, String> environnement;
/**
*
* @param base qui est la base utilisée par l'annuaire
* @param ldapUrl qui est l'url pour localiser l'annuaire
* @param securityType qui est le mode de sécurité utilisé par ldap (simple,
* SSL, SASL) pour la transaction des informations vers l'annuaire
*/
public LdapAuthentication(String base, String ldapUrl, String securityType) {
this.base = base;
this.ldapUrl = ldapUrl;
this.securityType = securityType;
this.environnement = new Hashtable();
}
/**
*
* @param name cn de l'utilisateur
* @param password mot de passe de l'utilisateur
*/
@Override
public boolean authenticate(String name, String password) {
boolean isDone = false;
this.dn = "cn=" + name + "," + this.base;
this.environnement.put(Context.INITIAL_CONTEXT_FACTORY, this.INITIAL_CONTEXT_FACTORY);
this.environnement.put(Context.PROVIDER_URL, this.ldapUrl);
this.environnement.put(Context.SECURITY_AUTHENTICATION, this.securityType);
this.environnement.put(Context.SECURITY_PRINCIPAL, this.dn);
this.environnement.put(Context.SECURITY_CREDENTIALS, password);
try {
DirContext authContext = new InitialDirContext(this.environnement);
isDone = true;
Etudiant etudiant = new Etudiant("jacque", "secret", 22, "GM");
authContext.bind("cn=Etudiant,dc=mydomain,dc=com", etudiant);
authContext.close();
} catch (AuthenticationException ae) {
Logger.getLogger(LdapAuthenticate.class.getName()).log(Level.SEVERE, null, ae);
} catch (NamingException ex) {
Logger.getLogger(LdapAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
}
return isDone;
}
public static void main(String[] args) {
// TODO code application logic here
LdapAuthentication ldapAuthentication = new LdapAuthentication("dc=mydomain,dc=com", "ldap://ordinateur-pc:389", "simple");
ldapAuthentication.authenticate("Manager", "password");
}} |
Partager