Today i am writing about Microsoft Active Directory 2008 to implement the SSO(Single Sign On).
As know SSO is the hottest these days on internet and manny large companies ( Microsoft,Google etc ) already implement this for their group of application.I think no need to intro of SSO i just start to log my idea here before it will flush from my brain.
we also implementing the SSO for our client which is basically giant school system of USA and they have dozen of applications for students and Administration.They basically want to implement such a system by which users login on application and on their domain system using only single credential so Active Directory is using to store the users information for both the applications and systems.
Now talking from the code side i explorer the java core JNDI technique to authenticate the user and also used my favorite Spring-LDAP Template.so i will show both code.
Lets start from core JAVA JNDI no more explanation just code to remembering
/**
*
* @param sn
* @param password
* @return
*/
public boolean authenticate(String username, String password) {
boolean flag = false;
DirContext authContext =null;
String base = "ou=Students,DC=lti-student,DC=test";
String dn = "cn=" + username;
String ldapURL = "ldap://localhost:389/" + base;
// Setup environment for authenticating
Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, username);//+",ou=Students,DC=lti-student,DC=test"
environment.put(Context.SECURITY_CREDENTIALS, password);
//environment.put(Context.SECURITY_PROTOCOL, "ssl");
try {
authContext = new InitialDirContext(environment);
flag = true;
// user is authenticated
} catch (AuthenticationException ex) {
// Authentication failed
flag = false;
ex.printStackTrace();
} catch (NamingException ex) {
flag = false;
ex.printStackTrace();
}
finally{
try {
authContext.close();
environment.clear();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
i hope you understand whats is this basically code, just taking 2 parameter userDn and password and try to bind with active directory if sucessfully bind return true if fail return false.
ok now moving the Spring-LDAP Template code which is more sophisticated and widely using
public boolean authenticate(String userDn,String password)
{
String base = "ou=Students,DC=lti-student,DC=test";
String ldapURL = "ldap://localhost:389/" + base;
DirContext ctx = null;
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(ldapURL);
contextSource.setUserDn("YOUR_OPTIONAL_ADMIN_USER_DN");
contextSource.setPassword("YOUR_OPTIONAL_ADMIN_PWD");
contextSource.setBase(base);
try {
contextSource.afterPropertiesSet();
}
catch(Exception e) {
e.printStackTrace();
}
try {
ctx = contextSource.getContext(userDn,password);
return true;
} catch (Exception e) {
// Context creation failed - authentication did not succeed
return false;
}
}
simply taking 2 parameter and try to bind if success return true otherwise false
so i show the both technique choice is your which one you would used but i prefer Spring-LDAP template less code and managed API.
I will write the full setup and using of Active Directory 2008 with operation of adding ,deleting,update user code in future InshaALLAH.
Ok thanks enjoy the code.
As know SSO is the hottest these days on internet and manny large companies ( Microsoft,Google etc ) already implement this for their group of application.I think no need to intro of SSO i just start to log my idea here before it will flush from my brain.
we also implementing the SSO for our client which is basically giant school system of USA and they have dozen of applications for students and Administration.They basically want to implement such a system by which users login on application and on their domain system using only single credential so Active Directory is using to store the users information for both the applications and systems.
Now talking from the code side i explorer the java core JNDI technique to authenticate the user and also used my favorite Spring-LDAP Template.so i will show both code.
Lets start from core JAVA JNDI no more explanation just code to remembering
/**
*
* @param sn
* @param password
* @return
*/
public boolean authenticate(String username, String password) {
boolean flag = false;
DirContext authContext =null;
String base = "ou=Students,DC=lti-student,DC=test";
String dn = "cn=" + username;
String ldapURL = "ldap://localhost:389/" + base;
// Setup environment for authenticating
Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, username);//+",ou=Students,DC=lti-student,DC=test"
environment.put(Context.SECURITY_CREDENTIALS, password);
//environment.put(Context.SECURITY_PROTOCOL, "ssl");
try {
authContext = new InitialDirContext(environment);
flag = true;
// user is authenticated
} catch (AuthenticationException ex) {
// Authentication failed
flag = false;
ex.printStackTrace();
} catch (NamingException ex) {
flag = false;
ex.printStackTrace();
}
finally{
try {
authContext.close();
environment.clear();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
i hope you understand whats is this basically code, just taking 2 parameter userDn and password and try to bind with active directory if sucessfully bind return true if fail return false.
ok now moving the Spring-LDAP Template code which is more sophisticated and widely using
public boolean authenticate(String userDn,String password)
{
String base = "ou=Students,DC=lti-student,DC=test";
String ldapURL = "ldap://localhost:389/" + base;
DirContext ctx = null;
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(ldapURL);
contextSource.setUserDn("YOUR_OPTIONAL_ADMIN_USER_DN");
contextSource.setPassword("YOUR_OPTIONAL_ADMIN_PWD");
contextSource.setBase(base);
try {
contextSource.afterPropertiesSet();
}
catch(Exception e) {
e.printStackTrace();
}
try {
ctx = contextSource.getContext(userDn,password);
return true;
} catch (Exception e) {
// Context creation failed - authentication did not succeed
return false;
}
}
simply taking 2 parameter and try to bind if success return true otherwise false
so i show the both technique choice is your which one you would used but i prefer Spring-LDAP template less code and managed API.
I will write the full setup and using of Active Directory 2008 with operation of adding ,deleting,update user code in future InshaALLAH.
Ok thanks enjoy the code.