java 调用ActiveDirectory,使用标准的LDAP协议

在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);
    }
}
}

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值