大家好,又见面了,我是你们的朋友全栈君。
我正在构建一个基于GXT(J2EE)的应用程序.现在的问题是我必须将应用程序连接到LDAP服务器.您能告诉我如何从我们的Java应用程序连接LDAP服务器以及我将使用哪些库或API?
解决方法:
要连接到LDAP,请查看以下包/类:
javax.naming.directory.*
javax.naming.ladp.*
com.sun.jndi.ldap.LdapCtxFactory
com.sun.jndi.ldap.ControlFactory
示例代码:
//build a hashtable containing all the necessary configuration parameters
Hashtable environment = new Hashtable();
environment.put(LdapContext.CONTROL_FACTORIES, conf.getProperty(“ldap.factories.control”));
environment.put(Context.INITIAL_CONTEXT_FACTORY, conf.getProperty(“ldap.factories.initctx”));
environment.put(Context.PROVIDER_URL, conf.getProperty(“ldap.host”));
environment.put(Context.SECURITY_AUTHENTICATION, “simple”);
environment.put(Context.SECURITY_PRINCIPAL, conf.getProperty(“ldap.user”));
environment.put(Context.SECURITY_CREDENTIALS, conf.getProperty(“ldap.password”));
environment.put(Context.STATE_FACTORIES, “PersonStateFactory”);
environment.put(Context.OBJECT_FACTORIES, “PersonObjectFactory”);
// connect to LDAP
DirContext ctx = new InitialDirContext(environment);
// Specify the search filter
String FILTER = “(&(objectClass=Person) ((sAMAccountName=” + user.getUsername() + “)))”;
// limit returned attributes to those we care about
String[] attrIDs = { “sn”, “givenName” };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Search for objects using filter and controls
NamingEnumeration answer = ctx.search(searchBase, FILTER, ctls);
…
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
surName = attrs.get(“sn”).toString();
givenName = attrs.get(“givenName”).toString();
…
在这个例子中,我有一个Configuration对象,它从配置文件中读取这些值.
价值观将是:
# LDAP parameters
ldap.host = ldap://ldap.mydomain.com:389
ldap.factories.initctx = com.sun.jndi.ldap.LdapCtxFactory
ldap.factories.control = com.sun.jndi.ldap.ControlFactory
ldap.searchbase = dc=mydomain,dc=us
ldap.user = MYDOMAIN.COM\\ldap-user
ldap.userBase= MYDOMAIN.COM\\
ldap.password = ******
标签:gxt,java,ldap
来源: https://codeday.me/bug/20191004/1853865.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/146696.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...