2023年6月21日发(作者:)

SpringLDAPTemplate连接LDAP数据库demostep1、创建maven⼯程step2、修改pom⽂件代码如下:step3、创建LDAP的配置⽂件代码如下:

step4、创建LDAP接⼝LdapPersonFactory代码如下:package y;import athXmlApplicationContext;import rsonDao;/** * 需要考虑线程安全 * @author ki_boy@ * */public class LdapPersonFactory { private static LdapPersonDao ldapPersonDao = null; public static LdapPersonDao getPersonDao(){ if (ldapPersonDao == null){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {""}); ldapPersonDao = (LdapPersonDao)n("ldapPerson"); } return ldapPersonDao; }}step5、创建DAO接⼝LdapPersonDao代码如下:package ;import ;import Exception;import ;/** * Data Access Object interface for the Person entity. *

* @author ki_boy *

*/public interface LdapPersonDao { void create(Person person) throws NamingException; void update(Person person) throws NamingException; void delete(Person person) throws NamingException; List getAllPersonNames() throws NamingException; List findAll() throws NamingException; boolean isUserCnExist(String cn);}step6、创建DAO接⼝的实现LdapPersonDaoImpl代码如下:package ;import ;import ;import Exception;import utes;import ttribute;import text;import cationItem;import Controls;import ;import Factory;import tFoundException;import esultsCookie;import esultsDirContextProcessor;import utesMapper;import tingNameClassPairCallbackHandler;import tMapper;import tMapperCallbackHandler;import textAdapter;import guishedName;import mplate;import ntextSource;import ter;import Filter;import er;import paceWildcardsFilter;import ;public class LdapPersonDaoImpl implements LdapPersonDao { private LdapTemplate ldapTemplate; private static final Logger log = ger(); public void create(Person person) { Name dn = buildDn(person); DirContextAdapter context = new DirContextAdapter(dn); mapToContext(person, context, true); (dn, context, null); } public void update(Person person) { Name dn = buildDn(person); DirContextAdapter context = (DirContextAdapter) (dn); mapToContext(person, context, false); Attributes(dn, ificationItems()); } public void delete(Person person) { (buildDn(person)); } public List getAllPersonNames() { EqualsFilter filter = new EqualsFilter("objectclass", "localPerson"); return (_PATH, (), new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return ("cn").get(); } }); } public List findAll() { EqualsFilter filter = new EqualsFilter("objectclass", "localPerson"); return (_PATH, (), getContextMapper()); } public Person findByPrimaryKey(String cn) { Person p = (Person) ("cn=" + cn, getContextMapper()); return p; } private ContextMapper getContextMapper() { return new PersonContextMapper(); } private DistinguishedName buildDn(Person person) { return buildDn(()); } private DistinguishedName buildDn(String cn) { DistinguishedName dn = new DistinguishedName(); ("cn", cn); if (gEnabled()) ("dn=" + ng()); return dn; } /** * 添加所有属性 密码修改单独维护 *

* @param person * @param context */ private void mapToContext(Person person, DirContextAdapter context,boolean isCreate) { ributeValues("objectclass", new String[] { "top","person" }); ributeValues("objectclass", new String[] { "person","localPerson" }); ributeValue("cn", ()); ributeValue("uid", ()); ributeValue("sn", ()); if (isCreate) { ributeValue("userPassword", rPassword()); } ributeValue("userType", rType()); ributeValues("mail", l()); ributeValue("mobile", ile()); ributeValue("sex", ()); ributeValue("age", ()); } /** * 查询不要返回密码信息 */ private static class PersonContextMapper implements ContextMapper { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; Person person = new Person(); // ributes() 通过属性赋值 TODO 减少代码 (ingAttribute("cn")); (ingAttribute("sn")); (ingAttribute("uid")); // rPassword(ingAttribute("userPassword")); cription(ingAttribute("description")); rType(ingAttribute("userType")); (ingAttribute("age")); l(ingAttributes("mail")); ile(ingAttribute("mobile")); return person; } } public boolean isUserCnExist(String cn) { try { ("cn=" + cn); } catch (NameNotFoundException e) { (" isUserCnExist:" + se()); return false; } return true; } public void setLdapTemplate(LdapTemplate ldapTemplate) { mplate = ldapTemplate; }}step7、创建实体Bean(Person)这个根据⾃⼰的实际情况,如userName,userPassword,sex等字段,并⽣成相应的getter和setter⽅法。step8、创建测试类代码如下:package ;import ;import athXmlApplicationContext;import ;public class PersonDaoImplTest { /** * @param args */ public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {""}); LdapPersonDao ldapPerson = (LdapPersonDao)n("ldapPerson"); PersonDaoImplTest test = new PersonDaoImplTest(); //UniqueId(ldapPerson); eate(ldapPerson, "ki_boy"); } public void testCreate(LdapPersonDao ldapPerson,String cn) { Person person = new Person(); (cn); ("cc-sn"); rPassword("111111"); (cn); ("30"); ("⼥"); ile(""); l(new String[]{"123@","234@"}); try { (person); n("person create success"); } catch (Exception e) { tackTrace(); n("person create fail.." + se()); } }

}尊重原创,谢谢合作!

