1【仿写】
2
public LdapContext getLdapContext() throws NamingException { String userName = "App01"; // 用户名称 String password = "password"; // 密码 String host = "192.168.1.1"; // AD服务器 String port = "389"; // 端口 String url = new String("ldap://" + host + ":" + port); Hashtable env = new Hashtable(); env.put(Context.SECURITY_AUTHENTICATION, "simple");// 以simple方式发送 env.put(Context.SECURITY_PRINCIPAL, "cn=App01,cn=users,DC=com"); // env.put(Context.SECURITY_CREDENTIALS, password); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, url); return new InitialLdapContext(env, null); }
3
public void add() { try { String newUserName = "test1"; BasicAttributes attrs = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute("objectclass"); objclassSet.add("person"); objclassSet.add("top"); objclassSet.add("organizationalPerson"); objclassSet.add("user"); attrs.put(objclassSet); attrs.put("sn", newUserName); attrs.put("uid", newUserName); attrs.put("cn", newUserName); attrs.put("userPassword", "password"); attrs.put("sAMAccountName","test1"); attrs.put("userAccountControl","2"); attrs.put("mail",newUserName+"@163.com"); attrs.put("displayName","张三"); attrs.put("homePhone","666666"); attrs.put("telephoneNumber","13888888888"); attrs.put("title","Test1"); getLdapContext().createSubcontext("cn=" + newUserName + ",cn=users,DC=com", attrs); } catch (Exception e) { e.printStackTrace(); } }
4
public void testModify() { String uid = "test1"; String userDN = "cn=" + uid + ",cn=users,DC=com"; Attributes attrs = new BasicAttributes(true); attrs.put("userPassword", "test2"); attrs.put("title", "Manager"); try { getLdapContext().modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
5
public void removeUser(String userName){ try { getLdapContext().destroySubcontext("cn=" + userName + ",cn=users,DC=com"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
6
public void search() { SearchControls searchCtls = new SearchControls(); // Create the search // controls searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify String searchFilter = "(&(objectClass=user)(cn=test2))";//"(objectClass=user)";// String[] returnedAtts = new String[] { "distinguishedName","userPassword", "department","title","userPassword","sAMAccountName", "flags", "displayName","whenChanged" }; searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集 String searchBase = "DC=com"; NamingEnumeration<SearchResult> answer = null; List<Map<String, String>> adList = new ArrayList<Map<String, String>>(); try { answer = this.getLdapContext().search(searchBase, searchFilter, searchCtls); while (answer.hasMoreElements()) { SearchResult searchResult = answer.next(); Attributes attributes = searchResult.getAttributes(); Map<String, String> accountInfo = new HashMap<String, String>(); for (NamingEnumeration<?> namingEnumeration = attributes .getAll(); namingEnumeration.hasMore();) { Attribute attribute = (Attribute) namingEnumeration.next(); String attrId = attribute.getID().toString(); attribute.getAttributeDefinition(); int i = 0; String attrValue = ""; for (NamingEnumeration<?> e = attribute.getAll(); e .hasMore();) { String val = e.next().toString(); if (i != 0) { attrValue += ";"; } i++; attrValue += val; } System.out.println("attrId:" + attrId+" attrValue:" + attrValue); accountInfo.put(attrId, attrValue); } adList.add(accountInfo); System.out.println("\n\n"); } System.out.println("size:" + adList.size()); } catch (AuthenticationException e) { e.printStackTrace(); System.out.println("AD服务器域管理员账号验证失败!"); } catch (NamingException e) { e.printStackTrace(); System.out.println("AD服务器连接失败,请检查配置是否正确!"); } }
7 Spring Ldap
private LdapTemplate ldapTemplate; public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } public void getAllUser() { AndFilter andFilter = new AndFilter(); andFilter.and(new EqualsFilter("objectclass", "person")); //andFilter.and(new EqualsFilter("cn", "xwl")); List list = ldapTemplate.search("cn=users,DC=com", andFilter.encode(), new UserAttributeMapper()); for(Object u:list){ System.out.println(((Users)u).getName()); System.out.println(((Users)u).getPwd()+"\n"); } System.out.println(list.size()); } public void bind1() { BasicAttribute objclassSet = new BasicAttribute("objectclass"); objclassSet.add("person"); objclassSet.add("top"); objclassSet.add("organizationalPerson"); objclassSet.add("user"); Attributes attr = new BasicAttributes(); attr.put(objclassSet); // 必填属性,不能为null也不能为空字符串 attr.put("sn", "test1"); attr.put("uid", "test1"); attr.put("cn", "xwl1"); attr.put("sAMAccountName", "test1"); attr.put("userPassword", "1qa2ws3ed54"); attr.put("userAccountControl", "2"); attr.put("mail", "test3@163.com"); ldapTemplate.bind(("cn=xwl1,cn=users,DC=com"), null, attr); } public static void main(String[] args) { ApplicationContext cxt = new ClassPathXmlApplicationContext("app_ldap.xml"); LdapPersonInfoImpl userDao = (LdapPersonInfoImpl) cxt .getBean("ldapPersonInfoImpl"); //List<String> users = userDao.getAllUser();//getAllPersonNames(); // for(String str:users) // System.out.println(str); // userDao.bind1(); }
public class UserAttributeMapper implements AttributesMapper { private Logger log=Logger.getLogger(UserAttributeMapper.class); @Override public Object mapFromAttributes(Attributes attr) throws NamingException { Users user = new Users(); user.setName(attr.get("sAMAccountName").get().toString()); try { user.setPwd(new String((byte[])attr.get(LdapContextSourceBean.AD_USER_PASS_WORD).get(), "GB2312")); } catch (Exception e) { log.error(" User Passwrod get fail",e); } return user; } }
8
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://192.168.1.1:389" /> <property name="userDn" value="cn=App01,cn=users,DC=com" /> <property name="password" value="password" /> </bean> <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> <constructor-arg ref="contextSource" /> </bean> <bean id="ldapPersonInfoImpl" class="com.ladp.LdapPersonInfoImpl"> <property name="ldapTemplate"> <ref bean="ldapTemplate" /> </property> </bean> </beans>
spring-ldap-core-1.3.2.RELEASE.jar
9
10
相关推荐
在这篇文章中,我们将探讨使用 JAVA 实现 LDAP 的 AD 域免证书查询、修改、删除、新增、启用、禁用和修改密码的操作。 首先,让我们了解什么是 LDAP 和 AD 域。LDAP(Lightweight Directory Access Protocol)是一...
本文将深入探讨标题“Sync_Data.rar_C LDAP AD_c++ ldap_ldap_数据同步”所涉及的知识点,包括C++ LDAP库的使用、Active Directory(AD)集成以及数据同步策略。 1. **C++ LDAP库**: LDAP(轻量级目录访问协议)...
在IT行业中, Lightweight Directory Access Protocol (LDAP) 是一种用于存储和检索目录信息的标准协议,而Active Directory (AD) 是微软提供的目录服务,广泛应用于企业环境中进行用户身份验证和权限管理。...
标题 "LDAP Weblogic和AD之间的通信" 涉及到的是如何使用轻量级目录访问协议(Lightweight Directory Access Protocol, LDAP)在Weblogic服务器与活动目录(Active Directory, AD)之间建立通信,以实现身份验证和...
**mutt-ldap.pl: LDAP AD查询工具** `mutt-ldap.pl` 是一个基于Perl语言编写的脚本,主要用于帮助用户在Mutt邮件客户端中通过LDAP(轻量级目录访问协议)查询Active Directory(AD)服务器上的信息。这个工具为Mutt...
Java 使用 LDAP 修改 AD 域用户密码 本文将详细介绍如何使用 Java 语言通过 LDAP 协议修改 Windows Active Directory 域用户的密码。同时,本文也将对相关概念进行解释,以便读者更好地理解整个过程。 LDAP 概念 ...
标题 "ldap 访问AD测试" 暗示了我们正在讨论如何使用Lightweight Directory Access Protocol (LDAP) 来连接并操作Active Directory (AD) 架构。在IT环境中,AD是一个重要的服务,用于集中管理用户账户、权限和网络...
CAS 服务器可以与多种身份验证系统集成,包括 LDAP、Active Directory 和数据库等。 Windows AD 是微软公司推出的目录服务,用于管理计算机网络中的用户、组和资源。Windows AD 提供了身份验证、授权和目录服务等...
基于SSL的LDAP安全访问AD认证 基于SSL的LDAP安全访问AD认证是指使用SSL(Secure Sockets Layer)协议来保护LDAP(Lightweight Directory Access Protocol)协议与AD(Active Directory)的通信,使得密码在网络中...
本文将深入探讨如何使用Spring Boot 2.x与LDAP集成,特别是在与Active Directory(AD)域控制器交互,实现用户和组织的同步。 首先,让我们了解Spring Boot 2.x。Spring Boot是Spring框架的一个扩展,它简化了创建...
通过Python,基于ldap3来实现操作AD域控,账户信息获取、解锁账户、禁用账户、启用账户、重置密码等功能。
在本文中,我们将深入探讨如何使用Java通过Ldap与Active Directory (AD)域进行交互。首先,我们需要了解AD域是一个集中式服务,用于管理网络中的用户、计算机和其他资源的身份和权限。Ldap(轻量级目录访问协议)是...
Adldap2, 用于人类的PHP LDAP包 Adldap2 使用LDAP不需要硬处理。Adldap2是一个经过测试的PHP包,它使用 Active Record Pattern 提供LDAP身份验证和目录管理工具。索引快速入门配置文件连接认证查
**Laravel 开发与 adldap2-laravel** 在IT行业中,Laravel是一个广泛使用的开源PHP框架,它以其优雅的语法和强大的功能深受开发者喜爱。Laravel致力于简化Web应用的开发,提供了一系列工具来帮助开发者更高效地构建...
Given the varied nature of organisations and sites, adLDAP may not be _your_ complete solution, but it should be a very sound starting point. LDAP isn't overly friendly on first glance, and it's a ...