Java 如何检索;windows active directory-属性id';s";在爪哇?
为了得到我问题的答案,我找了很多。但我不能 我在搜索中得到的信息:Java 如何检索;windows active directory-属性id';s";在爪哇?,java,active-directory,ldap,jndi,unboundid-ldap-sdk,Java,Active Directory,Ldap,Jndi,Unboundid Ldap Sdk,为了得到我问题的答案,我找了很多。但我不能 我在搜索中得到的信息: public class RetrieveUserAttributes { public static void main(String[] args) { RetrieveUserAttributes retrieveUserAttributes = new RetrieveUserAttributes(); retrieveUserAttributes.getUserBasicAttributes
public class RetrieveUserAttributes {
public static void main(String[] args) {
RetrieveUserAttributes retrieveUserAttributes = new RetrieveUserAttributes();
retrieveUserAttributes.getUserBasicAttributes("anand", retrieveUserAttributes.getLdapContext());
}
public LdapContext getLdapContext(){
LdapContext ctx = null;
try{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_PRINCIPAL, "anand@tstdmn.com");
env.put(Context.SECURITY_CREDENTIALS, "password@123");
env.put(Context.PROVIDER_URL, "ldap://192.168.100.182:389");
ctx = new InitialLdapContext(env, null);
System.out.println("Connection Successful.");
}catch(NamingException nex){
System.out.println("LDAP Connection: FAILED");
}
return ctx;
}
private void getUserBasicAttributes(String username, LdapContext ctx) {
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(objectClass=person)";
String[] attrIDs = { "distinguishedName","sn","givenname","mail", "telephonenumber","lockoutThreshold", "lockoutDuration", "minPwdAge","maxPwdAge", "minPwdLength","pwdLastSet"};
constraints.setReturningAttributes(attrIDs);
NamingEnumeration answer = ctx.search(searchBase, searchFilter, constraints);
if (answer.hasMore()) {
Attributes attrs = ((SearchResult) answer.next()).getAttributes();
for(String obj : attrIDs){
System.out.println(obj+" : "+ attrs.get(obj));
}
}else{
throw new Exception("Invalid User");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
公共类RetrieveUserAttributes{
公共静态void main(字符串[]args){
RetrieveUserAttributes RetrieveUserAttributes=新的RetrieveUserAttributes();
getUserBasicAttribute(“anand”,retrieveUserAttributes.getLdapContext());
}
公共LdapContext getLdapContext(){
LdapContext ctx=null;
试一试{
Hashtable env=新的Hashtable();
环境放置(Context.INITIAL\u Context\u工厂,
“com.sun.jndi.ldap.LdapCtxFactory”);
环境put(Context.SECURITY_PRINCIPAL,“anand@tstdmn.com");
环境放置(Context.SECURITY\u凭据,“password@123");
env.put(Context.PROVIDER\u URL,“ldap://192.168.100.182:389");
ctx=新的InitialLdapContext(env,null);
System.out.println(“连接成功”);
}捕获(NamingException nex){
System.out.println(“LDAP连接:失败”);
}
返回ctx;
}
私有void getUserBasicAttribute(字符串用户名,LdapContext ctx){
试一试{
SearchControls约束=新的SearchControls();
约束.setSearchScope(SearchControls.SUBTREE_范围);
字符串searchFilter=“(objectClass=person)”;
字符串[]attrIDs={“DifferentiedName”、“sn”、“givenname”、“mail”、“telephonenumber”、“lockoutThreshold”、“lockoutDuration”、“minPwdAge”、“maxPwdAge”、“minPwdLength”、“pwdLastSet”};
约束。设置ReturningAttribute(属性ID);
NamingEnumeration answer=ctx.search(searchBase、searchFilter、constraints);
if(answer.hasMore()){
Attributes attrs=((SearchResult)answer.next()).getAttributes();
用于(字符串对象:属性){
System.out.println(obj+“:”+attrs.get(obj));
}
}否则{
抛出新异常(“无效用户”);
}
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
我在这里给出的键完全是静态的
我需要在下面的“广告”图片中动态检索“常规、帐户、地址”选项卡中的所有属性
希望我能得到一个好的解决方案。我们已经识别了LDAP中显示的属性: -吉姆