Ldap Active Directory身份验证

时间:2016-12-15 05:06:54

标签: java active-directory ldap jndi

用户使用用户ID登录samaccountname。当我不知道完全区分的名称时,我如何使用ldap通过活动目录验证用户ID,只需samaccountname(JAVA)。

这就是我的代码:

public class LoginClass {

    public static void main(String args[]){
        boolean b= false;
        Scanner scan  = new Scanner(System.in);
        System.out.println("Enter username : ");
        String username = scan.next();
        System.out.println("Enter password : ");
        String password = scan.next();
        Hashtable<String, Object> env = new Hashtable<String, Object>(11);
        env
            .put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:389");

        // Authenticate as S. User and give incorrect password
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL,
            "cn="+username+", cn=Users, dc=server,dc=domain");
        env.put(Context.SECURITY_CREDENTIALS, password);

        try {
            System.out.println(env);
          // Create initial context
          DirContext ctx = new InitialDirContext(env);
          b=true;

          // Close the context when we're done
          ctx.close();
        } catch (NamingException e) {
            b=false;
        }
        finally{
            if(b==true){
                System.out.println("Login Successful");
            }
            else{
                System.out.println("Login Failed");
            }

        }
    }

}

0 个答案:

没有答案