package net.chiangfai.test;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
/**
*
* @author CHIANGFAI
* @description 域认证测试
*
*/
public class DomainAuthenticationTest {
public static void Test(){
String host = "192.168.10.200";//域服务器IP地址
String port = "389";//端口,一般默认389
String domain = "@witsystem.com.cn"; //@+域名
String username = "zhangsan";
String password = "password";
String user = username.indexOf(domain) > 0 ? username : username + domain;
String url = new String("ldap://" + host + ":" + port);
Hashtable evn = new Hashtable();
evn.put(Context.SECURITY_AUTHENTICATION, "simple");
evn.put(Context.SECURITY_PRINCIPAL, user);
evn.put(Context.SECURITY_CREDENTIALS, password);
evn.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
evn.put(Context.PROVIDER_URL, url);
DirContext dirContext = null;
try {
dirContext = new InitialDirContext(evn);
//dirContext.
System.out.println("成功 ...");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("失败...");
e.printStackTrace();
}finally{
if (null!=dirContext) {
try {
dirContext.close();
dirContext = null;
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
DomainAuthenticationTest.Test();
}
}