首页 > 解决方案 > 在 Spring Boot 中从 LDAP 中搜索属性

问题描述

我是 LDAP 新手。我使用 LDAP INITIAL_CONTEXT_FACTORY(“com.sun.jndi.ldap.LdapCtxFactory”)进行了身份验证部分。我所做的代码如下。

    String url = ldap_url;
    String uname = request.getUsername();
    String pwd = request.getPassword();
    boolean authentication = false;
    boolean error = true;
    String msg, attributes, search;
    String ldapDn = String.format("%s%s", searchFilter,uname);

    // create env for initial context
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, ldapDn);
    env.put(Context.SECURITY_CREDENTIALS, pwd);

    try {
        DirContext ctx = new InitialDirContext(env);
        authentication = true;
        error = false;

        ctx.close();

    } catch (Exception e) {

    } finally {

        if (!error) {
            msg = "Login success!!!";
        } else {
            msg = "Authentication failed!";
        }
    }

现在我需要获取与 LDAP 结果一起返回的“employee_id”。我在互联网上浏览了几个例子,但我无法做到。如果有人能建议我一种更好的方法来搜索这个属性,那就太好了。

标签: javaspringspring-bootldap

解决方案


推荐阅读