Skip to content

Instantly share code, notes, and snippets.

@schube
Created June 25, 2022 08:09
Show Gist options
  • Select an option

  • Save schube/1f9a268f1c624c669c5c3016a5b33ef8 to your computer and use it in GitHub Desktop.

Select an option

Save schube/1f9a268f1c624c669c5c3016a5b33ef8 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.ExtendedRequest;
import javax.naming.ldap.ExtendedResponse;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.StartTlsRequest;
import javax.naming.ldap.StartTlsResponse;
public class FMLdap {
// Benutzerangaben, gegebenenfalls aendern
final static String LDAPSERVER = "ldap://ldap.uni-XY.de:389";
final static String ADMIN_NAME = "cn=admin,ou=administratoren,o=universitaet_xy,c=de";
final static String ADMIN_PASSWORD = "geheim";
final static String SEARCH_BASE = "ou=people,o=universitaet_xy,c=de";
static LdapContext ctx;
static StartTlsResponse tls;
public String search(String fmemailadresse) throws Exception {
// JAVA KEYSTRORE FILE ABLAGEORT
System.setProperty("javax.net.ssl.keyStore", "D:\\xypfad\\Datenbank\\keystore.jks");
// System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.keyStorePassword", "schubec");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, LDAPSERVER);
/* Establish LDAP association */
ctx = new InitialLdapContext(env, null);
/* Requesting to start TLS on an LDAP association */
ExtendedRequest tlsRequest = new StartTlsRequest();
ExtendedResponse tlsResponse = ctx.extendedOperation(tlsRequest);
/* Starting TLS */
StartTlsResponse tls1 = (StartTlsResponse) tlsResponse;
tls1.negotiate();
// A TLS/SSL secure channel has been established if you reach here.
/* Assertion of client's authorization Identity -- Explicit way */
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, ADMIN_NAME);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, ADMIN_PASSWORD);
List<String> list = findUsersByEmail(fmemailadresse);
String element = null;
for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
element = iter.next();
System.out.println(element);
}
System.out.println("Fertig.");
ctx.close();
// Stop TLS
tls1.close();
if(element==null){
return "0";
} else {
return element;
}
}
List<String> findUsersByEmail(String mail) throws Exception {
List<String> list = new ArrayList<String>();
// Unsere LDAP Abfrage...
StringBuffer searchFilter = new StringBuffer();
searchFilter.append("(&(objectClass=person)(mail=");
searchFilter.append(mail);
searchFilter.append("))");
SearchControls searchControls = new SearchControls();
List<String> resultAttributes = new ArrayList<String>();
resultAttributes.add("uid");
searchControls.setReturningAttributes(resultAttributes.toArray(new String[0]));
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(SEARCH_BASE, searchFilter.toString(), searchControls);
while (results.hasMoreElements()) {
SearchResult searchResult = results.nextElement();
list.add(searchResult.toString());
}
results.close();
return list;
}
}
def fmldap = new FMLdap();
def y=fmldap.search(fmemailadresse);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment