相信大部份的企業中還是以 Microsoft Exchange Server 2000 或以上版本作為公司的 EMail 服務系統,因為 Exchange Server 2000 可以快速的提供身份認證、郵件服務、行事曆、以及通訊錄服務。本篇以一個簡單的程式碼說明如何使用 Java + JNDI LDAP provider 透過 ActiveDirectory 進行身份認證。
- 其實提供身份認證服務的是 ActiveDirecory 目錄服務系統,和 Exchange Server 是沒有關係的。但是因為在建置 Exchange Server 時會透過 ActiveDirectory 目錄服務記錄使用者相關資訊,才會有 Exchange Server 可以提供身份認證的錯覺。因此以下說明的測試只需 ActiveDirectory 存在即可。

完成認證後,可透過建立的 目錄服務 物件擷取更多的 LDAP 目錄訊息,不過目前還在測試中...
ADAuth.java 源碼列表
package test;import javax.naming.*; import javax.naming.directory.*;
import java.util.Hashtable;
/** * 建立一個透過 ActiveDirectory 認證過的使用者物件 * * @author Ada */ public class ADAuth {
/** * @return 認證是否通過 */ publicstaticboolean login( String email, String password ) {
/** LDAP 環境變數 */ Hashtable env = null;
/** 目錄 */ DirContext ctx = null;
/** 認證狀態 */ boolean logged = true;
env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // 記得修改 ActiveDirectory 實際提供主機位址 env.put( Context.PROVIDER_URL, "ldap://active.directory.server:389"); // 使用 username + password 進行認證 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); // 使用者的完整 email ,也就是 AD 中的 ${sAMAccountName}@your.domain.name env.put( Context.SECURITY_PRINCIPAL, email ); // 認證當時的密碼 env.put( Context.SECURITY_CREDENTIALS, password );
try { // 若可建立目錄物件,即表示完成登入 ctx = new InitialDirContext( env ); logged = true; } catch( AuthenticationException authe ) { // 授權失敗 logged = false; System.out.println( authe ); } catch( Exception e ) { // 不明錯誤 System.out.println( e ); } finally { try { // 記得把目錄關閉 ctx.close(); } catch ( Exception Ignore ) { } }
return logged; }
/** * 主執行程序 * @author Ada */ publicstatic void main( String[] args ) {
// 一定要 email + 密碼才能登入 if( args.length == 2 ) { System.out.println( "is Logon? : " + login( args[0], args[1] ); } else { System.out.println( "EMail/Password not initialed!" ); } } }
另一例:
/*
?*?ADAuth.java
?*
?*?Created?on?2004年9月29日,?上午?11:37
?*/
import?java.util.Hashtable;
import?javax.naming.Context;
import?javax.naming.AuthenticationException;
import?javax.naming.directory.DirContext;
import?javax.naming.directory.InitialDirContext;
/**
?*
?*?@author??ajax
?*/
public?class?ADAuth?{
????DirContext?ctx?=?null;
????Hashtable?env?=?null;
????/**?Creates?a?new?instance?of?ADAuth?*/
????public?ADAuth(String?acct,String?password)?{
????????env?=?new?Hashtable();
????????env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
????????/*
?????????*?Ldap://後可接active?directory的server名稱或ip,port可以不用key,預設為389
?????????*/
????????env.put(Context.PROVIDER_URL,"Ldap://192.168.1.16:389");
????????env.put(Context.SECURITY_AUTHENTICATION,?"simple");
????????/*
?????????*?公司網域為?company.com.tw
?????????*
?????????*?所以有這一段?DC=company,DC=com,DC=tw
?????????*/
????????env.put(Context.SECURITY_PRINCIPAL,"cn="+acct+",cn=users,DC=公司AD的網域名");//,DC=com,DC=tw");
????????env.put(Context.SECURITY_CREDENTIALS,password);
????????try{
????????????ctx?=?new?InitialDirContext(env);
????????????System.out.println("驗証通過");
????????}catch(AuthenticationException?authe){
????????????System.out.println("驗証失敗");
????????}catch(Exception?e){
????????????System.out.println(e);
????????}finally{
????????????try{
????????????????ctx.close();
????????????}catch(Exception?Ignore){}
????????}
?????}
??????public?static?void?main(String[]?args){
??????????new?ADAuth("帳號","密碼");
??????}
}