2

我在 jsp/servlet 中为 Active Directory 创建了一个搜索过滤器。实际上我想从我的 Active Directory 中搜索用户配置文件:

这是我的 Servlet 代码(在 Servlet 的 dopost 中并使用 netscape api):

        int port=0;
        String hostname=request.getParameter("hostname");

        String authenticationId = request.getParameter("authId");
        String authenticationPassword = request.getParameter("authPass");
        String distinguishedName = request.getParameter("dn");

        String searchFilter = request.getParameter("searchFilter");
        String[] attributes = request.getParameterValues("attribute");
        String sn = null,cn=null,mail=null,telephone=null;

        LDAPConnection ldi=new LDAPConnection();

        try {
//          scopeOfSearch = Integer.parseInt(request.getParameter("scope"));
            port = Integer.parseInt(request.getParameter("port"));
            System.out.println("Wait... Identifying the Active Directory / LDAP server.");
            ldi.connect(hostname,port,authenticationId,authenticationPassword);
            System.out.println("LDAP Server "+ hostname +" is connected on port " +port);
            LDAPConnectionInfo.ldapSearchResults = ldi.search(distinguishedName, LDAPConnectionInfo.MY_SCOPE, searchFilter, attributes, false);
            System.out.println("LDAP Search function working.......");


            while (LDAPConnectionInfo.ldapSearchResults.hasMoreElements()) {
                LDAPConnectionInfo.ldapEntry = (LDAPEntry) LDAPConnectionInfo.ldapSearchResults.nextElement();

                try {

                    LDAPConnectionInfo.ldapEntry= LDAPConnectionInfo.ldapSearchResults.next();
                    } 
                catch (LDAPReferralException e) {
                    // TODO: handle exception
                        System.out.println("Search reference: ");
                        LDAPUrl refUrls[]=e.getURLs();
                        for(int i=0;i < refUrls.length; i++){
                            System.out.println("\t" +refUrls[i].getUrl());
                        }
                        continue;
                }
                catch (LDAPException e) {
                    System.out.println("Error: " + e.toString());
                    continue;

            }
                LDAPConnectionInfo.ldapAttributeSet = LDAPConnectionInfo.ldapEntry.getAttributeSet();
                LDAPConnectionInfo.enumAttribute = LDAPConnectionInfo.ldapAttributeSet.getAttributes();

                /* While loop starts get Attribute of Entry */
                while (LDAPConnectionInfo.enumAttribute.hasMoreElements()) {
                    LDAPConnectionInfo.ldapAttribute = (LDAPAttribute) LDAPConnectionInfo.enumAttribute.nextElement();

                    String attributeName = LDAPConnectionInfo.ldapAttribute.getName();
                    if(attributeName.equals("cn")){
                        System.out.println("Full Name : "+cn);
                        request.setAttribute(cn, attributeName.getBytes(cn));
                    }
                    else if (attributeName.equals("sn")) {
                    System.out.println("Surname : ");
                    request.setAttribute(sn, attributeName.getBytes("sn"));
                    }
                    else if (attributeName.equals("mail")) {
                    System.out.println("Email : "); 
                    request.setAttribute(mail, attributeName.getBytes("mail"));
                    }
                    else if (attributeName.equals("telephoneNumber : ")) {
                    System.out.println("Telephone Number : ");  
                    request.setAttribute(telephone, attributeName.getBytes("telephoneNumber"));
                    }

                    Enumeration enumvalues= LDAPConnectionInfo.ldapAttribute.getStringValues();
                    if(enumvalues!=null){
                        while (enumvalues.hasMoreElements()) {
                            String aVal = (String) enumvalues.nextElement();
                            System.out.println("\t" +aVal);

                        }
                    }

                }

            }
            ldi.disconnect();
        }

            catch (NumberFormatException e) {
                // TODO: handle exception
                System.out.println("Error is : "+e.toString());
            }
            catch (LDAPException e) {
                // TODO: handle exception
                System.out.println("Error : "+e.toString());
            }

        getServletContext().getRequestDispatcher("/jsp/searchLDAPResult.jsp").forward(request , response);
        }

而且我遇到了一个例外,例如:Error : netscape.ldap.LDAPException: error result (1); 000004DC: LdapErr: DSID-0C0906DD, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1771

对我Search Filter从 simsys.com 域中搜索任何对象的任何建议。

4

0 回答 0