Use Java to connect to the AD domain to verify that the account password is correct

Source: Internet
Author: User
Tags ldap domain server

In Web projects, customers sometimes ask us to use the AD domain for identity verification, and no longer have to do another set of user management systems. In fact, the customer is as long as a set of accounts can access all OA,CRM and other office systems.
This is the third-party verification. Generally have ad domain, ldap,radius, mail server and so on. The most common is the number of ad domains. Because window System occupies a lot of land in the country. It is also very convenient to do.
I am writing this article, how to use Java to achieve the authentication of the ad domain. OK, just look at the code:
Package Com.test;import Java.util.hashtable;import Javax.naming.authenticationexception;import Javax.naming.context;import Javax.naming.directory.dircontext;import javax.naming.directory.initialdircontext;/* * * Use Java to connect to the ad domain, verify that the account password is correct * @author herman.xiong * @date 2014-12-23 pm 02:07:26 * @version V3.0 * @since JDK 1.6,tomcat 6.  0 */public class ADTest {/** * use Java to connect to AD domain * @author herman.xiong * @date 2014-12-23 pm 02:24:04 * @return void * @throws Exception description * @param host connects to an ad domain server's IP * @param the port of the post AD domain Server * @param username username * @param password password */public static void Co Nnect (String host,string post,string username,string password) {DirContext ctx=null; hashtable<string,string> hashenv = new hashtable<string,string> (); Hashenv.put (Context.security_authentication, "simple"); LDAP access security level (None,simple,strong) hashenv.put (Context.security_principal, username); The user name of Ad hashenv.put (context.security_credentials, password); The password for ad hashenv.put (context.initial_context_factory, "Com.sun.jndi. LDAP. Ldapctxfactory "); LDAP Factory class Hashenv.put ("Com.sun.jndi.ldap.connect.timeout", "3000");//connection timeout set to 3 seconds Hashenv.put (Context.provider_url, " LDAP://"+ Host +": "+ post);//default port 389try {ctx = new InitialDirContext (hashenv);//Initialize Context System.out.println (" Authentication succeeded! "); } catch (Authenticationexception e) {System.out.println ("Failed authentication!"); E.printstacktrace ();} catch (Javax.naming.CommunicationException e) {System.out.println ("AD Domain Connection failed!"); E.printstacktrace ();} catch (Exception e) {System.out.println ("Authentication unknown exception!"); E.printstacktrace ();} Finally{if (null!=ctx) {try {ctx.close (); ctx=null;} catch (Exception e) {e.printstacktrace ();}}}} public static void Main (string[] args) {adtest.connect ("10.10.2.116", "389", "[email protected]", "123456");}}

This concludes the code to see how it works:

Welcome everyone to follow my blog! If in doubt, please add QQ Group: 135430763 Study together!

Use Java to connect to the AD domain to verify that the account password is correct

Related Article

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.