检索登录的LDAP用户inJava的属性

时间:2016-01-26 04:22:12

标签: java ldap

我正在使用LDAP对用户进行身份验证。之后我想检索邮件,用户名和其他属性。有没有办法检索而不进行搜索。

Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://url");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "distinguished_name");
    env.put(Context.SECURITY_CREDENTIALS, "password");
    DirContext ctx = new InitialDirContext(env);

1 个答案:

答案 0 :(得分:0)

如果您知道ldap服务器中的确切路径(DN),那么您可以执行以下操作:

String dn = "uid="+ username +","+rootdn;
Attributes attrs = ctx.getAttributes(dn);
attrs.get("mail"); //gives you the mail attribute