Java invoke ActiveDirectory, using standard LDAP protocol

Source: Internet
Author: User
Tags end ldap ldap host ldap protocol string port number websphere application server
The active| standard supports the LDAP protocol in ActiveDirectory, and we can access it using the standard Java JNDI API in Java. The LDAP server does not really have to support the JNDI API, as long as the LDAP protocol is supported. We have provided a simple test case program to authenticate an LDAP server's distinguished name. In general, the treatment of activedirectory does not have to be different from treating any other LDAP server.
Import Java.util.properties;import javax.naming.*; Import Javax.naming.directory.*;//include The JNDI in the classpath.  Should use the same JDK used by WebSphere application Server.class Wasldapauth {public static void main (string[) args) {//***************** user information to is authenticated ********************************//*****************Plea  SE 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 is 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);        The 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);     }}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.