Required Details

Important Reference: will introduce you to the classes needed for querying Active Directory using Java. Have a look and know more about it.


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

3. Java JNDI/LDAP: Windows Active Directory Authentication, Organizational Unit, Group & Other Information Access

4. A complete Java example complete with LDAP query code ...

5. Using JAVA code with Active Directory – JefTek.com

用JAVA 查询 Active Directory(AD)的更多相关文章

  1. 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 ...

  2. C#操作Active Directory(AD)详解

    1. LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门 ...

  3. datazen Active Directory AD 配置

    今天苦心经营的datazen 链接AD,文档已经无法吐槽了简单的几句话,根本不够用. 先说一下链接AD 的好处吧, 1 首先免去设置密码的麻烦,因为直接用AD账号的密码. 2 更安全,因为客户可不想自 ...

  4. C# AD(Active Directory)域信息同步,组织单位、用户等信息查询

    示例准备 打开上一篇文章配置好的AD域控制器 开始菜单-->管理工具-->Active Directory 用户和计算机 新建组织单位和用户   新建层次关系如下: 知识了解 我们要用C# ...

  5. TFS 与活动目录AD(Active Directory)的同步机制

    TFS用户管理机制 TFS系统与企业域服务器用户系统(或本地计算机用户系统)高度集成在一起,使用域服务器验证系统用户的账户和密码,从而在企业中实现单一用户,单点登录.也就是说,TFS系统自身并没有用户 ...

  6. AD域的安装(在Windows Server 2003中安装Active Directory)

    在Active Directory中提供了一组服务器作为身份验证服务器或登录服务器,这类服务器被称作域控制器(Domain Controller,简称DC).建立一个AD域的过程实际就是在一台运行Wi ...

  7. Windows Azure Active Directory (2) Windows Azure AD基础

    <Windows Azure Platform 系列文章目录> Windows Azure AD (WAAD)是Windows Azure提供的一个REST风格的服务,为您的云服务提供了身 ...

  8. Windows Azure Active Directory (3) China Azure AD增加新用户

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的China Azure. 本文是对笔者之前的文档:Windows Azure Active ...

  9. Windows Azure Active Directory (4) China Azure AD Self Password Reset

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 在开始本章内容之前,请读者熟悉笔者之前写的文档: Windows ...

随机推荐

  1. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  2. com.android.builder.packaging.DuplicateFile

    解决方法:     packagingOptions {        exclude 'META-INF/DEPENDENCIES'        exclude 'META-INF/NOTICE' ...

  3. c#泛型方法重载

    这里存在普通的方法Foo和泛型方法Foo,如果直接调用: 则会自动优先匹配对应的非泛型方法.输出如下: 但需要注意的是,这一匹配过程是在编译过程进行的,所以如果是通过其它泛型间接调用.则只会调用对应的 ...

  4. js判断主流浏览器类型和版本号

    如今的互联网中,浏览器可以说是太多太多了,但是大部分都是换壳不换心,基本上主流的浏览器还是火狐,谷歌,IE,safrai这几种比较常见,所以在我们的开发中,有时候需要遇到判断用户正在使用什么浏览器以及 ...

  5. 01_JavaMail_05_创建邮件工具类MailUtils等方便发送邮件

    [工程截图] [代码实现] [Mail.java] package com.Higgin.Utils; import java.util.ArrayList; import java.util.Lis ...

  6. 九度OJ 1019 简单计算器 -- 2006年浙江大学计算机及软件工程研究生机试真题

    题目地址:http://ac.jobdu.com/problem.php?pid=1019 题目描述:     读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 输入: ...

  7. 简单的doc命令

    cd 切换目录 dir 显示目录列表 mkdir 创建目录(mkdir) rmdir 删除空目录(rmdir test) rmdir  /s 删除非空目录(rmdir test /s) echo 创建 ...

  8. 网站开发常用jQuery插件总结(五)滚动条插件nanoscroller

    网站在展示信息时,如果信息量过大,解决方法主要有三种.1.分页,将信息分页显示.2.整页显示,但是页面过长,影响浏览体验.3.使用滚动条,而默认滚动条样式太单一,用户体验不友好.所以我们需要美化滚动条 ...

  9. php中浮点数计算问题

    如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个 ...

  10. 表格行变换顺序功能(jquery)

    周末写了个更改表格行顺序的小功能,直接贴代码 表格部分如下: <table class="table" id="test_table"> <t ...