java - Authentification failed while connecting to ActiveDirectory from a remote host -


i wrote code on ubuntu 16 , tried connect activedirectory on windows server 2012 virtual machine. user name : siwar user password : siwarmp domain name is: squeezer.celtron.com vm host address (windows server 2012) : 192.168.1.115 following code did not work , generated authentification:

package ldap;  import java.util.hashtable;  import javax.naming.authenticationexception; import javax.naming.context; import javax.naming.namenotfoundexception; import javax.naming.namingenumeration; import javax.naming.namingexception; import javax.naming.sizelimitexceededexception; import javax.naming.directory.attribute; import javax.naming.directory.attributes; import javax.naming.directory.dircontext; import javax.naming.directory.initialdircontext; import javax.naming.directory.searchcontrols; import javax.naming.directory.searchresult;  public class ldapmain {     static dircontext ctx = null;     static string userlog = "cn=siwar,ou=users,dc=squeezer,dc=celtron,dc=com";     // static string userlog =     // "cn=siwar,cn=users,dc=squeezer,dc=celtron,dc=com";     static string usermp = "siwarmp";      public static void main(string args[]) throws exception {         hashtable env = new hashtable();          env.put(context.initial_context_factory, "com.sun.jndi.ldap.ldapctxfactory");         env.put(context.provider_url, "ldap://192.168.1.115:389/");         env.put(context.security_authentication, "simple");          env.put(context.security_principal, "cn=admin,dc=squeezer,dc=celtron,dc=com");         env.put(context.security_credentials, "ldap");         searchcontrols controls = new searchcontrols();         controls.setsearchscope(searchcontrols.subtree_scope);          getgroup(env, 500);         getrole(env, "readonly");         validatelogin(env, userlog, usermp);      }      private static searchcontrols getsimplesearchcontrols() {         searchcontrols searchcontrols = new searchcontrols();         searchcontrols.setsearchscope(searchcontrols.subtree_scope);         searchcontrols.settimelimit(30000);         // string[] attrids = {"objectguid"};         // searchcontrols.setreturningattributes(attrids);         return searchcontrols;     }      public static boolean validatelogin(hashtable<string, string> env, string username, string userpassword) {          namingenumeration<searchresult> results = null;          try {             searchcontrols controls = new searchcontrols();             controls.setsearchscope(searchcontrols.subtree_scope);             controls.setcountlimit(1);             controls.settimelimit(5000);              env.put(context.security_principal, username);             env.put(context.security_credentials, userpassword);             ctx = new initialdircontext(env);              results = ctx.search("ou=users,dc=celtron,dc=com", "(objectclass=inetorgperson)",                     getsimplesearchcontrols());              // results = ctx.search("dc=celtron,dc=com",             // "(objectclass=inetorgperson)", getsimplesearchcontrols());             results = ctx.search(username, "(objectclass=*)", getsimplesearchcontrols());             system.out.println(results);             while (results.hasmore()) {                  searchresult result = (searchresult) results.next();                 attributes attrs = result.getattributes();                 attribute dnattr = attrs.get("cn");                 string dn = (string) dnattr.get();                 system.out.println(dn);                  attribute gidattr = attrs.get("gidnumber");                 string gid = (string) gidattr.get();                 system.out.println(gid);                 // user exists, validate password                  env.put(context.security_principal, username);                 env.put(context.security_credentials, userpassword);                  return true;             }             return false;          } catch (authenticationexception e) { // invalid login              return false;         } catch (namenotfoundexception e) { // base context not found.              return false;         } catch (sizelimitexceededexception e) {             throw new runtimeexception("ldap query limit exceeded, adjust query bring less records", e);         } catch (namingexception e) {             throw new runtimeexception(e);         } {             try {                 if (results != null) {                     results.close();                 }                  if (ctx != null) {                     ctx.close();                 }             } catch (exception e) { /* nothing */             }         }     }      public static boolean getrole(hashtable<string, string> env, string rolename) {          namingenumeration<searchresult> results = null;          try {             searchcontrols controls = new searchcontrols();             controls.setsearchscope(searchcontrols.subtree_scope);             controls.setcountlimit(1);             controls.settimelimit(5000);              ctx = new initialdircontext(env);              results = ctx.search("cn=readonly,ou=roles,dc=celtron,dc=com", "(objectclass=organizationalrole)",                     getsimplesearchcontrols());              while (results.hasmore()) {                  searchresult result = (searchresult) results.next();                 attributes attrs = result.getattributes();                 attribute dnattr = attrs.get("roleoccupant");                 string dn = (string) dnattr.get();                 system.out.println(dn);                 return true;             }             return false;          } catch (authenticationexception e) { // invalid login             system.out.println("auth failed");             return false;         } catch (namenotfoundexception e) { // base context not found.              return false;         } catch (sizelimitexceededexception e) {             throw new runtimeexception("ldap query limit exceeded, adjust query bring less records", e);         } catch (namingexception e) {             throw new runtimeexception(e);         } {             try {                 if (results != null) {                     results.close();                 }                  if (ctx != null) {                     ctx.close();                 }             } catch (exception e) {             }         }     }      public static string getgroup(hashtable<string, string> env, int gid) {          namingenumeration<searchresult> results = null;          try {             searchcontrols controls = new searchcontrols();             controls.setsearchscope(searchcontrols.subtree_scope);             controls.setcountlimit(1);             controls.settimelimit(5000);              ctx = new initialdircontext(env);              results = ctx.search("ou=groups,dc=celtron,dc=com", "(gidnumber=500)", getsimplesearchcontrols());              while (results.hasmore()) {                  searchresult result = (searchresult) results.next();                 attributes attrs = result.getattributes();                 attribute dnattr = attrs.get("cn");                 string dn = (string) dnattr.get();                 system.out.println(dn);                  return dn;             }             return "";          } catch (authenticationexception e) {              return "";         } catch (namenotfoundexception e) {              return "";         } catch (sizelimitexceededexception e) {             throw new runtimeexception("ldap query limit exceeded, adjust query bring less records", e);         } catch (namingexception e) {             throw new runtimeexception(e);         } {             try {                 if (results != null) {                     results.close();                 }                  if (ctx != null) {                     ctx.close();                 }             } catch (exception e) {             }         }     } } 


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -