LDAP: How to authenticate user with connection details

后端 未结 4 1430
梦谈多话
梦谈多话 2020-12-04 23:09

I am not able to authenticate a user using LDAP. I have got following details:

URL=ldap://10.10.10.10:389 
LDAP BASE:DC=lab2,DC=ins 
LDAP Bind Account: CN=Ld         


        
4条回答
  •  盖世英雄少女心
    2020-12-04 23:33

    Try using this, it worked for me

    public static Boolean validateLogin(String userName, String userPassword) {
        Hashtable env = new Hashtable();
    
    
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://" + LDAP_SERVER + ":" + LDAP_SERVER_PORT + "/" + LDAP_BASE_DN);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, userName + "@" + LDAP_SERVER);
        env.put(Context.SECURITY_CREDENTIALS, userPassword);
    
        DirContext ctx;
        try {
           ctx = new InitialDirContext(env); //throw exception, if username-password not correct
           return true;
        } catch (Exception e) {
            return false;
        }
    }
    

提交回复
热议问题