summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/auth/Ldap.java
blob: b50bfeaa748318d5f2ecfca5752a8ba4bc9499a9 (plain) (blame)
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
package auth;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.swing.JOptionPane;


public class Ldap {

	public boolean LdapAuth(String user, String pass)
	{
        String base = "ou=hrz,o=fho";
        String dn = "cn="+user + "," + base;
        String ldapURL = "ldaps://fs3.rz.hs-offenburg.de";
        boolean userok=false;
        
        Hashtable<String, String> environment = new Hashtable<String, String>();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        environment.put(Context.PROVIDER_URL, ldapURL);
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, dn);
        environment.put(Context.SECURITY_CREDENTIALS, pass);
        
        try {
            @SuppressWarnings("unused")
			DirContext authContext = new InitialDirContext(environment);
            // user is authenticated
            
            userok=true;

        } catch (NamingException ex) {
            userok=false;
            //user hat keine Zugriffsrechte
            JOptionPane.showMessageDialog(null,
                    ex.toString(), "Message",
                    JOptionPane.INFORMATION_MESSAGE);
            Logger.getLogger(Ldap.class.getName()).log(Level.SEVERE, null, ex);
        }
		return userok;
		
	}

}