java+LDAP

环境搭建:拷贝novell ldap中ldap.jar,Novel_LDAP.jar ,utilities.jar到项目环境中即可。

程序代码:

  public java.util.List getInfoSearch( java.lang.String name )
  {
    //@@begin getInfoSearch()
 //wdComponentAPI.getMessageManager().reportWarning("以下为查询关键字/'"+name+"/'结果:");
    List resultList =new ArrayList();
    //HashMap map=null;
 String ldapHost = "10.209.4.165";
  int ldapPort = 389;
  String loginDN = "cn=admin,ou=users,ou=nw,o=sgcc";
  String password = "nwpn1234";
  //String[] mechanisms = { "DIGEST-MD5" };
  int ldapVersion = LDAPConnection.LDAP_V3;

  int searchScope = LDAPConnection.SCOPE_ONE;
  String searchBase = "ou=users,ou=nw,o=sgcc";
  //String searchFilter = "(givenName=*)&(fullName=user*)";
  String searchFilter = "(fullName="+name+"*)";
  LDAPConnection lc = new LDAPConnection();
  System.out.println(searchFilter);
  
  try {
   // connect to the server
   lc.connect(ldapHost, ldapPort);
   // bind to the server
   try {
    lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
    
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   System.out.println("Digest-MD5 Sasl bind was successful.");
   //lc.disconnect();
  } catch (LDAPException e) {
   System.err.println("MD5Bind example failed");
   System.err.println("Error: " + e.toString());
  }
  //search ldap
  try {
   LDAPSearchResults searchResults = lc.search(searchBase,
     searchScope, searchFilter, null, // return all attributes
     false);
   while (searchResults.hasMore()) {
    LDAPEntry nextEntry = null;
    try {
     nextEntry = searchResults.next();
    } catch (LDAPException e) {
     System.out.println("Error: " + e.toString());
    }
    LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
    Iterator allAttributes = attributeSet.iterator();
    HashMap map=new HashMap();
    //List resultList=new ArrayList();
    while (allAttributes.hasNext()) {
     LDAPAttribute attribute = (LDAPAttribute) allAttributes
       .next();
     String attributeName = attribute.getName();
     
     if(attributeName.equals("fullName")){
      //System.out.println("    " + attributeName);
      Enumeration allValues = attribute.getStringValues();
      if (allValues != null) {
       while (allValues.hasMoreElements()) {
        String Value = (String) allValues.nextElement();
        if (Base64.isLDIFSafe(Value)) {
         //System.out.println("      " + Value);
         map.put(attributeName,Value);
        } else {
         // base64 encode and then print out
         Value = Base64.encode(Value.getBytes());
         map.put(attributeName,Value);
         //System.out.println("      " + Value);
        }
       }
      }
     }     
    }
    resultList.add(map);
   }
  } catch (Exception ex) {
   wdComponentAPI.getMessageManager().reportWarning("search Error:" + ex.toString());
  }
    return resultList;
    //@@end
  }

注:若是web dynpro调用LDAP的话,在controller中创建一个方法(假如为A),将上述代码拷贝到A中即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值