4

認証後にLDAPからDescription、Officeなどのさまざまな値を取得する必要があります。

認証を完了できましたが、他の値を取得できません。

完全なデータを取得するには、どのような名前を使用する必要がありますか?

助けてください。

私のコードは以下の通りです:

    public boolean authenticate(String userid, String pass, String domain) {
        boolean retval = false;
        String searchFilter ="(&(objectClass=user)(" + LDAP_UID_ATTR + "=" + userid + "))";


        try {
            System.out.println("Start: getLDAPAttrs");
            NamingEnumeration answer =
                getLDAPAttrs(userid, pass, searchFilter, domain);
            String uid = "";

            while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult)answer.next();

                Attributes attrs = sr.getAttributes();

                try {
                    uid = attrs.get(LDAP_UID_ATTR).toString();
                    System.out.println("uid: " + uid);
                    System.out.println(attrs.get("mail"));
                    uid = uid.substring(uid.indexOf(':') + 2);
                } catch (Exception err) {
//                    uid = "";
                    System.out.println(err.getMessage());
                    err.printStackTrace();
                }

                // verify userid
                if (userid.equalsIgnoreCase(uid)) {
                    retval = true;

                    break;
                }
            }
        } catch (NamingException ne) {
            System.out.println("In authenticateWithLDAP, LDAP Authentication NamingException : " +
                               ne.getMessage());
        } catch (Exception ex) {
            System.out.println("In authenticateWithLDAP, LDAP Authentication Exception : " +
                               ex.getMessage());
        }

        return retval;
        //        return retval;
    }

    private NamingEnumeration getLDAPAttrs(String userid, String pass,
                                           String searchFilter,
                                           String domain) throws NamingException,
                                                                 Exception {
        String host = getServerName();
        String port = getIP_Port();
        String dcPart1 = getDcPart1();
        String dcPart2 = getDcPart2();
//        String attrUserID = getLDAP_UID_ATTR();
//        String attrUserName = getLDAP_UNAME_ATTR();

        // set attribute names to obtain value of
        String[] returnedAtts = { "sAMAccountName", "cn","mail" };
        SearchControls searchCtls = new SearchControls();
        searchCtls.setReturningAttributes(returnedAtts);

        // specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        // set search base
        String searchBase = "DC=" + dcPart1 + ",DC=" + dcPart2;

        // set ldap env values
        Hashtable environment = new Hashtable();
        environment.put(Context.INITIAL_CONTEXT_FACTORY,
                        "com.sun.jndi.ldap.LdapCtxFactory");
        environment.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, userid + "@" + domain);
        environment.put(Context.SECURITY_CREDENTIALS, pass);

        // set ldap context
        DirContext ctxGC = new InitialDirContext(environment);

        // perform search to obtain values
        NamingEnumeration answer =
            ctxGC.search(searchBase, searchFilter, searchCtls);
        return answer;
    }
4

2 に答える 2

3

LDAPクライアントは、検索要求をサーバーに送信し、サーバーの応答を読み取ることによって、属性値(質問では「フィールド」と呼ばれます)を取得します。検索リクエストは、少なくとも次のコンポーネントで構成されます。

  • ベースDN-検索を開始するオブジェクト。ベースDNより上のオブジェクトは返されません
  • scope-検索のスコープ。これはbase、、、oneまたはsubtree
  • filter-サーバーから返されるエントリを制限するフィルター

さらに、要求された属性のリストを検索要求とともに送信できます。多くのLDAPSDKは、要求された属性リストが提供されていない場合、すべてのユーザー属性を返すだけで、操作属性は返しません。この場合、必要な属性descriptionofficeその他をリクエストしてください。

LDAP準拠のサーバーは、サーバーが特定の属性を返さない原因となる可能性のあるアクセス制御スキームを適用します。LDAP管理者に相談して、LDAPクライアント接続の認証状態に必要な属性にアクセスする権限があるかどうかを確認してください。

も参照してください

  • LDAP:ldapsearchの使用:この記事ではldapsearchコマンドラインツールについて説明していますが、概念はプログラムによるアクセスの場合と同じです。
于 2012-07-05T10:01:07.303 に答える
0

何が悪かったのかわかりました。

返される属性にパラメーターを含める必要がありました。

String[] returnedAtts = { "sAMAccountName", "cn","mail" };

また

String[] returnedAtts = { "sAMAccountName", "cn","mail","description" };

次に、属性を取得するときにその値を使用します。

ありがとう

于 2012-07-06T12:25:28.773 に答える