抱歉,我在 JNDI 方面是菜鸟,我尝试使用 JNDI 通过简单的身份验证连接到我的 LDAPS,但我不知道连接后如何获取数据,所以我的代码是:
public static void main(String[] args) {
// Set up environment for creating initial context
Hashtable<String, String> env = new Hashtable<String, String>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://myadress:636");
// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "my BASE DN");
env.put(Context.SECURITY_CREDENTIALS, "mypass");
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
之后
DirContext ctx = new InitialDirContext(env);`
我想获取我的树和一些数据,但是如何?..例如我的树是:
-ou=people,dc=info,dc=uni,dc=com
---ou=students
-----uid=5tey37
如何获取 uid 的数据?
对不起,我是菜鸟,对不起我的英语
最佳答案
您可以使用特定参数在上下文
上调用搜索。在您的示例中,您可以根据特定的 uid
进行上下文搜索,并获取与目录对象对应的所有不同的可用属性
。
在下面的示例中,您可能想要调整特定于您的目录的搜索和属性
// Create initial context
DirContext ctx = new InitialDirContext(env);
String searchBase = "ou=people";
SearchControls searchCtls = new SearchControls();
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String uid = "5tey37";
String searchFilter = " (uid=" + uid + ") ";
NamingEnumeration<?> namingEnum = ctx.search(searchBase,searchFilter, searchCtls);
while (namingEnum.hasMore()) {
SearchResult result = (SearchResult) namingEnum.next();
// GET STUFF
Attributes attrs = result.getAttributes();
System.out.println(attrs.get("uid"));
...
}
namingEnum.close();
// Close the context when we're done
ctx.close();
关于java - 如何使用 JNDI 从 ldap 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21475327/