java 调用ActiveDirectory,使用标准的LDAP协议
2008-05-01 06:24
411 查看
在ActiveDirectory中 支持 LDAP 协议,我们可以在 java 中使用标准的 java JNDI API 来访问它。LDAP 服务器并非真的必须支持 JNDI API,只要支持 LDAP 协议就可以了。我们已经提供了一个简单的测试案例程序来认证一个 LDAP 服务器的识别名。一般情况下,对待 ActiveDirectory 不必与对待任何其他的 LDAP 服务器有什么不同。
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;
//include the JNDI in the classpath. You should use the same JDK used by WebSphere Application server.
class wasLDAPAuth
{
public static void main(String[] args)
{
//***************** user information to be authenticated ********************************
//*****************Please modify the following three properties accordingly ************
String LDAPHost= "LDAP://cliang1.austin.ibm.com:389"; //LDAP host + port number
String DN = "cn=user1, ou=Austin,o=ibm,c=us"; // DN to be authenticated
String password = "security"; // DN's password
//***************** End of user information
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.LDAP.LDAPCtxFactory");
//for websphere 4.0 and 5.0
//props.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.jndi.LDAPCtxFactory");
// for WebSphere 3.5 release
props.put(Context.SECURITY_AUTHENTICATION, "simple"); //use simple authentication mechanism
props.put(Context.SECURITY_CREDENTIALS, password);
props.put(Context.SECURITY_PRINCIPAL, DN);
props.put(Context.PROVIDER_URL, LDAPHost);
long start = System.currentTimeMillis();
long end=0;
long time =0;
try
{
System.out.println("authenticating");
DirContext ctx = new InitialDirContext(props);
System.out.println("authenticated");
end = System.currentTimeMillis();
time = end - start;
System.out.println( "authentication takes = " + time + " millis");
System.out.println("successfully authenticate DN: "+DN);
}
catch (Exception ex)
{
end = System.currentTimeMillis();
time = end - start;
System.out.println("Exception is "+ex.toString());
ex.printStackTrace();
System.out.println( "authentication takes = " + time + " millis");
System.out.println("fail to authenticate DN: "+DN);
}
}
}
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;
//include the JNDI in the classpath. You should use the same JDK used by WebSphere Application server.
class wasLDAPAuth
{
public static void main(String[] args)
{
//***************** user information to be authenticated ********************************
//*****************Please modify the following three properties accordingly ************
String LDAPHost= "LDAP://cliang1.austin.ibm.com:389"; //LDAP host + port number
String DN = "cn=user1, ou=Austin,o=ibm,c=us"; // DN to be authenticated
String password = "security"; // DN's password
//***************** End of user information
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.LDAP.LDAPCtxFactory");
//for websphere 4.0 and 5.0
//props.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.jndi.LDAPCtxFactory");
// for WebSphere 3.5 release
props.put(Context.SECURITY_AUTHENTICATION, "simple"); //use simple authentication mechanism
props.put(Context.SECURITY_CREDENTIALS, password);
props.put(Context.SECURITY_PRINCIPAL, DN);
props.put(Context.PROVIDER_URL, LDAPHost);
long start = System.currentTimeMillis();
long end=0;
long time =0;
try
{
System.out.println("authenticating");
DirContext ctx = new InitialDirContext(props);
System.out.println("authenticated");
end = System.currentTimeMillis();
time = end - start;
System.out.println( "authentication takes = " + time + " millis");
System.out.println("successfully authenticate DN: "+DN);
}
catch (Exception ex)
{
end = System.currentTimeMillis();
time = end - start;
System.out.println("Exception is "+ex.toString());
ex.printStackTrace();
System.out.println( "authentication takes = " + time + " millis");
System.out.println("fail to authenticate DN: "+DN);
}
}
}
相关文章推荐
- java 调用ActiveDirectory,使用标准的LDAP协议
- Java调用使用SSL/HTTPS协议来传输的axis webservice服务
- java使用域账号登陆,ldap协议
- 如何使用LoadRunner8.1的java Vuser协议调用jar文件
- 使用LoadRunner8.1的java Vuser协议调用jar文件(三) [ 光影人像 东海陈光剑 的博客 ]
- java如何调用ldap协议【LdapContext】
- 使用LoadRunner8.1的java Vuser协议调用jar文件(二) [ 光影人像 东海陈光剑 的博客 ]
- java如何调用ldap协议【LdapContext】
- 使用LoadRunner8.1的java Vuser协议调用jar文件(一) [ 光影人像 东海陈光剑 的博客 ]
- PHP使用PHPRPC协议调用JAVA类
- Java调用使用SSL/HTTPS协议来传输的axis webservice服务
- (java使用myeclipse)调用webservice接口进行操作
- 使用python调用java-jpype
- java使用AXIS2调用asp.net的WebService
- Android jni 使用C语言调用java中的log方法
- 【性能】Loadrunner(LR)中java协议调用get/post请求,并验证结果脚本
- 关于C++调用jar包,在创建虚拟机时使用JNI_CreateJavaVM调用失败返回-1的问题
- Java处理http协议相关初步(三)——线程池的使用分析
- 如何使用网页开发自己的app,在网页中的按钮与自己的java代码绑定来实现打电话即javascript代码调用java代码,和java代码来调用javascript代码
- 关于java使用jacob调用word,ppt,excel等的转化为html的问题