Import com.sun.org.apache.xerces.internal.impl.dv.util.*;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
Import java.util.Hashtable;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import Javax.naming.Context;
Import javax.naming.NamingException;
Import Javax.naming.directory.BasicAttribute;
Import javax.naming.directory.BasicAttributes;
Import Javax.naming.directory.DirContext;
Import Javax.naming.directory.InitialDirContext;
Import Javax.naming.directory.ModificationItem;
/** * User login authentication, LDAP Cross-domain authentication, through LDAP to update the user * * public class Ldaputil {private static DirContext ctx;
LDAP Server port defaults to 389 private static final String Ldap_url = "ldap://test.demo.com:389";
LDAP-driven private static final String ldap_factory = "Com.sun.jndi.ldap.LdapCtxFactory";
/**** test ****/public static void Main (string[] args) {getlogincontext (); }//By connecting the LDAP server to the userFor authentication, return LDAP object public static DirContext Getlogincontext () {String account = "demotest";//Impersonate user name String password = "12345678";
Analog password for (int i = 0; i < 5; i++) {//Authentication count Hashtable env = new Hashtable ();
Env.put (Context.security_authentication, "simple");
Env.put (context.security_credentials, password);
Env.put (Context.security_principal, "uid=" + account + ", cn=users,cn=accounts,dc=demo,dc=com");
Env.put (Context.initial_context_factory, ldap_factory);
Env.put (Context.provider_url, Ldap_url); Which organization structure name cn= belongs to, ou= the hierarchy position number under an organizational structure name try {///Connect LDAP for authentication ctx = new Initi
Aldircontext (env);
SYSTEM.OUT.PRINTLN ("Successful Authentication");
System.out.println ("" "+ Account +" "User Login System Success"); catch (Javax.naming.AuthenticationException e) {System.out.prinTLN ("Authentication Failure"); catch (Namingexception err) {System.out.println ("--------->>" "+ Account +" "User authentication failed" "+ i +" "Times
");
catch (Exception e) {System.out.println ("Authentication error:");
E.printstacktrace ();
} return CTX; }
}
Reference https://blog.csdn.net/u012411219/article/details/51331846