用JAVA 查询 Active Directory(AD)
Required Details
- LDAP address (For e.g.: myjeeva.com or IP of the Domain Controller/Global Catalog[GC])
- Port # (For e.g.: 3289 or 389) where would you to like search user details?
- Domain Username
- Domain Password
Important Reference: will introduce you to the classes needed for querying Active Directory using Java. Have a look and know more about it.
- javax.naming.Context
- javax.naming.directory.InitialDirContext
- javax.naming.directory.DirContext
- javax.naming.directory.SearchControls
- javax.naming.directory.SearchResult
How to do – Step by Step explaination
For an easy understanding perspective; I will be following line by line approach. ActiveDirectory Class file and example of how to use that ActiveDirectory class file in javaprogram. Downloads of these files you will find below.
Step 1
Compose LDAP address and supply following parameters username, password, ldap address as a domain into ActiveDirectory constructor.
ActiveDirectory activeDirectory = new ActiveDirectory(username, password,
domain);
Step 2
Invoke searchUser method with parameters of searchTerm, choice and searchBase.
NamingEnumeration<SearchResult> result =
activeDirectory.searchUser(searchTerm, choice, “DC=myjeeva,DC=com”);
Step 3
Now you have your search result in result variable.
How it works?
Part 1
ActiveDirectory constructor-
- It creates properties instance with given values (ldap address, username, password)
- It initializes the Directory Context
- It assign the Search Scope and return attribute names
/** * constructor with parameter for initializing a LDAP context * * @param username a {@link java.lang.String} object - username to establish a LDAP connection * @param password a {@link java.lang.String} object - password to establish a LDAP connection * @param domainController a {@link java.lang.String} object - domain controller name for LDAP connection */ public ActiveDirectory(String username, String password, String domainController) { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); properties.put(Context.PROVIDER_URL, "LDAP://" + domainController); properties.put(Context.SECURITY_PRINCIPAL, username + "@" + domainController); properties.put(Context.SECURITY_CREDENTIALS, password); // initializing active directory LDAP connection try { dirContext = new InitialDirContext(properties); } catch (NamingException e) { LOG.severe(e.getMessage()); } // default domain base for search domainBase = getDomainBase(domainController); // initializing search controls searchCtls = new SearchControls(); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); searchCtls.setReturningAttributes(returnAttributes); }
Part 2
searchUser method utilizes the filter method to construct the active directory query.
/** * search the Active directory by username/email id for given search base * * @param searchValue a {@link java.lang.String} object - search value used for AD search for eg. username or email * @param searchBy a {@link java.lang.String} object - scope of search by username or by email id * @param searchBase a {@link java.lang.String} object - search base value for scope tree for eg. DC=myjeeva,DC=com * @return search result a {@link javax.naming.NamingEnumeration} object - active directory search result * @throws NamingException */ public NamingEnumeration<SearchResult> searchUser(String searchValue, String searchBy, String searchBase) throws NamingException { String filter = getFilter(searchValue, searchBy); // For eg.: "DC=myjeeva,DC=com"; String base = (null == searchBase) ? domainBase : getDomainBase(searchBase); return this.dirContext.search(base, filter, this.searchCtls); } private String getFilter(String searchValue, String searchBy) { String filter = this.baseFilter; if(searchBy.equals("email")) { filter += "(mail=" + searchValue + "))"; } else if(searchBy.equals("username")) { filter += "(samaccountname=" + searchValue + "))"; } return filter; }
Downloads
ACTIVEDIRECTORY.JAVASAMPLEUSAGEACTIVEDIRECTORY.JAVA
Completion
That’s it, you have learned querying active directory using java and you can download artifacts. Try it out yourself with class provided and experiment it.
For any queries please leave a comment!
原文: http://myjeeva.com/querying-active-directory-using-java.html
相关链接:
1. How To Authenticate Users With Active Directory
2. AzureAD/azure-activedirectory-library-for-java
4. A complete Java example complete with LDAP query code ...
5. Using JAVA code with Active Directory – JefTek.com
用JAVA 查询 Active Directory(AD)的更多相关文章
- How to setup Active Directory (AD) In Windows Server 2016
Windows Server 2016 is the newest server operating system released by Microsoft in October 12th, 201 ...
- C#操作Active Directory(AD)详解
1. LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门 ...
- datazen Active Directory AD 配置
今天苦心经营的datazen 链接AD,文档已经无法吐槽了简单的几句话,根本不够用. 先说一下链接AD 的好处吧, 1 首先免去设置密码的麻烦,因为直接用AD账号的密码. 2 更安全,因为客户可不想自 ...
- C# AD(Active Directory)域信息同步,组织单位、用户等信息查询
示例准备 打开上一篇文章配置好的AD域控制器 开始菜单-->管理工具-->Active Directory 用户和计算机 新建组织单位和用户 新建层次关系如下: 知识了解 我们要用C# ...
- TFS 与活动目录AD(Active Directory)的同步机制
TFS用户管理机制 TFS系统与企业域服务器用户系统(或本地计算机用户系统)高度集成在一起,使用域服务器验证系统用户的账户和密码,从而在企业中实现单一用户,单点登录.也就是说,TFS系统自身并没有用户 ...
- AD域的安装(在Windows Server 2003中安装Active Directory)
在Active Directory中提供了一组服务器作为身份验证服务器或登录服务器,这类服务器被称作域控制器(Domain Controller,简称DC).建立一个AD域的过程实际就是在一台运行Wi ...
- Windows Azure Active Directory (2) Windows Azure AD基础
<Windows Azure Platform 系列文章目录> Windows Azure AD (WAAD)是Windows Azure提供的一个REST风格的服务,为您的云服务提供了身 ...
- Windows Azure Active Directory (3) China Azure AD增加新用户
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的China Azure. 本文是对笔者之前的文档:Windows Azure Active ...
- Windows Azure Active Directory (4) China Azure AD Self Password Reset
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 在开始本章内容之前,请读者熟悉笔者之前写的文档: Windows ...
随机推荐
- dom+bom
一.判断最大值和最小值,注:arr为数组 最大值:Math.max.apply(null, arr); 最小值:Math.min.apply(null, arr); 二.BOM 打开新页面和关闭打 ...
- U3D 2D游戏之黑暗纪元 2D游戏基础入门开发全(1)
第一个U3D 2D游戏的例子,全部自己编写,算是入门用,这里记录一下. 1.首先游戏把层次布置好,这里分为 背景层,游戏层,UI层 背景层 (Background-1):就是单纯的背景显示作用. 游戏 ...
- Android XML解析
解析XML有三种方式:Dom.SAX.Pull 其中pull解析器运行方式与SAX类似. 我们首先认识pull解析器:http://developer.android.com/intl/zh-cn/r ...
- Webservice学习之——即时发布与定制发布
一.工具 myEclipse tomcat 6.0 以上版本 axis-bin-1_4.zip 二.即时发布 1.解压 axis-bin-1_4.zip 2.axis-bin-1_4.zip\axi ...
- 一个fibonacci数列简单求和的问题
前段时间老师在讲函数调用的时候,用Fibonacci数列来演示了一下,因为以前没怎么接触过Fibonacci,所以当时很懵. 当时让求的是Fibonacci数列中,第N位值为多少,当时老师写的是: 之 ...
- ECMA5.1中关于encodeURI,decodeURI 和encodeComponentURI,decodeComponentURI的区别
The encodeURI and decodeURI functions are intended to work with complete URIs; theyassume that any r ...
- Linux下Docker安装
1 在 CentOS 6.4 上安装 docker docker当前官方只支持Ubuntu,所以在 CentOS 安装Docker比较麻烦(Issue #172). docker官方文档说要求 ...
- 构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml
学习maven项目时 搭建个ssm项目 算是给自己留个备份吧 环境说明: MyEclipse10 Maven 3.2.3 框架: struts2 2.3.24.1 spring3 3. ...
- c#面向对象小结
特点: 1:将复杂的事情简单化. 2:面向对象将以前的过程中的执行者,变成了指挥者. 3:面向对象这种思想是符合现在人们思考习惯的一种思想. 过程和对象在我们的程序中是如何体现的呢?过程其实就是函数: ...
- 织梦DedeCMS网站地图模板
亲和百度蜘蛛,分页多层次特色,织梦系统最好用的网站地图! 用 DedeCMS(织梦) 系统搭建的网站多数都是以优化为主要目标的网站类型,既然是优化站 SEO 手段就离不开为网站设置网站地图.可是 De ...