I need to authenticate a user using the windows ID which is not the same as the "user name". For example: windows ID is jSmith but the user name is "Joe Smith". I need to find Joe Smith based 开发者_运维知识库on the windows ID and then check if he is a member of a certain group.
I'm assuming or hoping that the windows ID is an attribute of the user but I'm new to the LDAP and don't know how to search for a specific attribute in all users.
I am assuming when you say Windows ID for authentication your LDAP is actually Active Directory.
If you just want to find the user by LanId in the Active Directory:
- connecting to the Active using an Admin/service account username and password.
- search for user with following filter:
"(&(objectClass=user)(sAMAccountName=" + searchUsername + "))"
If you want to authenticate the user:
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");// can make it variable, not needed so far
environment.put(Context.SECURITY_AUTHENTICATION, "simple"); // can make it variable, not needed so far
environment.put(Context.PROVIDER_URL, url);
environment.put(Context.SECURITY_PRINCIPAL, username + "@" + domain); // This is specific to AD
environment.put(Context.SECURITY_CREDENTIALS, password);
return new InitialLdapContext(environment, null);
If the above code is executed successfully without exception, that means user provided correct username/password
精彩评论