Advertisement
Guest User

Untitled

a guest
Nov 7th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. import com.sun.jndi.ldap.LdapCtxFactory;
  2. import java.util.ArrayList;
  3. import java.util.Hashtable;
  4. import java.util.List;
  5. import java.util.Iterator;
  6. import javax.naming.Context;
  7. import javax.naming.AuthenticationException;
  8. import javax.naming.NamingEnumeration;
  9. import javax.naming.NamingException;
  10. import javax.naming.directory.Attribute;
  11. import javax.naming.directory.Attributes;
  12. import javax.naming.directory.DirContext;
  13. import javax.naming.directory.SearchControls;
  14. import javax.naming.directory.SearchResult;
  15. import static javax.naming.directory.SearchControls.SUBTREE_SCOPE;
  16.  
  17. //import org.acegisecurity.AuthenticationException;
  18. import org.acegisecurity.BadCredentialsException;
  19. import org.acegisecurity.GrantedAuthority;
  20. import org.acegisecurity.GrantedAuthorityImpl;
  21. import org.acegisecurity.providers.AuthenticationProvider;
  22. import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
  23. import org.acegisecurity.providers.dao.AbstractUserDetailsAuthenticationProvider;
  24. import org.acegisecurity.userdetails.UserDetails;
  25. import org.acegisecurity.userdetails.UserDetailsService;
  26. import org.acegisecurity.userdetails.UsernameNotFoundException;
  27.  
  28. class App2 {
  29.  
  30. public static void main(String[] args) {
  31.  
  32. if (args.length != 4 && args.length != 2) {
  33. System.out.println("Purpose: authenticate user against Active Directory and list group membership.");
  34. System.out.println("Usage: App2 <username> <password> <domain> <server>");
  35. System.out.println("Short usage: App2 <username> <password>");
  36. System.out.println("(short usage assumes 'xyz.tld' as domain and 'abc' as server)");
  37. System.exit(1);
  38. }
  39.  
  40. String domainName;
  41. String serverName;
  42.  
  43. if (args.length == 4) {
  44. domainName = args[2];
  45. serverName = args[3];
  46. } else {
  47. domainName = "xyz.tld";
  48. serverName = "abc";
  49. }
  50.  
  51. String username = args[0];
  52. String password = args[1];
  53.  
  54. System.out
  55. .println("Authenticating " + username + "@" + domainName + " through " + serverName + "." + domainName);
  56.  
  57. // bind by using the specified username/password
  58. Hashtable props = new Hashtable();
  59. String principalName = username + "@" + domainName;
  60. props.put(Context.SECURITY_PRINCIPAL, principalName);
  61. props.put(Context.SECURITY_CREDENTIALS, password);
  62. DirContext context;
  63.  
  64. try {
  65. context = LdapCtxFactory.getLdapCtxInstance("ldap://" + serverName + "." + domainName + '/', props);
  66. System.out.println("Authentication succeeded!");
  67.  
  68. // locate this user's record
  69. SearchControls controls = new SearchControls();
  70. controls.setSearchScope(SUBTREE_SCOPE);
  71. NamingEnumeration<SearchResult> renum = context.search(toDC(domainName),
  72. "(& (userPrincipalName=" + principalName + ")(objectClass=user))", controls);
  73. if (!renum.hasMore()) {
  74. System.out.println("Cannot locate user information for " + username);
  75. System.exit(1);
  76. }
  77. SearchResult result = renum.next();
  78.  
  79. List<GrantedAuthority> groups = new ArrayList<GrantedAuthority>();
  80. Attribute memberOf = result.getAttributes().get("memberOf");
  81. if (memberOf != null) {// null if this user belongs to no group at all
  82. for (int i = 0; i < memberOf.size(); i++) {
  83. Attributes atts = context.getAttributes(memberOf.get(i).toString(), new String[] { "CN" });
  84. Attribute att = atts.get("CN");
  85. groups.add(new GrantedAuthorityImpl(att.get().toString()));
  86. }
  87. }
  88.  
  89. context.close();
  90.  
  91. System.out.println();
  92. System.out.println("User belongs to: ");
  93. Iterator ig = groups.iterator();
  94. while (ig.hasNext()) {
  95. System.out.println(" " + ig.next().toString());
  96. }
  97.  
  98. } catch (AuthenticationException a) {
  99. System.out.println("Authentication failed: " + a);
  100. System.exit(1);
  101. } catch (NamingException e) {
  102. System.out.println("Failed to bind to LDAP / get account information: " + e);
  103. System.exit(1);
  104. }
  105. }
  106.  
  107. private static String toDC(String domainName) {
  108. StringBuilder buf = new StringBuilder();
  109. for (String token : domainName.split("\.")) {
  110. if (token.length() == 0)
  111. continue; // defensive check
  112. if (buf.length() > 0)
  113. buf.append(",");
  114. buf.append("DC=").append(token);
  115. }
  116. return buf.toString();
  117. }
  118.  
  119. }
  120.  
  121. LDAPConnection connection = new LDAPConnection( new LDAPJSSEStartTLSFactory() );
  122. connection.connect(hostname, port);
  123. connection.startTLS();
  124. connection.bind(LDAPConnection.LDAP_V3, username+"@"+domain, password.getBytes());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement