Authenticating Users with Windows Active Directory from Java
Authenticating Users with Windows Active Directory from Java
Here is a sample code that works with me:
/////////////////////////////////////////////////////////////////////
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
public class Main {
public static void main(String[] args) {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
"LDAP://my.ldap.server:389"); //replace with your server URL/IP
//only DIGEST-MD5 works with our Windows Active Directory
env.put(Context.SECURITY_AUTHENTICATION,
"DIGEST-MD5"); //No other SALS worked with me
env.put(Context.SECURITY_PRINCIPAL,
"user1"); // specify the username ONLY to let Microsoft Happy
env.put(Context.SECURITY_CREDENTIALS, "secret1"); //the password
DirContext ctx = new InitialDirContext(env);
ctx.close();
} catch(NamingException ne) {
System.out.println("Error authenticating user:");
System.out.println(ne.getMessage());
return;
}
//if no exception, the user is already authenticated.
System.out.println("OK, successfully authenticating user");
}
////////////////////////////////////////////////////////////////////
I stripped comments to make the blog shorter.
Resources Helped Me:
Notes:

8 comments:
You save my life :)
This code works fine and it's very easy to use and very intuitive.
Good work and thank you :)
This code is super portable and is just the tool I need for my web app.
Thanx :-)
Hi Nice Blog,
But it doesnt works in my case, I get an error like Error authenticating user:
[LDAP: error code 49 - 8009030C: LdapErr: DSID-0C09043E, comment: AcceptSecurityContext error, data 0, vece]
I got it when I set up my domain in the PROVIDER_URL in other.
Hope you can help me,
Carlos
Hi
It was simple program but powerfull.but i am getting eception like this
"
Error authenticating user:
[LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 525, v1771 "
Could you tell us what was wrong i am trying to figure this out for long time
can u guide me here
@Snake54 and anoymous
The SecurityContext error is because you haven't included the domain in your login id. Usually it's "YourDomain/UserName".
Thank You very Much: this few lines makes me happy.
Mays you also help to get, once authenficationis sucessfull, the group list he user belongs ?
And Again, thanks a lot
Omer AKDENIZ
omer.akdeniz@free.fr
Hi i saw your code authenticate user from active directory.but i need retrive user group from Active directory and take when user login and logout time using java is it possible means sent me some sample code Advance in thanks.....
I don't have any code, sorry.
Post a Comment