Connection Freeipa Authentication LDAP login, Java code demo

Source: Internet
Author: User
Tags ldap server port
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


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.