Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. String userName = "John P R-Asst General Manager";
  2. String passWord = "asdfgh123";
  3. String base ="OU=SOU,DC=example,DC=com";
  4. String dn = "cn=" + userName + "," + base;
  5.  
  6. String ldapURL = "ldap://mdsdc3.example.com:389";
  7. authEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
  8. authEnv.put(Context.PROVIDER_URL, ldapURL);
  9. authEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
  10. authEnv.put(Context.SECURITY_PRINCIPAL, dn);
  11. authEnv.put(Context.SECURITY_CREDENTIALS, password);
  12.  
  13. try {
  14. DirContext authContext = new InitialDirContext(authEnv);
  15. return true;
  16.  
  17. } catch (NamingException namEx) {
  18. return false;
  19. }
  20.  
  21. try
  22. {
  23. // Set up the environment for creating the initial context
  24. Hashtable<String, String> env = new Hashtable<String, String>();
  25. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  26. env.put(Context.PROVIDER_URL, "ldap://ldap_server:389");
  27. //
  28. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  29. env.put(Context.SECURITY_PRINCIPAL, "domain\user"); //we have 2 \ because it's a escape char
  30. env.put(Context.SECURITY_CREDENTIALS, "test");
  31.  
  32. // Create the initial context
  33.  
  34. DirContext ctx = new InitialDirContext(env);
  35. boolean result = ctx != null;
  36.  
  37. if(ctx != null)
  38. ctx.close();
  39.  
  40. return result;
  41. }
  42. catch (Exception e)
  43. {
  44. return false;
  45. }
  46.  
  47. String ldapURL = "ldap://mdsdc3.example.com:389/DC=example,DC=com";
  48.  
  49. env.put(Context.SECURITY_PRINCIPAL, "Zeus");
  50. env.put(Context.SECURITY_CREDENTIALS, "Z3usP@55");
  51. env.put(Context.PROVIDER_URL, "ldap://athena.local:389");
  52. String deecee = "DC=athena,DC=local";
  53.  
  54. LdapContext context = new InitialLdapContext(env, null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement