Guest User

Untitled

a guest
Oct 22nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. package digital_signature;
  2.  
  3. import java.io.IOException;
  4. import java.util.Properties;
  5.  
  6. import javax.naming.Context;
  7. import javax.naming.NamingEnumeration;
  8. import javax.naming.directory.InitialDirContext;
  9. import javax.naming.directory.SearchControls;
  10.  
  11. public class ldap {
  12. public static void main(String[] args) throws IOException, Exception{
  13. String username = "*****";
  14. String password = "######";
  15.  
  16. // public static boolean authenticateJndi(String username, String password) throws Exception{
  17.  
  18. Properties props = new Properties();
  19. props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  20. props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT");
  21. props.put(Context.SECURITY_PRINCIPAL, "uid=adminuser,ou=special users,o=xx.com");//adminuser - User with special priviledge, dn user
  22. props.put(Context.SECURITY_CREDENTIALS, "adminpassword");//dn user password
  23.  
  24.  
  25. InitialDirContext context = new InitialDirContext(props);
  26.  
  27. SearchControls ctrls = new SearchControls();
  28. ctrls.setReturningAttributes(new String[] { "givenName", "sn","memberOf" });
  29. ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  30.  
  31. NamingEnumeration<javax.naming.directory.SearchResult> answers = context.search("o=xx.com", "(uid=" + username + ")", ctrls);
  32. javax.naming.directory.SearchResult result = answers.nextElement();
  33.  
  34. String user = result.getNameInNamespace();
  35.  
  36. try {
  37. props = new Properties();
  38. props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  39. props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT");
  40. props.put(Context.SECURITY_PRINCIPAL, user);
  41. props.put(Context.SECURITY_CREDENTIALS, password);
  42.  
  43. context = new InitialDirContext(props);
  44. } catch (Exception e) {
  45. // return false;
  46. }
  47. // return true;
  48. }
  49.  
  50. }
Add Comment
Please, Sign In to add comment