Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Following example illustrates how to authenticate
the User who has been
registered in Active Directory
Sever by using LDAP.
import
java.util.Hashtable;
import
javax.naming.AuthenticationException;
import
javax.naming.Context;
import
javax.naming.NamingException;
import
javax.naming.directory.DirContext;
import
javax.naming.directory.InitialDirContext;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
public class LDAPHandler {
private static final Log log =
LogFactory.getLog( LDAPHandler.class );
private String
host = System.getProperty(Constants.PROPERTY_ADS_SERVER);
private static int port = 636;
private static boolean isSecure = true;
private static boolean allowSelfSignedCerts;
private static boolean
allowInvalidHostnames;
private String domain =
System.getProperty(Constants.PROPERTY_JCIFS_CLIENT_DOMAIN); //??
or cable
public boolean
autheticate(String username, String password) {
log.debug("Verifying with ADS for the user
login"+username);
Hashtable env =
new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + host +
":" + port);
env.put("com.sun.jndi.ldap.connect.timeout",
"5000");
env.put(Context.SECURITY_AUTHENTICATION,
"simple");
log.debug("Authenticating Cable User " +
username);
env.put(Context.SECURITY_PRINCIPAL, domain +
"\\" + username);
env.put(Context.SECURITY_CREDENTIALS, password);
// Create initial context
try {
DirContext ctx = new InitialDirContext(env);
log.debug("AD Authenticatiion
successful");
return true;
} catch (AuthenticationException ex) {
log.warn("AD Authenticatiion failed for the
user"+username);
return false;
} catch (NamingException ex) {
ex.printStackTrace();
log.warn("Unable to do authetication for the
user"+username);
return false;
}
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.