Guest User

Untitled

a guest
Apr 20th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package beans;
  2.  
  3. import java.util.*;
  4. import javax.naming.*;
  5. import javax.naming.directory.*;
  6.  
  7. public class TestLdapScript {
  8. public static void main(String[] args) {
  9.  
  10. String userName = "username";
  11. String passWord = "password";
  12.  
  13. try { Hashtable env = new Hashtable();
  14. env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
  15. env.put(Context.PROVIDER_URL,"LDAP://xx.xxx.net:389"); //replace with your server URL/IP
  16. //only DIGEST-MD5 works with our Windows Active Directory
  17. env.put(Context.SECURITY_AUTHENTICATION,"DIGEST-MD5"); //No other SALS worked with me
  18. env.put(Context.SECURITY_PRINCIPAL,userName); // specify the username ONLY to let Microsoft Happy
  19. env.put(Context.SECURITY_CREDENTIALS, passWord); //the password
  20. DirContext ctx = new InitialDirContext(env);
  21. //Create the search controls
  22. SearchControls searchCtls = new SearchControls();
  23.  
  24. //Specify the attributes to return
  25. String returnedAtts[]={"cn","DisplayName"};
  26. searchCtls.setReturningAttributes(returnedAtts);
  27.  
  28. //Specify the search scope
  29. searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  30.  
  31. //specify the LDAP search filter
  32. //String searchFilter = "(&(ObjectClass=Person)(!(ObjectClass=user)))";
  33. //String searchFilter = "(&(objectClass=user)(&(objectClass=Person)(!(userAccountControl=514))))";
  34. String searchFilter = "(&(objectClass=person)(CN=))";
  35.  
  36. //Specify the Base for the search
  37. String searchBase = "DC=xxxx,DC=net";
  38.  
  39. //initialize counter to total the results
  40. int totalResults = 0;
  41.  
  42.  
  43. // Search for objects using the filter
  44. NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
  45.  
  46. //Loop through the search results
  47. while (answer.hasMoreElements()) {
  48. SearchResult sr = (SearchResult)answer.next();
  49.  
  50. totalResults++;
  51.  
  52. System.out.println(">>>" + sr.getName());
  53.  
  54. // Print out some of the attributes, catch the exception if the attributes have no values
  55.  
  56. Attributes attrs = sr.getAttributes();
  57. if (attrs != null) {
  58. try {
  59. System.out.println(" surname: " + attrs.get("cn").get());
  60. System.out.println(" firstname: " + attrs.get("DisplayName").get());
  61.  
  62. }
  63. catch (NullPointerException e) {
  64. System.out.println("Errors listing attributes: " + e);
  65. }
  66. }
  67.  
  68.  
  69. }
  70.  
  71. ctx.close();
  72. } catch(NamingException ne)
  73. { System.out.println("Error authenticating user:");
  74. System.out.println(ne.getMessage());
  75. return;
  76. } //if no exception, the user is already authenticated.
  77. System.out.println("OK, successfully authenticating user");
  78. }
  79. }
Add Comment
Please, Sign In to add comment