Аутентификация LDAP NULL - PullRequest
0 голосов
/ 09 мая 2019

Я пытаюсь выполнить аутентификацию LDAP с использованием Java, но это всегда возвращает нулевой результат и usrNamespace .Также удалось подтвердить, что имя пользователя и пароль верны.

С именем пользователя, которое я использую cn , я попытался изменить (uid=" + username + ") на cn , но все равно даетмне тот же результат

Если кто-нибудь может мне помочь, это было бы очень признательно.Спасибо!

public class LdapAuthenticationAdapter implements AuthenticationAdapter {

    @Override
    public boolean authenticate(String username, String password) throws Exception {
            Properties prop = new Properties();

            //set the property value
            SECURITY_AUTHENTICATION = prop.getProperty("eq.SECURITY_AUTHENTICATION");
            SECURITY_PRINCIPAL = prop.getProperty("eq.SECURITY_PRINCIPAL");
            SECURITY_CREDENTIALS = prop.getProperty("eq.SECURITY_CREDENTIALS");
            PROVIDER_URL = prop.getProperty("eq.PROVIDER_URL");

        // Get admin user, password(encrypted), host, port and other LDAP parameters 
        // from equationConfiguration.properties
        Hashtable<String, Object> env = new Hashtable<String, Object>();

        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:10389/dc=main,dc=com");
//      env.put("java.naming.ldap.attributes.binary", "objectSID"); // validate this line if applicable

        InitialDirContext context = new InitialDirContext(env);

        SearchControls ctrls = new SearchControls();
        ctrls.setReturningAttributes(new String[] { "givenName", "sn","memberOf" });
        ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        NamingEnumeration<javax.naming.directory.SearchResult> answers = null;
        SearchResult result = null;
        String usrNamespace = null;

        try {           
            answers = context.search("ou=bankfusionusers", "(uid=" + username + ")", ctrls); // Get directory context
            result = answers.nextElement(); 
            usrNamespace = result.getNameInNamespace();

            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            props.put(Context.PROVIDER_URL, "ldap://localhost:10389/dc=main,dc=com");
            props.put(Context.SECURITY_PRINCIPAL, usrNamespace);
            props.put(Context.SECURITY_CREDENTIALS, password);

            System.err.println("Entry 1");

            context = new InitialDirContext(props);

        }catch(NullPointerException e){

            System.err.println("Unsuccessful authenticated bind " + e + "\n");
            return false;
        }

        return true;

    }//end method

}

1 Ответ

0 голосов
/ 16 мая 2019

Я немного изменил ваш код, и он работает на моем.

public static void main(String[] args) throws NamingException {

    Properties initialProperties = new Properties();
    initialProperties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    initialProperties.put(Context.PROVIDER_URL, "ldap://192.168.0.179:389");
    initialProperties.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
    initialProperties.put(Context.SECURITY_CREDENTIALS, "dirmanager");
    initialProperties.put(Context.SECURITY_AUTHENTICATION, "simple");

    InitialDirContext context = new InitialDirContext(initialProperties);

    SearchControls ctrls = new SearchControls();
    ctrls.setReturningAttributes(new String[] { "cn", "sn","givenname" });
    ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration<javax.naming.directory.SearchResult> answers = null;
    SearchResult result = null;
    String usrNamespace = null;


    try {           
        String username = "user.997";      //  I added this, I removed some of your code as well
        answers = context.search("dc=example,dc=com", "(uid=" + username + ")", ctrls); // Get directory context
        result = answers.nextElement(); 
        usrNamespace = result.getNameInNamespace();
        System.out.println("result variable shows : " + result);
        System.out.println("usrNamespace variable shows: " + usrNamespace);

    }catch(NullPointerException e){

        System.err.println("Unsuccessful authenticated bind " + e + "\n");

    }

}
}

В моей консоли я вижу

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...