2023年6月21日发(作者:)

SpringLDAPTemplate连接LDAP数据库demostep1、创建maven⼯程step2、修改pom⽂件代码如下:step3、创建LDAP的配置⽂件代码如下:

step4、创建LDAP接⼝LdapPersonFactory代码如下:package y;import athXmlApplicationContext;import rsonDao;/** * 需要考虑线程安全 * @author ki_boy@ * */public class LdapPersonFactory { private static LdapPersonDao ldapPersonDao = null; public static LdapPersonDao getPersonDao(){ if (ldapPersonDao == null){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {""}); ldapPersonDao = (LdapPersonDao)n("ldapPerson"); } return ldapPersonDao; }}step5、创建DAO接⼝LdapPersonDao代码如下:package ;import ;import Exception;import ;/** * Data Access Object interface for the Person entity. *

* @author ki_boy *

*/public interface LdapPersonDao { void create(Person person) throws NamingException; void update(Person person) throws NamingException; void delete(Person person) throws NamingException; List getAllPersonNames() throws NamingException; List findAll() throws NamingException; boolean isUserCnExist(String cn);}step6、创建DAO接⼝的实现LdapPersonDaoImpl代码如下:package ;import ;import ;import Exception;import utes;import ttribute;import text;import cationItem;import Controls;import ;import Factory;import tFoundException;import esultsCookie;import esultsDirContextProcessor;import utesMapper;import tingNameClassPairCallbackHandler;import tMapper;import tMapperCallbackHandler;import textAdapter;import guishedName;import mplate;import ntextSource;import ter;import Filter;import er;import paceWildcardsFilter;import ;public class LdapPersonDaoImpl implements LdapPersonDao { private LdapTemplate ldapTemplate; private static final Logger log = ger(); public void create(Person person) { Name dn = buildDn(person); DirContextAdapter context = new DirContextAdapter(dn); mapToContext(person, context, true); (dn, context, null); } public void update(Person person) { Name dn = buildDn(person); DirContextAdapter context = (DirContextAdapter) (dn); mapToContext(person, context, false); Attributes(dn, ificationItems()); } public void delete(Person person) { (buildDn(person)); } public List getAllPersonNames() { EqualsFilter filter = new EqualsFilter("objectclass", "localPerson"); return (_PATH, (), new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return ("cn").get(); } }); } public List findAll() { EqualsFilter filter = new EqualsFilter("objectclass", "localPerson"); return (_PATH, (), getContextMapper()); } public Person findByPrimaryKey(String cn) { Person p = (Person) ("cn=" + cn, getContextMapper()); return p; } private ContextMapper getContextMapper() { return new PersonContextMapper(); } private DistinguishedName buildDn(Person person) { return buildDn(()); } private DistinguishedName buildDn(String cn) { DistinguishedName dn = new DistinguishedName(); ("cn", cn); if (gEnabled()) ("dn=" + ng()); return dn; } /** * 添加所有属性 密码修改单独维护 *

* @param person * @param context */ private void mapToContext(Person person, DirContextAdapter context,boolean isCreate) { ributeValues("objectclass", new String[] { "top","person" }); ributeValues("objectclass", new String[] { "person","localPerson" }); ributeValue("cn", ()); ributeValue("uid", ()); ributeValue("sn", ()); if (isCreate) { ributeValue("userPassword", rPassword()); } ributeValue("userType", rType()); ributeValues("mail", l()); ributeValue("mobile", ile()); ributeValue("sex", ()); ributeValue("age", ()); } /** * 查询不要返回密码信息 */ private static class PersonContextMapper implements ContextMapper { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; Person person = new Person(); // ributes() 通过属性赋值 TODO 减少代码 (ingAttribute("cn")); (ingAttribute("sn")); (ingAttribute("uid")); // rPassword(ingAttribute("userPassword")); cription(ingAttribute("description")); rType(ingAttribute("userType")); (ingAttribute("age")); l(ingAttributes("mail")); ile(ingAttribute("mobile")); return person; } } public boolean isUserCnExist(String cn) { try { ("cn=" + cn); } catch (NameNotFoundException e) { (" isUserCnExist:" + se()); return false; } return true; } public void setLdapTemplate(LdapTemplate ldapTemplate) { mplate = ldapTemplate; }}step7、创建实体Bean(Person)这个根据⾃⼰的实际情况,如userName,userPassword,sex等字段,并⽣成相应的getter和setter⽅法。step8、创建测试类代码如下:package ;import ;import athXmlApplicationContext;import ;public class PersonDaoImplTest { /** * @param args */ public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {""}); LdapPersonDao ldapPerson = (LdapPersonDao)n("ldapPerson"); PersonDaoImplTest test = new PersonDaoImplTest(); //UniqueId(ldapPerson); eate(ldapPerson, "ki_boy"); } public void testCreate(LdapPersonDao ldapPerson,String cn) { Person person = new Person(); (cn); ("cc-sn"); rPassword("111111"); (cn); ("30"); ("⼥"); ile(""); l(new String[]{"123@","234@"}); try { (person); n("person create success"); } catch (Exception e) { tackTrace(); n("person create fail.." + se()); } }

}尊重原创,谢谢合作!