[英]LDAP attributes with JNDI.LDAP
I am using LDAP module for log in my webapps. 我正在使用LDAP模块登录我的Web应用程序。 I could check the login successfully with the below code. 我可以使用以下代码成功检查登录名。
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "LDAP://my-company.com:389/ou=people,ou=intranet,dc=company,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid="+ user +",dc=company,dc=com");
env.put(Context.SECURITY_CREDENTIALS, pwd);
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
// Close the context when we're done
b = true;
ctx.close();
}
But I m trying to get attributes from the connection like, Firstname, commonname,mail id etc 但是我试图从连接中获取属性,例如,名字,通用名,邮件ID等
Try this in your try block 在您的try块中尝试
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = { "CommonNames","firstname","givenName","uid","email"};
constraints.setReturningAttributes(attrIDs);
NamingEnumeration answer = ctx.search("", "uid="+user, constraints);
if (answer.hasMore()) {
Attributes attrs = ((SearchResult) answer.next()).getAttributes();
CommonNames= (attrs.get("CommonNames").toString());
System.out.println(CommonNames);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.