我正在使用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);
如果您知道 ldap 服务器中的确切路径 (DN),则可以执行以下操作:
String dn = "uid="+ username +","+rootdn;
Attributes attrs = ctx.getAttributes(dn);
attrs.get("mail"); //gives you the mail attribute