java连接微软AD进行用户验证
2010-10-21 21:43:40| 分类: Java |字号 订阅
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
/**
* @author winter
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String userName = "xxxxx"; //用户名称
String password = "xxxxx"; //密码
String host = "xxx.xxx.xxx.xxx"; //AD服务器
String port = "389"; //端口
String domain = "@xxx.com"; //邮箱的后缀名
String url = new String("ldap://" + host + ":" + port);
String user = userName.indexOf(domain) > 0 ? userName : userName + domain;
Hashtable<String, String> env = new Hashtable<String, String>();
DirContext ctx;
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user); //不带邮箱后缀名的话,会报错,具体原因还未探究。高手可以解释分享。
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
try {
ctx = new InitialDirContext(env);
ctx.close();
System.out.println("验证成功!");
} catch (NamingException err) {
err.printStackTrace();
System.out.println("验证失败!");
}
}
}
2010-10-21 21:43:40| 分类: Java |字号 订阅
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
/**
* @author winter
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String userName = "xxxxx"; //用户名称
String password = "xxxxx"; //密码
String host = "xxx.xxx.xxx.xxx"; //AD服务器
String port = "389"; //端口
String domain = "@xxx.com"; //邮箱的后缀名
String url = new String("ldap://" + host + ":" + port);
String user = userName.indexOf(domain) > 0 ? userName : userName + domain;
Hashtable<String, String> env = new Hashtable<String, String>();
DirContext ctx;
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user); //不带邮箱后缀名的话,会报错,具体原因还未探究。高手可以解释分享。
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
try {
ctx = new InitialDirContext(env);
ctx.close();
System.out.println("验证成功!");
} catch (NamingException err) {
err.printStackTrace();
System.out.println("验证失败!");
}
}
}