android 平台使用ldap服务

android sdk并没有提供ldap 的接口,看到网上有封装好的jar包。

ldap sdk下载地址:https://www.unboundid.com/products/ldap-sdk/files/unboundid-ldapsdk-2.3.4-se.zip

将unboundid-ldapsdk-se.jar引用到项目中。

MainAcitvity.java

package com.example.flashcall;

import java.util.LinkedList;

import android.app.Activity;
import android.os.Bundle;

import com.example.flashcall.SearchThread.OnSearchFinishedListener;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Make sure that the filter is valid.

		String filterString = "(cn=test)";
		Filter filter = null;
		try {
			filter = Filter.create(filterString);
		} catch (LDAPException le) {

		}

		// Create a thread to process the search.
		if (filter != null) {
			final SearchThread searchThread = new SearchThread(filter);
			searchThread.setResultListner(new OnSearchFinishedListener() {

				@Override
				public void onFinished(SearchResult result) {
					final int entryCount = result.getEntryCount();

					if (entryCount == 0) {

					} else if (entryCount == 1) {
						SearchResultEntry ret = result.getSearchEntries()
								.get(0);
						ToolUtil.log(ret.toString());

					} else {
						final LinkedList<SearchResultEntry> entries = new LinkedList<SearchResultEntry>(
								result.getSearchEntries());

					}
				}
			});
			searchThread.start();

		} else {

		}

	}
}

 LdapServer.java

package com.example.flashcall;

import com.unboundid.ldap.sdk.LDAPConnection;

public class LdapServer {

	// The port number of the directory server.
	private static final int PORT = 389;

	// The base DN to use when searching the server.
	public static final String BASE_DN = "dc=example,dc=com";

	// The address of the directory server.
	private static final String HOST = "127.0.0.1";

	private static final String BIND_DN = "domain\\username";
	private static final String BIND_PW = "123456";

	private static LDAPConnection conn;

	public static LDAPConnection getConnection() {
		if (conn == null) {
			try {

				conn = new LDAPConnection(HOST, PORT, BIND_DN, BIND_PW);
			} catch (Exception e) {

			}
		}

		return conn;
	}

}

 

SearchThread.java

package com.example.flashcall;

import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPSearchException;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchScope;

final class SearchThread extends Thread {
	/**
	 * The size limit that will be used for searches.
	 */
	static final int SIZE_LIMIT = 100;

	/**
	 * The time limit (in seconds) that will be used for searches.
	 */
	static final int TIME_LIMIT_SECONDS = 30;

	// The filter to use for the search.
	private final Filter filter;

	public OnSearchFinishedListener l;

	public void setResultListner(OnSearchFinishedListener ml) {
		l = ml;
	};

	SearchThread(final Filter filter) {
		ToolUtil.log(filter);

		this.filter = filter;
	}

	@Override()
	public void run() {

		SearchResult result = null;
		LDAPConnection conn = null;
		try {
			conn = LdapServer.getConnection();

			final SearchRequest request = new SearchRequest(LdapServer.BASE_DN,
					SearchScope.SUB, filter);
			request.setSizeLimit(SIZE_LIMIT);
			request.setTimeLimitSeconds(TIME_LIMIT_SECONDS);

			result = conn.search(request);
		} catch (LDAPException lse) {

		} finally {
			if (conn != null) {
				conn.close();
			}
		}

		if (result != null) {

			final ResultCode resultCode = result.getResultCode();
			if (resultCode != ResultCode.SUCCESS) {

			} else {
				if (l != null) {
					l.onFinished(result);
				}
			}
		}
	}

	public interface OnSearchFinishedListener {
		public void onFinished(SearchResult ret);
	}
}

 

 

注意事项:

1、若包引用无效,在“Order and Export”中将jar文件勾上,clean一下。

2、在manifest中添加<uses-permission android:name="android.permission.INTERNET" />权限。

 

参考文档:

https://www.unboundid.com/products/ldapsdk/

 

 

你可能感兴趣的:(android,LDAP)