ldaptemplate 查询


      最近利用空闲时间研究了一把LDAP,然后用spring进行了一些编程尝试,通过spring的LdapTemplate可以很方便的进行LDAP的CRUD操作。如果你不清楚啥是LDAP的话,可以查询相关资料后再看此文。一般来说LDAP可以用来作为一个用户中心,围绕LDAP可以部署一些应用来共享相同的账号,这个在企业管理中是非常有帮助的,因为企业的内部应用可能是几个到几十个,员工如果有统一的账号密码,那将非常方便。

      我也是因为内部需要,围绕LDAP做了一些应用集成,使用的LDAP服务器是apache的DS,标准的LDAP协议,客户端编程用java,使用spring的LdapTemplate类进行操作。使用maven管理的话,在项目中加入如下依赖:

 
   
  1. <dependency>
  2. <groupId>com.sun </groupId>
  3. <artifactId>ldapbp </artifactId>
  4. <version>1.0 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework </groupId>
  8. <artifactId>spring-ldap </artifactId>
  9. <version>1.1.2 </version>
  10. </dependency>

      然后就可以使用LDAP进行操作了,当然了也要加入其它的spring对应的包,比如core包等,下面分别说明使用spring的LdapTemplate如何进行操作。注意,在应用之前请先配置好apache-DS服务(请参考文章xxx)。

1、初始化LdapTemplate

 
   
  1. private static final LdapTemplate template ;
  2.  
  3. static {
  4. LdapContextSource cs = new LdapContextSource ( ) ;
  5. cs. setCacheEnvironmentProperties ( false ) ;
  6. cs. setUrl ( "ldap://192.168.1.188:10389" ) ;
  7. cs. setBase ( "dc=mzone,dc=cc" ) ;
  8. cs. setAuthenticationSource ( new AuthenticationSource ( ) {
  9. @Override
  10. public String getCredentials ( ) {
  11. return "mzonecc" ;
  12. }
  13.  
  14. @Override
  15. public String getPrincipal ( ) {
  16. return "uid=admin,ou=system" ;
  17. }
  18. } ) ;
  19. template = new LdapTemplate (cs ) ;
  20. }

      初始化时主要是设置连接地址、基础目录(我们这里是dc=mzone,dc=cc)和认证信息(包括账号和密码)。注意认证信息中getCredentials返回的是密码信息,而getPrincipal方法返回的是账号DN(基于基础目录)。

2、查询(搜索)

      查询在LdapTemplate中是search说法,相关代码如下:

 
   
  1. public User getUserById ( String uid ) {
  2. String filter = "(&(objectclass=inetOrgPerson)(uid=" + uid + "))" ;
  3. List <User > list = template. search ( "ou=rd", filter, new AttributesMapper ( ) {
  4. @Override
  5. public Object mapFromAttributes ( Attributes attributes ) throws NamingException {
  6. User user = new User ( ) ;
  7.  
  8. Attribute a = attributes. get ( "cn" ) ;
  9. if (a != null ) user. setRealname ( ( String )a. get ( ) ) ;
  10.  
  11. a = attributes. get ( "uid" ) ;
  12. if (a != null ) user. setUsername ( ( String )a. get ( ) ) ;
  13.  
  14. return user ;
  15. }
  16. } ) ;
  17. if (list. isEmpty ( ) ) return null ;
  18. return list. get ( 0 ) ;
  19. }

      首先我们要构造一个filter,即search方法的第2个参数,这个filter是标准的LDAP查询过滤器,可以参考下LDAP的filter写法相关文档。

