遍历查询ldap服务器用户
準(zhǔn)備工作:使用openldap搭建server 過程略
名詞
DN = Distinguished Name
DC = Domain Component
OU = Organization Unit
CN = Common Name
RDN = Relative DN
UID = User ID
1.初始化
??? protected static int init() {
?? ??? ?int flag = 0;
?? ??? ?try {
?? ??? ??? ?ldapHost = "192.168.1.1";
?? ??? ??? ?ldapNameAll = "ldap://" + ldapHost;
?? ??? ??? ?ldapPort = 389;
?? ??? ??? ?rootEntry ="dc=sysu,dc=edu,dc=cn";
?? ??? ??? ?rootdn = "uid=rgsam,ou=hosts,dc=sysu,dc=edu,dc=cn";
?? ??? ??? ?rootpw = "111";???? ????
?? ??? ??? ?Hashtable env = new Hashtable();
?? ??? ??? ?env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
?? ??? ??? ?env.put(Context.PROVIDER_URL, ldapNameAll + ":" + ldapPort);
?? ??? ??? ?env.put("com.sun.jndi.ldap.connect.timeout", "3000");
?? ??? ??? ??? ?if (rootdn != null && !rootdn.equals("") && rootpw != null && !rootpw.equals("")) {
?? ??? ??? ??? ??? ?env.put(Context.SECURITY_AUTHENTICATION, "simple");
?? ??? ??? ??? ??? ?env.put(Context.SECURITY_PRINCIPAL, rootdn);
?? ??? ??? ??? ??? ?env.put(Context.SECURITY_CREDENTIALS, rootpw);
?? ??? ??? ??? ?}
?? ??? ??? ?ctx = new InitialDirContext(env);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?flag = -1;
?? ??? ?}
?? ??? ?//返回初始化是否成功的標(biāo)志位
?? ??? ?return flag;
?? ?}
?
2.遍歷查詢
??? protected static int getProcessResultBatch(DirContext ctx,int limit) {
? ??? ??? ?try {
? ??? ??? ??? ?String userObjectClass = "eduPerson";
? ??? ??? ??? ?String userIdAttrName ="uid";
? ??? ??? ??? ?String userPwdAttrName = "userPassword";
? ??? ??? ??? ?String userAccessTimeAttrName ="radiusExpiration";
? ??? ??? ??? ?String ldapFilter = "";
? ??? ??? ??? ?String ldapPassword = null;
? ??? ??? ??? ?String ldapAccessTime = null;
? ??? ??? ??? ?String[] attrList = null;
? ??? ??? ??? ?String rootEntry="dc=sysu,dc=edu,dc=cn";
? ??? ??? ??? ?
? ??? ??? ??? ?String searchFilter = "(&(objectClass=" + userObjectClass +? "))";
? ??? ??? ??? ?//Filter可自定義,一旦定義了Filter則ObjectClass就無效了
? ??? ??? ??? ?if (ldapFilter != null && !ldapFilter.equals("")) {
? ??? ??? ??? ??? ?//searchFilter = StringUtil.str_replace(ldapFilter, "%{User-Name}", userId);
? ??? ??? ??? ?}
? ??? ??? ????
? ??? ??? ??? ??? ?attrList = new String[] {userPwdAttrName, userAccessTimeAttrName};
? ??? ??? ??? ?
? ??? ??? ??? ?String dn = null;
? ??? ??? ??? ?NamingEnumeration ne = null;
? ??? ??? ??? ?try {
? ??? ??? ??? ??? ?SearchControls controls = new SearchControls(SearchControls.SUBTREE_SCOPE, limit, 0, attrList, false, false);
? ??? ??? ??? ??? ?ne = ctx.search(rootEntry, searchFilter, controls);
? ??? ??? ??? ?} catch (Exception e) {
? ??? ??? ??? ??? ?return RESULT_ERR_CONNECT;
? ??? ??? ??? ?}
? ??? ??? ??? ?while(ne.hasMore()) {
? ??? ??? ??? ??? ?SearchResult sr = (SearchResult)ne.next();
? ??? ??? ??? ??? ?Attributes attrs = sr.getAttributes();
? ??? ??? ??? ??? ?Attribute passwordAttr = attrs.get(userPwdAttrName);
? ??? ??? ??? ??? ?ldapPassword = new String((byte[]) passwordAttr.get());
? ??? ??? ??? ??? ?if(ldapPassword == null){
? ??? ??? ??? ??? ??? ?return RESULT_ERR_PASSWORD;
? ??? ??? ??? ??? ?}
? ??? ??? ??? ??? ??? ?Attribute accessTimeAttr = attrs.get(userAccessTimeAttrName);
? ??? ??? ??? ??? ??? ?ldapAccessTime = (String)accessTimeAttr.get();
? ??? ??? ??? ??? ??? ?dn = sr.getNameInNamespace();
? ??? ??? ??? ??? ??? ?System.out.println(dn.toString());
? ??? ??? ??? ?}?? ?
? ??? ??? ?} catch (Exception e) {
? ??? ??? ??? ?return RESULT_ERR_CONNECT;
? ??? ??? ?}
? ??? ??? ?return RESULT_SUCCESS;
? ??? ?}
3.測試程序
????? int ret=init();
??????? if(ret==0){
??????????? System.out.println("LDAP初始化成功");
??????? }else{
?????? ??? ?System.out.println("LDAP初始化失敗");
??????? }
?????? int result=getProcessResultBatch(ctx,100);
?????? if(result==RESULT_SUCCESS){
?????????? System.out.println("RESULT_SUCCESS"); ?? ??? ??? ?
?????? }else if(result==RESULT_ERR_CONNECT){
??? ??? ? // System.out.println("RESULT_ERR_CONNECT");
?????? }else if(result==RESULT_ERR_NOUSER){
??? ??? ?? System.out.println("RESULT_ERR_NOUSER");
?????? }else if(result==RESULT_ERR_PASSWORD){
??? ??? ?? System.out.println("RESULT_ERR_PASSWORD");
?????? }else{
??? ??? ?? System.out.println("RESULT_OTHER");
?????? }
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/archive/2013/01/09/2853191.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的遍历查询ldap服务器用户的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2013年新的开始,每周至少要写一篇博客
- 下一篇: spring定时器分析