InitialDirContext- Java SE 11 & JDK 11
Introduction
InitialDirContext is a Java class that provides a way to connect to a directory service and perform operations such as searching, adding, modifying, and deleting entries. It is part of the Java Naming and Directory Interface (JNDI) API, which is a standard way of accessing different naming and directory services.
Working with InitialDirContext
To use InitialDirContext, you need to create an instance of it by passing it a set of properties that define the connection parameters. These properties include the URL of the directory service, the username and password (if required), and other options such as the security settings and the protocol to use.
Hashtable<String, Object> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=mycompany,dc=com"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=mycompany,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "adminpassword"); InitialDirContext ctx = new InitialDirContext(env);
Once you have created an InitialDirContext object, you can use its methods to perform various operations on the directory service. For example, you can search for entries that match certain criteria, add new entries, modify existing entries, and delete entries.
Searching for Entries
To search for entries in a directory service using InitialDirContext, you need to create a SearchControls object that specifies the search criteria and the attributes to return. You then pass this object to the search() method of the InitialDirContext object, along with the search base (the point in the directory tree where the search should start) and the search filter (the criteria for matching entries).
SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); controls.setReturningAttributes(new String[]{"cn", "mail", "telephoneNumber"}); controls.setTimeLimit(10000); String filter = "(&(objectClass=person)(cn=John Doe))"; NamingEnumeration<SearchResult> results = ctx.search("ou=people,dc=mycompany,dc=com", filter, controls); while (results.hasMore()) { SearchResult result = results.next(); Attributes attrs = result.getAttributes(); String name = attrs.get("cn").get().toString(); String email = attrs.get("mail").get().toString(); String phone = attrs.get("telephoneNumber").get().toString(); System.out.println(name + ", " + email + ", " + phone); }
In this example, we are searching for all entries of class "person" with the common name "John Doe" in the "ou=people,dc=mycompany,dc=com" subtree. We are returning the "cn", "mail", and "telephoneNumber" attributes and setting a time limit of 10 seconds for the search. We are then iterating over the results and printing out the values of these attributes for each matching entry.
Adding Entries
To add a new entry to a directory service using InitialDirContext, you need to create a BasicAttributes object that defines the attributes of the entry. You then create a new DirContext object for the parent entry (the one under which the new entry will be added) and call its createSubcontext() method, passing it the name of the new entry and the BasicAttributes object.
Attributes attrs = new BasicAttributes(); attrs.put(new BasicAttribute("objectClass", "person")); attrs.put(new BasicAttribute("cn", "Jane Doe")); attrs.put(new BasicAttribute("mail", "jane.doe@mycompany.com")); attrs.put(new BasicAttribute("telephoneNumber", "555-1234")); DirContext parentCtx = (DirContext)ctx.lookup("ou=people,dc=mycompany,dc=com"); parentCtx.createSubcontext("cn=Jane Doe", attrs);
In this example, we are creating a new entry of class "person" with the common name "Jane Doe" and the email address "jane.doe@mycompany.com" and phone number "555-1234". We are then creating a new DirContext object for the "ou=people,dc=mycompany,dc=com" entry and calling its createSubcontext() method to add the new entry as a subentry.
Modifying Entries
To modify an existing entry in a directory service using InitialDirContext, you need to create a ModificationItem object that specifies the attribute to modify and the type of modification (add, replace, or remove). You then create an array of ModificationItem objects and call the modifyAttributes() method of the DirContext object for the entry, passing it the array.
ModificationItem[] mods = new ModificationItem[2]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", "jane.doe@mynewcompany.com")); mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("description", "Marketing Manager")); ctx.modifyAttributes("cn=Jane Doe,ou=people,dc=mycompany,dc=com", mods);
In this example, we are modifying the email address of the "Jane Doe" entry to "jane.doe@mynewcompany.com" and adding a new attribute "description" with the value "Marketing Manager". We are then calling the modifyAttributes() method of the InitialDirContext object, passing it the DN (distinguished name) of the entry and the array of ModificationItem objects.
Deleting Entries
To delete an entry from a directory service using InitialDirContext, you need to call the destroySubcontext() method of the DirContext object for the entry.
ctx.destroySubcontext("cn=Jane Doe,ou=people,dc=mycompany,dc=com");
In this example, we are deleting the "Jane Doe" entry from the "ou=people,dc=mycompany,dc=com" subtree by calling the destroySubcontext() method of the InitialDirContext object and passing it the DN of the entry.
Conclusion
InitialDirContext is a powerful and flexible class that provides a standard way of connecting to and working with directory services in Java applications. By using its methods to search, add, modify, and delete entries, you can easily manage directory data and integrate it with other systems and applications.
Whether you are building an LDAP-enabled web application, a network management tool, or a directory-enabled messaging system, InitialDirContext can help you access and manipulate directory data in a simple and efficient way.
本文来源:词雅网
本文地址:https://www.ciyawang.com/x1tcyu.html
本文使用「 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 」许可协议授权,转载或使用请署名并注明出处。
相关推荐
-
事件处理:什么是它?
的一系列操作。这些事件可以是用户输入、系统消息、网络数据包等。在编程中,事件处理常用于图形用户界面(gui)和网络编程中。通过事件处理,程序可以及时响应用户行为,从而实现更好的用户体验。 事件处理的
-
如何进行数据库表结构设计和索引优化建议
您需要唯一标识每个行,并且您不想依赖于外部数据,那么应该使用自增长整数作为主键。 -全局唯一标识符(guiD):如果您需要在多个数据库之间复制数据,并且需要唯一标识每个行,那么应该使用guiD作为主键
-
如何使用PHP进行机器学习和数据挖掘?
机器学习工具,它提供了许多常用的机器学习算法和数据挖掘工具。Weka可以通过Java的命令行接口或者gui界面来进行操作。同时,Weka也提供了PHP的接口,可以通过调用Weka的PHP库来实现机器学
-
Array_udiff Function in PHP- An Advanced Guide
Introduction If you're a PHP developer, then you must be familiar with arrays. Arrays are an import...
-
Android Eclipse集成环境搭建
。作为Android开发的主要工具之一,Eclipse提供了完整的开发环境,包括代码编辑器、调试器、gui设计器和版本控制系统等。使用Eclipse可以大大提高开发效率和代码质量。 安装Eclips
-
BorderLayout:Java SE 11 & JDK 11的布局管理器
其是在企业级应用开发方面。在Java中,布局管理器是一种非常重要的工具,它可以帮助我们快速地创建各种gui应用程序。BorderLayout是Java SE 11和JDK 11中的一个布局管理器,它被
-
C语言回调函数详解:让你的程序变得更加智能
是一种常见的编程技术,它可以让程序变得更加智能,更加灵活。回调函数通常被用于异步编程、事件驱动编程、gui编程等领域。在本文中,我们将深入探讨C语言中的回调函数,从而更好地理解这一技术的本质。 什么
-
GridBagConstraints:Java SE 11 &amp; JDK 11中的布局神器
引言 Java是一种跨平台的编程语言,支持各种类型的应用程序。在Java中,gui(图形用户界面)设计是一项重要的任务。Java提供了一个名为GridBagLayout的布局管理器,它可以帮助开发人员
-
Java 简介:探索最流行的编程语言
桌面应用程序 Java 可以用于开发桌面应用程序。Java Swing 是 Java 中最流行的 gui 工具包之一,它可以用于创建跨平台的桌面应用程序。 public class HelloWo