3、添加

 
   
  1. public boolean addUser (User vo ) {
  2. try {
  3. // 基类设置
  4. BasicAttribute ocattr = new BasicAttribute ( "objectClass" ) ;
  5. ocattr. add ( "top" ) ;
  6. ocattr. add ( "person" ) ;
  7. ocattr. add ( "uidObject" ) ;
  8. ocattr. add ( "inetOrgPerson" ) ;
  9. ocattr. add ( "organizationalPerson" ) ;
  10. // 用户属性
  11. Attributes attrs = new BasicAttributes ( ) ;
  12. attrs. put (ocattr ) ;
  13. attrs. put ( "cn", StringUtils. trimToEmpty (vo. getRealname ( ) ) ) ;
  14. attrs. put ( "sn", StringUtils. trimToEmpty (vo. getUsername ( ) ) ) ;
  15. attrs. put ( "displayName", StringUtils. trimToEmpty (vo. getRealname ( ) ) ) ;
  16. attrs. put ( "mail", StringUtils. trimToEmpty (vo. getEmail ( ) ) ) ;
  17. attrs. put ( "telephoneNumber", StringUtils. trimToEmpty (vo. getMobile ( ) ) ) ;
  18. attrs. put ( "title", StringUtils. trimToEmpty (vo. getTitle ( ) ) ) ;
  19. attrs. put ( "userPassword", StringUtils. trimToEmpty (vo. getPassword ( ) ) ) ;
  20. template. bind ( "uid=" + vo. getUsername ( ). trim ( ), null, attrs ) ;
  21. return true ;
  22. } catch ( Exception ex ) {
  23. ex. printStackTrace ( ) ;
  24. return false ;
  25. }
  26. }

      在LDAP中是没有添加这一说法的,标准的叫法是绑定,对应的删除就是解绑。绑定时要将所有必须属性都添加上,首先是objectClass属性,这个是LDAP中的对象,LDAP中的对象是继承的,每个对象都有一些特定的属性,有些属性是必须的,有些是可选的。第2个步骤就是将每个对象的必须属性和你想要的非必须属性填上交由LdapTemplate进行绑定即可。

4、更新

 
   
  1. public boolean updateUser (User vo ) {
  2. try {
  3. template. modifyAttributes ( "uid=" + vo. getUsername ( ). trim ( ), new ModificationItem [ ] {
  4. new ModificationItem ( DirContext. REPLACE_ATTRIBUTE, new BasicAttribute ( "cn", vo. getRealname ( ). trim ( ) ) ),
  5. new ModificationItem ( DirContext. REPLACE_ATTRIBUTE, new BasicAttribute ( "displayName", vo. getRealname ( ). trim ( ) ) ),
  6. new ModificationItem ( DirContext. REPLACE_ATTRIBUTE, new BasicAttribute ( "sn", vo. getUsername ( ). trim ( ) ) ),
  7. new ModificationItem ( DirContext. REPLACE_ATTRIBUTE, new BasicAttribute ( "mail", vo. getEmail ( ). trim ( ) ) ),
  8. new ModificationItem ( DirContext. REPLACE_ATTRIBUTE, new BasicAttribute ( "telephoneNumber", vo. getMobile ( ). trim ( ) ) ),
  9. new ModificationItem ( DirContext. REPLACE_ATTRIBUTE, new BasicAttribute ( "title", vo. getTitle ( ). trim ( ) ) )
  10. } ) ;
  11. return true ;
  12. } catch ( Exception ex ) {
  13. ex. printStackTrace ( ) ;
  14. return false ;
  15. }
  16. }

      更新就是替换属性,使用ModificationItem类进行处理。

5、删除

 
   
  1. public boolean deleteUser ( String username ) {
  2. try {
  3. template. unbind ( "uid=" + username. trim ( ) ) ;
  4. return true ;
  5. } catch ( Exception ex ) {
  6. ex. printStackTrace ( ) ;
  7. return false ;
  8. }
  9. }

      删除也就是解绑的过程,直接调用unbind即可。


      上面几个操作基本上就覆盖了LDAP的基本操作,对于查询可能更多的是要注意如何些filter,增加要确定objectClass。另外,如果在初始化LdapContextSource时设置了base,那么后面的LdapTemplate中所有的操作DN都是基于改base构建而成的全路径,这个要注意。其他来说,看看代码就知道怎么回事了,相对比较简单。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值