Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package util;
  7.  
  8. /**
  9. *
  10. * @author izilbert
  11. */
  12. import java.util.*;
  13. import javax.naming.*;
  14. import java.util.regex.*;
  15. import javax.naming.directory.*;
  16.  
  17. public class LdapAuth {
  18. private final static String ldapURI = "ldap://10.56.0.29:389/dc=mpcta,dc=mppr";
  19. private final static String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
  20.  
  21. private static DirContext ldapContext () throws Exception {
  22. Hashtable<String,String> env = new Hashtable <String,String>();
  23. return ldapContext(env);
  24. }
  25.  
  26. private static DirContext ldapContext (Hashtable <String,String>env) throws Exception {
  27. env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
  28. env.put(Context.PROVIDER_URL, ldapURI);
  29. DirContext ctx = new InitialDirContext(env);
  30. return ctx;
  31. }
  32.  
  33. private static String getUid (String user) throws Exception {
  34. DirContext ctx = ldapContext();
  35.  
  36. String filter = "(uid=" + user + ")";
  37. SearchControls ctrl = new SearchControls();
  38. ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
  39. NamingEnumeration answer = ctx.search("", filter, ctrl);
  40.  
  41. String dn;
  42. if (answer.hasMore()) {
  43. SearchResult result = (SearchResult) answer.next();
  44. dn = result.getNameInNamespace();
  45. }
  46. else {
  47. dn = null;
  48. }
  49. answer.close();
  50. return dn;
  51. }
  52.  
  53. private static boolean testBind (String dn, String password) throws Exception {
  54. Hashtable<String,String> env = new Hashtable <String,String>();
  55. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  56. env.put(Context.SECURITY_PRINCIPAL, dn);
  57. env.put(Context.SECURITY_CREDENTIALS, password);
  58.  
  59. try {
  60. ldapContext(env);
  61. }
  62. catch (javax.naming.AuthenticationException e) {
  63. return false;
  64. }
  65. return true;
  66. }
  67.  
  68. public static void main(String args[]) throws Exception {
  69. args = new String[]{"iodsilva","XXXXX"};
  70. if (args.length != 2) {
  71. System.out.println( "missing requried username and password" );
  72. System.exit(1);
  73. }
  74.  
  75. String user = args[0];
  76. String password = args[1];
  77. String dn = getUid( user );
  78.  
  79. if (dn != null) {
  80. /* Found user - test password */
  81. if ( testBind( dn, password ) ) {
  82. System.out.println( "user '" + user + "' authentication succeeded" );
  83. System.exit(0);
  84. }
  85. else {
  86. System.out.println( "user '" + user + "' authentication failed" );
  87. System.exit(1);
  88. }
  89. }
  90. else {
  91. System.out.println( "user '" + user + "' not found" );
  92. System.exit(1);
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement