SHARE
TWEET

Forumsys.com with mhimu.wordpress.com

Bispoysv Oct 2nd, 2015 222 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.HashMap;
  2. import java.util.Hashtable;
  3. import java.util.Map;
  4. import javax.naming.Context;
  5. import javax.naming.NamingEnumeration;
  6. import javax.naming.NamingException;
  7. import javax.naming.directory.Attribute;
  8. import javax.naming.directory.Attributes;
  9. import javax.naming.directory.SearchControls;
  10. import javax.naming.directory.SearchResult;
  11. import javax.naming.ldap.InitialLdapContext;
  12. import javax.naming.ldap.LdapContext;
  13.  
  14. public class ADAuthenticator {
  15.     private String domain;
  16.     private String ldapHost;
  17.     private String searchBase;
  18.  
  19.     public ADAuthenticator() {
  20.         this.domain = "forumsys.com";
  21.         this.ldapHost = "ldap://ldap.forumsys.com:389";
  22.         this.searchBase = "cn=read-only-admin,dc=example,dc=com";
  23.     }
  24.  
  25.     public ADAuthenticator(String domain, String host, String dn) {
  26.         this.domain = domain;
  27.         this.ldapHost = host;
  28.         this.searchBase = dn;
  29.     }
  30.  
  31.     public Map authenticate(String user, String pass) {
  32.  
  33.         String returnedAtts[] = { "sn", "givenName", "mail" };
  34.         String searchFilter = "(&(objectClass=user)(sAMAccountName=" + user + "))";
  35.  
  36.         // Create the search controls
  37.         SearchControls searchCtls = new SearchControls();
  38.         searchCtls.setReturningAttributes(returnedAtts);
  39.  
  40.         // Specify the search scope
  41.         searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  42.  
  43.         Hashtable env = new Hashtable();
  44.         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  45.         env.put(Context.PROVIDER_URL, ldapHost);
  46.         env.put(Context.SECURITY_AUTHENTICATION, "simple");
  47.         env.put(Context.SECURITY_PRINCIPAL, user + "@" + domain);
  48.         env.put(Context.SECURITY_CREDENTIALS, pass);
  49.  
  50.         LdapContext ctxGC = null;
  51.  
  52.         try {
  53.             ctxGC = new InitialLdapContext(env, null);
  54.             // Search objects in GC using filters
  55.             NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls);
  56.             while (answer.hasMoreElements()) {
  57.                 SearchResult sr = (SearchResult) answer.next();
  58.                 Attributes attrs = sr.getAttributes();
  59.                 Map amap = null;
  60.                 if (attrs != null) {
  61.                     amap = new HashMap();
  62.                     NamingEnumeration ne = attrs.getAll();
  63.                     while (ne.hasMore()) {
  64.                         Attribute attr = (Attribute) ne.next();
  65.                         amap.put(attr.getID(), attr.get());
  66.                     }
  67.                     ne.close();
  68.                 }
  69.                 return amap;
  70.             }
  71.         } catch (NamingException ex) {
  72.             ex.printStackTrace();
  73.         }
  74.  
  75.         return null;
  76.  
  77.     }
  78.         //THE TEST
  79.     public static void main(String[] args) {
  80.         ADAuthenticator adAuthenticator = new ADAuthenticator();
  81.  
  82.         adAuthenticator.authenticate("einstein", "password");
  83.  
  84.     }
  85.  
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top