1
0

LDAPデータをJavaで受信する方法

Last updated at Posted at 2024-06-01

LDAP (Lightweight Directory Access Protocol) のデータをJavaで受信するためには、Java Naming and Directory Interface (JNDI) を利用するのが一般的です。以下に、LDAPからデータを受信する基本的な手順とコード例を示します。

手順
①必要なライブラリを追加する: JNDI APIはJava SEに標準で含まれていますが、LDAPサービスプロバイダを追加する必要があります。javax.namingパッケージを使用します。

②接続設定を行う: LDAPサーバのURL、接続するための認証情報(ユーザーDNとパスワード)を設定します。

LDAP接続を確立する: InitialDirContextを使用して接続を確立します。

検索クエリを実行する: SearchControlsを設定し、検索クエリを実行します。

結果を処理する: 検索結果を受信し、必要なデータを抽出します。

コード例
以下に、LDAPサーバからデータを受信するための基本的なJavaコード例を示します。

java

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import java.util.Hashtable;

public class LDAPExample {
    public static void main(String[] args) {
        /* LDAPサーバの接続情報を設定 */
        String ldapUrl = "ldap://localhost:389";
        String baseDn = "dc=example,dc=com";
        String userDn = "cn=admin,dc=example,dc=com";
        String password = "password";

        /** 接続プロパティを設定 **/
        Hashtable<String, String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapUrl);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, userDn);
        env.put(Context.SECURITY_CREDENTIALS, password);

        try {
            /* LDAP接続を確立 */
            DirContext ctx = new InitialDirContext(env);

            /* 検索クエリと検索コントロールを設定 */
            String searchFilter = "(objectClass=inetOrgPerson)";
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            /* 検索を実行 */
            NamingEnumeration<SearchResult> results = ctx.search(baseDn, searchFilter, searchControls);

            /* 結果を処理 */
            while (results.hasMore()) {
                SearchResult searchResult = results.next();
                Attributes attributes = searchResult.getAttributes();

                /* 必要な属性を取得 */
                Attribute attr = attributes.get("cn");
                if (attr != null) {
                    System.out.println("CN: " + attr.get());
                }

                attr = attributes.get("mail");
                if (attr != null) {
                    System.out.println("Email: " + attr.get());
                }
            }

            /* 接続をクローズ */
            ctx.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

説明
①環境設定 (Hashtable):

  • Context.INITIAL_CONTEXT_FACTORY: LDAPコンテキストファクトリクラスを指定。
  • Context.PROVIDER_URL: LDAPサーバのURLを指定。
  • Context.SECURITY_AUTHENTICATION: 認証方法を指定(ここではシンプル認証)。
  • Context.SECURITY_PRINCIPAL: バインドDNを指定。
  • Context.SECURITY_CREDENTIALS: バインドパスワードを指定。

②検索クエリ (searchFilter):

  • 検索フィルターを指定(例:(objectClass=inetOrgPerson))。

③検索コントロール (SearchControls):

  • 検索スコープを設定(例:サブツリー全体を検索)。

④結果の処理:

  • 検索結果を反復処理し、必要な属性値を取得。

上記のコードを実行すると、指定されたLDAPサーバからユーザーのCN(Common Name)とEmail(メールアドレス)を取得できます。自分の環境に合わせて、LDAPサーバのURL、DN、およびパスワードを変更してください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0