Created
June 13, 2013 07:45
-
-
Save mikamboo/5771914 to your computer and use it in GitHub Desktop.
LDAP Connect Java (2010)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.Hashtable; | |
import javax.naming.*; | |
import javax.naming.directory.*; | |
import javax.swing.*; | |
public class LDAP_connect extends JDialog implements ActionListener { | |
private static final String serverLdap="ldap://ist-guizay.univ-st-etienne.fr:389"; | |
private String Profil; | |
private JDialog ConnectDlg; | |
private String login, password; | |
private JButton ok, cancel; | |
private JTextField log, pwd; | |
// Constructeur de LDAP_connect | |
public LDAP_connect(){ | |
ConnectDlg = new JDialog(getFrame(), "Autentification"); | |
this.ConnectDlg.setSize(250,150); | |
this.ConnectDlg.setLocationRelativeTo(null); | |
ok = new JButton("Valider"); | |
cancel = new JButton("Annuler"); | |
pwd = new JTextField("password"); | |
log = new JTextField("login"); | |
ConnectDlg.setLayout(new BorderLayout()); | |
/* | |
* On place les boutons dans un JPanel | |
* */ | |
JPanel panelboutons = new JPanel(); | |
panelboutons.setLayout(new FlowLayout()); | |
panelboutons.setPreferredSize(new Dimension(350,50)); | |
panelboutons.add(ok); | |
panelboutons.add(cancel); | |
/* | |
* On déclare un panel pour les champs de saisie | |
* Et un panel globale qui contiendra panelsaisie + panelboutons | |
* */ | |
JPanel panelsaisie = new JPanel(); | |
JPanel global = new JPanel(); | |
panelsaisie.setBorder(BorderFactory.createEmptyBorder(15,25,15,15)); | |
panelsaisie.setLayout(new GridLayout(3,2)); | |
panelsaisie.add(new JLabel("Login")); | |
panelsaisie.add(log); | |
panelsaisie.add(new JPanel()); | |
panelsaisie.add(new JPanel()); | |
panelsaisie.add(new JLabel("Mot de Pass")); | |
panelsaisie.add(pwd); | |
global.setLayout(new BorderLayout()); | |
global.setBorder(BorderFactory.createTitledBorder("Saisie des informations")); | |
global.add(panelsaisie, BorderLayout.CENTER); | |
global.add(panelboutons, BorderLayout.SOUTH); | |
Container c = ConnectDlg.getContentPane(); | |
c.setLayout(new BorderLayout()); | |
c.add(global, BorderLayout.CENTER); | |
/* | |
* On Place les écouteurs d'évènements associés aux boutons. | |
*/ | |
cancel.addActionListener(this); | |
ok.addActionListener(this); | |
this.ConnectDlg.pack(); | |
this.ConnectDlg.setVisible(true); | |
//this.ConnectDlg.setLocation(250,250); | |
} | |
//----------------------------------------------------------------- | |
/* récupération de la frame de l'applet | |
* | |
*/ | |
public Frame getFrame() { | |
Component component=this; | |
do{component=component.getParent();} | |
while(!(component instanceof Frame)); | |
return (Frame)component; | |
} | |
/* | |
* Méthodes de récupération des Champs "Login" et "Mot de Pass" | |
*/ | |
public String getLogin(){ return this.log.getText(); } | |
public String getPassword(){ return this.pwd.getText(); } | |
//----------------------------------------------------------------- | |
/* | |
* Connection auserveur LDAP | |
*/ | |
@SuppressWarnings("unchecked") | |
boolean verifiePassword() { | |
try | |
{ | |
Hashtable env = new Hashtable(); | |
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); | |
env.put(Context.PROVIDER_URL, serverLdap); | |
env.put(Context.SECURITY_AUTHENTICATION,"simple"); | |
String cont="uid=login, ou=person, o=istase,c=fr"; // login | |
env.put(Context.SECURITY_PRINCIPAL,cont); | |
env.put(Context.SECURITY_CREDENTIALS, "password"); // password | |
DirContext ctx=new InitialDirContext(env);//Tentative de login sur le serveur LDAP | |
cont=cont.replaceAll(" ",""); | |
//Verification du groupe | |
String[] attrIDs = {"uniquemember"}; | |
SearchControls ctls = new SearchControls(); | |
ctls.setReturningAttributes(attrIDs); | |
String token = "cn=direction,ou=groupes,o=istase,c=fr"; | |
Attributes answer = ctx.getAttributes(token, attrIDs); | |
for (NamingEnumeration ae = answer.getAll();ae.hasMore(); ) | |
{ | |
Attribute attr = (Attribute) ae.next(); | |
for (int i = 0; i < attr.size(); i++) | |
{ | |
String temp=attr.get(i).toString().replaceAll(" ",""); | |
System.out.println(cont+"/"+temp); | |
if (cont.equals(temp)) {Profil="Direction";return true;} | |
} | |
} | |
token = "cn=groupeAdministration,ou=groupes,o=istase,c=fr"; | |
answer = ctx.getAttributes(token, attrIDs); | |
for (NamingEnumeration ae = answer.getAll();ae.hasMore(); ) | |
{ | |
Attribute attr = (Attribute) ae.next(); | |
for (int i = 0; i < attr.size(); i++) | |
{ | |
String temp=attr.get(i).toString().replaceAll(" ",""); | |
if (cont.equals(temp)) {Profil="Secretaire";return true;} | |
} | |
} | |
return false; | |
} | |
catch (NamingException e) | |
{ | |
JOptionPane.showMessageDialog(null,e,"Login LDAP", JOptionPane.WARNING_MESSAGE); | |
return false; | |
} | |
} | |
//----------------------------------------------------------------- | |
public static void main(String[] args) { | |
LDAP_connect ldap = new LDAP_connect(); | |
} | |
//----------------------------------------------------------------- | |
/* | |
* Actions associées aux évènements de clics sur les boutons | |
*/ | |
public void actionPerformed(ActionEvent e) { | |
//Actions Bouton "Valider" | |
this.ConnectDlg.pack(); | |
if(e.getSource()==ok){ | |
this.login = this.getLogin(); | |
this.password = this.getPassword(); | |
boolean c = this.verifiePassword(); | |
if(c == true) | |
System.out.println("Connexion Réussie"); | |
else | |
System.out.println("La Connexion à Echouée"); | |
ConnectDlg.dispose(); | |
} | |
//Action Bouton "Annuler" | |
else if(e.getSource()==cancel){ | |
//----- | |
JOptionPane.showConfirmDialog(getFrame()," Abandon ?", | |
"Login LDAP", JOptionPane.YES_NO_OPTION); | |
ConnectDlg.dispose(); // Destruction de la boite de dialogue | |
} | |
} | |
//----------------------------------------------------------------- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment