java ldap连接_JAVA操作LDAP总结

本文详细介绍了如何使用JAVA操作LDAP,包括添加、更新和查询用户信息。通过`LdapTemplate`实现与LDAP目录服务的交互,利用过滤器进行多条件查询,并展示了如何处理多值属性。
摘要由CSDN通过智能技术生成

1 package com.test.dao;2

3 import java.beans.PropertyDescriptor;4 import java.lang.reflect.Field;5 import java.lang.reflect.InvocationTargetException;6 import java.lang.reflect.Method;7 import java.util.ArrayList;8 import java.util.List;9 import javax.naming.Name;10 import javax.naming.NamingEnumeration;11 import javax.naming.NamingException;12 import javax.naming.directory.Attribute;13 import javax.naming.directory.Attributes;14 import javax.naming.directory.BasicAttribute;15 import javax.naming.directory.BasicAttributes;16 import javax.naming.directory.ModificationItem;17 import org.apache.commons.beanutils.BeanUtils;18 import org.springframework.beans.factory.annotation.Autowired;19 import org.springframework.dao.EmptyResultDataAccessException;20 import org.springframework.ldap.core.ContextMapper;21 import org.springframework.ldap.core.DirContextAdapter;22 import org.springframework.ldap.core.DirContextOperations;23 import org.springframework.ldap.core.DistinguishedName;24 import org.springframework.ldap.core.LdapTemplate;25 import org.springframework.ldap.core.support.AbstractContextMapper;26 import org.springframework.ldap.filter.AndFilter;27 import org.springframework.ldap.filter.EqualsFilter;28 import org.springframework.ldap.filter.OrFilter;29 import org.springframework.stereotype.Component;30 import com.surekam.model.Person;31 import com.surekam.utils.DateCl;32 import com.surekam.utils.StringUtil;33

