LDAP.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. import java.util.HashMap;
  7. import javax.naming.Context;
  8. import javax.naming.directory.DirContext;
  9. import javax.naming.ldap.LdapContext;
  10. import java.util.Hashtable;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import javax.naming.NamingEnumeration;
  14. import javax.naming.NamingException;
  15. import javax.naming.directory.Attributes;
  16. import javax.naming.directory.BasicAttribute;
  17. import javax.naming.directory.InitialDirContext;
  18. import javax.naming.directory.ModificationItem;
  19. import javax.naming.directory.SearchControls;
  20. import javax.naming.directory.SearchResult;
  21. /**
  22. *
  23. * @author Deben
  24. */
  25. public class LDAP {
  26. private static DirContext ctx;
  27. private static String userName;
  28. private static String passWord;
  29. LDAP(String username, String password) throws NamingException {
  30. Hashtable<String, String> env = new Hashtable();
  31. env.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory");
  32. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  33. env.put(Context.PROVIDER_URL, "ldap://192.168.2.240:389");
  34. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  35. env.put(Context.SECURITY_PRINCIPAL, "VPN\\Administrator");
  36. env.put(Context.SECURITY_CREDENTIALS, "Magnesium12");
  37. try {
  38. ctx = new InitialDirContext(env);
  39. }
  40. catch(NamingException e) {
  41. int errCode= getErrorCode(e.getMessage());
  42. System.out.println("*** Conection with LDAP failed ("+username+", "+password+") Error: "+errCode+" ***");
  43. }
  44. userName = username;
  45. passWord = password;
  46. }
  47. public int userCheck() throws NamingException {
  48. Hashtable tmpEnv = (Hashtable) ctx.getEnvironment().clone();
  49. tmpEnv.put(Context.SECURITY_PRINCIPAL, userName);
  50. tmpEnv.put(Context.SECURITY_CREDENTIALS, passWord);
  51. try {
  52. new InitialDirContext(tmpEnv);
  53. return 0;
  54. }
  55. catch(NamingException e) {
  56. return getErrorCode(e.toString()) + 700;
  57. }
  58. }
  59. public HashMap getUserInfo(String[] keys) throws NamingException {
  60. SearchControls cons = new SearchControls();
  61. cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
  62. cons.setReturningAttributes(keys);
  63. HashMap<String, String> array = new HashMap();
  64. NamingEnumeration<SearchResult> answer = ctx.search("dc=vpn,dc=local", "sAMAccountName=" + userName, cons);
  65. if(answer.hasMore()) {
  66. Attributes attrs = answer.next().getAttributes();
  67. //System.out.println(attrs.get("serviceType").toString());
  68. //array.put("serviceType", attrs.get("serviceType").toString());
  69. String tmp;
  70. for(String key : keys) {
  71. if(attrs.get(key) != null) {
  72. tmp = attrs.get(key).toString();
  73. array.put(key, tmp.substring(tmp.indexOf(":") + 2));
  74. }
  75. else {
  76. array.put(key, null);
  77. }
  78. }
  79. return array;
  80. }
  81. return null;
  82. }
  83. public boolean writeInfo(String key, String value) {
  84. ModificationItem[] mods = new ModificationItem[1];
  85. String name = "cn="+userName+",cn=Users,dc=vpn,dc=local";
  86. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  87. new BasicAttribute(key, value));
  88. try {
  89. ctx.modifyAttributes(name, mods);
  90. }
  91. catch(NamingException e){
  92. System.out.println(e);
  93. return false;
  94. }
  95. return true;
  96. }
  97. private int getErrorCode(final String exceptionMsg)
  98. {
  99. String pattern="-?\\d+";
  100. Pattern p=Pattern.compile(pattern);
  101. Matcher m=p.matcher(exceptionMsg);
  102. if (m.find()) {
  103. return Integer.valueOf(m.group(0));
  104. }
  105. return -1;
  106. }
  107. }