博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用JAVA 查询 Active Directory(AD)
阅读量:7091 次
发布时间:2019-06-28

本文共 3984 字,大约阅读时间需要 13 分钟。

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) ?
  • 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.


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 program. 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
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 connectiontry {dirContext = new InitialDirContext(properties);} catch (NamingException e) {LOG.severe(e.getMessage());}// default domain base for searchdomainBase = getDomainBase(domainController);// initializing search controlssearchCtls = 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
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


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!

 

原文:

相关链接:

1.

2.

3.

4.

5.

转载于:https://www.cnblogs.com/Tuzki/p/4633381.html

你可能感兴趣的文章
[译] 系列教程:选择准备安装的 TensorFlow 类型
查看>>
Webhook到底是个啥?
查看>>
Flutter学习 ---- TravisCI加持
查看>>
借用UAC完成的提权思路分享
查看>>
【小程序踩坑系列1】 扫普通二维码调起小程序bug:码地址传递错误,传为历史地址...
查看>>
高阶自定义View --- 粒子变幻、隧道散列、组合文字
查看>>
Django REST framework API 指南(6):路由
查看>>
性能优化工具知识梳理(6) Memory Monitor & Heap Viewer & Allocation Tracker
查看>>
《Java8实战》-第八章笔记(重构、测试和调试)
查看>>
安卓调用PrinterShare实现无线打印功能
查看>>
Python 学习(一)
查看>>
Android开发人员不得不学习的JavaScript基础(二)
查看>>
Android项目实践——短信发送接口的封装与设计
查看>>
openresty下lua的function定义及调用
查看>>
五分钟看懂UML类图与类的关系详解
查看>>
Android中Lottie的简单使用
查看>>
[翻译]Spring Boot 特征参考2——外部配置:下
查看>>
设计模式系列-单例模式
查看>>
PHP语言好学吗?php学习从入门到会需要多久?
查看>>
leetCode 5 Longest Palindromic Substring
查看>>