34 @Component35 public classUserDAOL {36 @Autowired37 privateLdapTemplate ldapTemplate;38

39 /**40 *41 * @Description: 添加42 *43 */

44 publicboolean addPerson(Person person) {45 boolean flag = false;46 Name dn =buildUDn(person.getUid());47 Attributes buildAddAttributes =buildAddAttributes(person);48 ldapTemplate.bind(dn, null, buildAddAttributes);49 flag = true;50 returnflag;51 }52

53 /**54 *55 * @Description: 修改56 *57 */

58 publicboolean updatePerson(Person person) {59 boolean flag = false;60 Name dn =buildUDn(person.getUid());61 ModificationItem[] modificationItem =buildModifyAttributes(person);62 ldapTemplate.modifyAttributes(dn, modificationItem);63 flag = true;64 returnflag;65 }66

67 /**68 *69 * 多条件获取用户70 *71 */

72 public ListgetPersonByOu(String ou, String level) {73 AndFilter filter = newAndFilter();74 OrFilter orFilter1 = newOrFilter();75 if ("处级干部".equals(level)) {76 orFilter1.or(new EqualsFilter("cuadministrativelevels", "aa"));77 orFilter1.or(new EqualsFilter("cuadministrativelevels", "bb"));78 orFilter1.or(new EqualsFilter("cuadministrativelevels", "cc"));79 orFilter1.or(new EqualsFilter("cuadministrativelevels", "dd"));80 }81 if ("普通员工".equals(level)) {82 orFilter1.or(new EqualsFilter("cuadministrativelevels", "ee"));83 orFilter1.or(new EqualsFilter("cuadministrativelevels", "ff"));84 orFilter1.or(new EqualsFilter("cuadministrativelevels", "gg"));85 }86 OrFilter orFilter2 = newOrFilter();87 orFilter2.or(new EqualsFilter("departmentnumber", ou));88 orFilter2.or(new EqualsFilter("cutransferdnumber", ou));89 filter.and(orFilter2);90 filter.and(orFilter1);91 System.out.println(filter.toString());92 List list = ldapTemplate.search("cn=users,dc=hq", filter93 .encode(), newPersonContextMapper());94 returnlist;95 }96

97

98 /**99 *100 * 生成用户DN101 *102 * @return uid=123455,cn=users,dc=hq103 */

104 privateName buildUDn(String urdn) {105 DistinguishedName dn = new DistinguishedName("");106 dn.add("dc", "hq");107 dn.add("cn", "users");108 dn.add("uid", urdn);109 returndn;110 }111

112 /**113 *114 * 组织查询结果115 *116 */

117 public static classPersonContextMapper implements ContextMapper {118 publicObject mapFromContext(Object ctx) {119 if (ctx == null)120 return newPerson();121 DirContextAdapter context =(DirContextAdapter) ctx;122 Person per = newPerson();123 Attributes attrs =context.getAttributes();124 NamingEnumeration results =attrs.getAll();125 Class c =per.getClass();126 while(results.hasMoreElements()) {127 try{128 Attribute attr =(Attribute) results.next();129 String value = attr.get().toString();130 if(StringUtil.isNotEmpty(value)) {131 String fieldName =attr.getID();132 if ("objectclass".equals(fieldName.toLowerCase())) {133 continue;134 }135 Field field =c.getDeclaredField(fieldName136 .toLowerCase());137 Class fieldClazz =field.getType();138 /*

139 * 如果属性条数大于1,那就是多值属性 attr.getAttributeDefinition()140 * 获取多值属性的方法没找到141 */

142 if (fieldClazz.isAssignableFrom(List.class)) { //属性值数大于1

143 NamingEnumeration values = attr.getAll(); //获取所有值144 //LDAP中的多值属性只会是String类型

145 List list = new ArrayList();146 while(values.hasMoreElements()) {147 list.add(values.next().toString());148 }149 BeanUtils.setProperty(per, fieldName.toLowerCase(),150 list);151 } else{152 //普通属性

153 BeanUtils.setProperty(per, fieldName.toLowerCase(),154 value);155 }156 }157 } catch(IllegalAccessException e) {158 e.printStackTrace();159 } catch(InvocationTargetException e) {160 e.printStackTrace();161 } catch(NamingException e) {162 e.printStackTrace();163 } catch(SecurityException e) {164 //TODO Auto-generated catch block

165 e.printStackTrace();166 } catch(NoSuchFieldException e) {167 //TODO Auto-generated catch block

168 e.printStackTrace();169 }170 }171 per.setDn(context.getNameInNamespace());172 returnper;173 }174 }175

176 /**177 *178 * 组织添加数据数据179 *180 */

181 @SuppressWarnings("unchecked")182 privateAttributes buildAddAttributes(Person p) {183 Attributes attrs = newBasicAttributes();184 BasicAttribute ocattr = new BasicAttribute("objectclass");185 ocattr.add("top");186 ocattr.add("person");187 ocattr.add("organizationalPerson");188 ocattr.add("inetOrgPerson");189 ocattr.add("FJTicPerson");190 attrs.put(ocattr);191 Class c =p.getClass();192 Field[] fields =c.getDeclaredFields();193 for (int i = 0; i < fields.length; i++) {194 try{195 Class fieldClazz =fields[i].getType();196 String fieldName = fields[i].getName(); //获得属性名

197 String fieldVlue = BeanUtils.getProperty(p, fieldName); //获得属性值

198 /*

199 * 判断属性是否要过滤,例如修改时间之类的字段LDAP是没有的 判断属性值是否为空,在这里过滤了所有null和""200 * 增加操作中不存在主动设置某个值为空的情况 所以只需要处理有值属性201 */

202 if (checkfieldName(fieldName) ||StringUtil.isEmpty(fieldVlue))203 continue;204 /*

205 * 多值属性的处理 如果多值属性为空,那么增加的时候就不会增加值进去206 */

207 if (fieldClazz.isAssignableFrom(List.class)) { //集合属性

208 BasicAttribute ocattr1 = newBasicAttribute(fieldName);209 PropertyDescriptor pd = newPropertyDescriptor(fieldName, c);210 Method getMethod = pd.getReadMethod();//获得get方法

211 List list = (List) getMethod.invoke(p);//执行get方法返回一个Object

212 for (Object object: list) {213 ocattr1.add(object);214 }215 attrs.put(ocattr1);216 } else{217 attrs.put(fieldName, fieldVlue);218 }219 } catch(Exception e) {220 e.printStackTrace();221 }222 }223 returnattrs;224

225 }226

227 /**228 *229 * 组织修改数据230 *231 */

232 @SuppressWarnings("unchecked")233 privateModificationItem[] buildModifyAttributes(Person p) {234 ArrayList attrs = new ArrayList();235 Class c =p.getClass();236 Field[] fields =c.getDeclaredFields();237 for(Field field : fields) {238 try{239 Class fieldClazz =field.getType();240 String fieldName = field.getName(); //获得属性名

241 String fieldValue =BeanUtils.getProperty(p, fieldName);242 /*

243 * 判断属性是否要过滤,例如修改时间之类的字段LDAP是没有的 判断属性值是否为空,在这里过滤了所有null和""244 * 要置空的属性通过识别特殊属性值:delAtr 在后面做重新置空操作245 */

246 if (checkfieldName(fieldName) ||StringUtil.isEmpty(fieldValue))247 continue;248 BasicAttribute basicAttr = newBasicAttribute(fieldName);249 /*

250 * 多值属性的处理 如果传递一个空的list,那么修改的时候就会清空多值属性 (new ArrayList())251 */

252 if (fieldClazz.isAssignableFrom(List.class)) { //如果是集合属性

253 PropertyDescriptor pd = newPropertyDescriptor(fieldName, c);254 Method getMethod = pd.getReadMethod();//获得get方法

255 List list = (List) getMethod.invoke(p);//执行get方法返回一个Object

256 for (Object object: list) {257 basicAttr.add(object);258 }259 } else{260 /*

261 * 判断删除标记来对值进行置空 传递过来的对象中有些属性没有做修改就传递了"" 有些是要修改为 ""262 * 所以在上面要过滤所有 "" 属性,避免将不修改的参数全都置空了 然后通过识别要修改参数的特有值来判断是否主动置空263 * 如果add一个""进去,那么在LDAP中依然会显示264 * 如果不给值,由BasicAttribute自动授予空值,那么在LDAP中就不显示了265 */

266 if ("delAtr".equals(fieldValue)) {267 basicAttr.add(""); //置空属性

268 } else{269 basicAttr.add(fieldValue);//有值属性

270 }271 }272 //替换条目

273 attrs.add(newModificationItem(274 DirContextAdapter.REPLACE_ATTRIBUTE, basicAttr));275 } catch(Exception e) {276 e.printStackTrace();277 }278 }279 return attrs.toArray(newModificationItem[attrs.size()]);280 }281

282 /**283 *284 * 过滤默认值字段285 *286 */

287 private staticboolean checkfieldName(String fieldName) {288 String[] check = new String[] { "id", "status", "createtime",289 "updatetime", "dn"};290 for (int i = 0; i < check.length; i++) {291 if(check[i].equalsIgnoreCase(fieldName))292 return true;293 }294 return false;295 }296 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LDAP(Lightweight Directory Access Protocol)是一种轻量级目录访问协议,旨在提供对目录服务的快速访问。在Java中,可以通过使用JNDI(Java Naming and Directory Interface)API来操作LDAP。 下面是一些Java操作LDAP的常见步骤和示例代码: 1.创建连接对象 ``` Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "password"); DirContext ctx = new InitialDirContext(env); ``` 2.查询数据 ``` String base = "ou=people,dc=example,dc=com"; String filter = "(objectclass=person)"; SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<SearchResult> results = ctx.search(base, filter, sc); while (results.hasMore()) { SearchResult sr = (SearchResult) results.next(); // 处理查询结果 } ``` 3.添加数据 ``` Attributes attrs = new BasicAttributes(); attrs.put("cn", "John Smith"); attrs.put("sn", "Smith"); attrs.put("givenName", "John"); attrs.put("mail", "john.smith@example.com"); attrs.put("userPassword", "password"); ctx.createSubcontext("cn=John Smith,ou=people,dc=example,dc=com", attrs); ``` 4.修改数据 ``` ModificationItem[] mods = new ModificationItem[1]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", "new-email@example.com")); ctx.modifyAttributes("cn=John Smith,ou=people,dc=example,dc=com", mods); ``` 5.删除数据 ``` ctx.destroySubcontext("cn=John Smith,ou=people,dc=example,dc=com"); ``` 这些是Java操作LDAP的一些基本步骤和示例代码,但实际应用中还需要考虑LDAP服务器的具体配置和授权等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值