-
Spring LDAP,是Spring的一個組件,實現對LDAP的操作。
-
在編程操作MySQL時,我們除了用JDBC,可能都會選用一些框架,比如JbdcTemplate。
-
JdbcTemplate的實現是通過傳入sql語句和RowMapper,query返回目標列表,或是傳入sql和參數,執行update方法。JdbcTemplate的優點是簡化了與數據庫連接的代碼,以及避免了一些常見的錯誤。
-
同樣的,Spring LDAP框架也提供了類似的特性——LdapTemplate。
-
優點都是相通的,Spring LdapTemplate的優點是簡化了與LDAP交互的代碼。
-
按之前Spring配置JavaBean的方式,在xml文件配置LdapTemplate及其屬性值即可,本文將演示使用Springboot 用Java代碼的方式定義LdapTemplate,完成Spring ldap連接數據庫服務及進行相關操作。
下面是使用Spring-ldap的依賴
<!-- spring ldapTemplate操作 --> <dependency> <groupId>com.sun</groupId> <artifactId>ldapbp</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.ldap</groupId> <artifactId>spring-ldap-core</artifactId> <version>2.3.2.RELEASE</version> </dependency>
-
先介紹一些Spring-ldap,因為網上有很多教程,在給出的工程依賴中有用spring-ldap的,也有spring-ldap-core的,而且還有版本問題。筆者使用目前最新的spring-ldap-2.3.2.RELEASE。推薦直接使用,這個最新版本。
-
spring-ldap框架,是Spring集成ldap操作的總和,包含spring-ldap-core,spring-ldap-core-tiger,spring-ldap-ldif-core,spring-ldap-odm等jar,而通常我們在工程中只需要引入spring-ldap-core即可,它提供了絕大部分功能。而且截至目前,spring-ldap的<version>2.3.2.RELEASE</version>不在maven的中央倉庫,不好獲取。但spring-ldap-core在。
-
另外,Spring LDAP 2.0對jdk版本要求是1.6,並且開始支持ODM,並后來引入Spring ldap pool連接池。
-
據本人嘗試,這些版本之間,變化差異很大。在新版本中可能有些關鍵的核心類,都會被移動到不同的package下;一些老版本完成的愚鈍功能,可能在新版本中有了更好的實現或支持,所以在新版本中,一些“愚鈍”實現可能會被移除。
-
比如LdapTemplate,原先在org.springframework.ldap包,在最新版本被移至core包。在spring-ldap-core的<version>2.0.2.RELEASE</version>版本中支持類似於JPA方式的LdapRepository,但在2.3.2最新版本中,完全被移除,但是新版本增強的LdapTemplate,使得LdapTemplate功能更強大,可以完全替代LdapRepository。
下面是用Java代碼的方式定義LdapTemplate,完成用Spring ldap連接LDAP服務器
import com.cvte.csb.sim.ldap.constants.LdapConstans; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.pool.factory.PoolingContextSource; import org.springframework.ldap.pool.validation.DefaultDirContextValidator; import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy; import java.util.HashMap; import java.util.Map; /** * LDAP 的自動配置類 * * 完成連接 及LdapTemplate生成 */ @Configuration public class LdapConfiguration { private LdapTemplate ldapTemplate; @Bean public LdapContextSource contextSource() { LdapContextSource contextSource = new LdapContextSource(); Map<String, Object> config = new HashMap(); contextSource.setUrl(LdapConstans.LDAP_URL); contextSource.setBase(LdapConstans.BASE_DC); contextSource.setUserDn(LdapConstans.USER_NAME); contextSource.setPassword(LdapConstans.PASS_WORD); // 解決 亂碼 的關鍵一句 config.put("java.naming.ldap.attributes.binary", "objectGUID"); contextSource.setPooled(true); contextSource.setBaseEnvironmentProperties(config); return contextSource; } @Bean public LdapTemplate ldapTemplate() { if (null == ldapTemplate) ldapTemplate = new LdapTemplate(contextSource()); return ldapTemplate; } }
- 完成LdapTemplate的bean定義,是最關鍵的一步。因為后續的操作,對於LDAP目錄樹的CRUD操作,全都靠它完成。
- 通過上面的代碼,在IOC容器完成bean的定義,我們在外部就可以注入使用LdapTemplate了。
下面給出用LdapTemplate完成CRUD功能:
import com.cvte.csb.sim.ldap.attribute.LdapDeptAttributeMapper; import com.cvte.csb.sim.ldap.attribute.LdapUserAttributeMapper; import com.cvte.csb.sim.ldap.module.dto.LdapUser; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.filter.AndFilter; import org.springframework.ldap.filter.EqualsFilter; import java.util.List; public class ConfigTest extends BaseTest { @Autowired private LdapTemplate ldapTemplate; /** * 獲取所有 內部人員 * ou=Internal,ou=People */ @Test public void listUsers(){ AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectClass", "person")); //查詢所有內部人員 List<LdapUser> users = ldapTemplate.search("ou=Internal,ou=People", filter.encode(), new LdapUserAttributeMapper()); for (LdapUser user: users ) { System.out.println(user); } // Assert.assertEquals(3056, users.size()); } /** * 根據uid 查找單個人員 */ @Test public void findUser(){ //uid=00012047,ou=Internal,ou=People,o=cvte.com,o=isp DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("uid=00012047,ou=Internal,ou=People");//BASE_DC 不用填 System.out.println(obj); } /** * 根據部門編號o,查找部門 */ @Test public void findDept(){ //o=598b09cb12ab4364864d8ac73ecee00d,ou=Organizations,ou=People,o=cvte.com,o=isp DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("o=598b09cb12ab4364864d8ac73ecee00d,ou=Organizations");//BASE_DC 不用填 System.out.println(obj); } @Test public void listDepts(){ AndFilter filter = new AndFilter(); filter.and(new EqualsFilter("objectClass", "organization")); //search是根據過濾條件進行查詢,第一個參數是父節點的dn,可以為空,不為空時查詢效率更高 List depts = ldapTemplate.search("", filter.encode(), new LdapDeptAttributeMapper()); System.out.println(depts.size()); // Assert.assertEquals(3056, depts.size()); } }
- 在ldap中,有兩個"查詢"概念,search和lookup。search是ldaptemplate對每一個entry進行查詢,lookup是通過DN直接找到某個條目。
- 在Ldap中,新增與刪除叫做綁定bind和解綁unBind。這些方法LdapTemplate全部提供,並且還提供各種條件過濾等方法,不如findAll(),list()等。
我們注意到,findAll(),list()肯定是返回一個java.util.List<T>,包括,
//查詢所有內部人員 List<LdapUser> users = ldapTemplate.search("ou=Internal,ou=People", filter.encode(), new LdapUserAttributeMapper());
也是返回列表,列表里裝的是查詢出來的結果。但是上一篇文章用JNDI方式查詢出來的是
Attributes attrs = ctx.getAttributes("uid=00012047,ou=Internal,ou=People");//獲取到一個人員
Spring-ldap是基於JNDI實現的封裝,那是哪里實現的把Attributes轉成我們需要的Java Bean對象呢?
答案在new LdapUserAttributeMapper(),這個接口實現了查詢結果到對象的轉化。
import com.cvte.csb.sim.ldap.module.dto.LdapUser; import org.springframework.ldap.core.AttributesMapper; import javax.naming.NamingException; import javax.naming.directory.Attributes; /** * 將ldap返回的結果,轉成指定對象 */ public class LdapUserAttributeMapper implements AttributesMapper { /** * 將單個Attributes轉成單個對象 * @param attrs * @return * @throws NamingException */ public Object mapFromAttributes(Attributes attrs) throws NamingException { LdapUser user = new LdapUser(); if(attrs.get("uid") != null){ user.setUsername( attrs.get("uid").get().toString()); } if(attrs.get("cn") != null){ user.setUserCn( attrs.get("cn").get().toString()); } if(attrs.get("mobile") != null){ user.setMobile( attrs.get("mobile").get().toString()); } if(attrs.get("mail") != null){ user.setMail( attrs.get("mail").get().toString()); } if(attrs.get("employeeNumber") != null){ user.setUserNumber( attrs.get("employeeNumber").get().toString()); } if(attrs.get("type") != null){ user.setUserType( attrs.get("type").get().toString()); } if(attrs.get("py") != null){ user.setPinyin(attrs.get("py").get().toString()); } if(attrs.get("alias") != null){ user.setAlias(attrs.get("alias").get().toString()); } if(attrs.get("departmentNumber") != null){ user.setDeptId(attrs.get("departmentNumber").get().toString()); } if(attrs.get("departmentName") != null){ user.setDeptName(attrs.get("departmentName").get().toString()); } if(attrs.get("jobname") != null){ user.setPositionName(attrs.get("jobname").get().toString()); } if(attrs.get("modifyTimestamp") != null){ user.setModifyTimestamp(attrs.get("modifyTimestamp").get().toString()); } return user; } }
可以看到轉化的過程非常繁瑣,無非就是拿JNDI查詢到的Attributes,不停的獲取屬性值,再設置到Java對象中;attrs.get("uid").get().toString()然后set。
那好了,在每次查詢的時候,要查詢到多少列,在這個AttributesMapper轉化方法中就要寫多少個,判斷及賦值。而且,如果因為業務不同,要查詢不同的列,那AttributesMapper接口的實現必須重新寫。那有沒有支持復用的方式呢?答案是肯定的。下節分享spring ldap ODM , Object-Directory Mapping。
spring-ldap-2.3.2.RELEASE所有jar包下載
http://download.csdn.net/download/ljheee/10150501
Spring-ldap最新版官方文檔:
https://docs.spring.io/spring-ldap/docs/2.3.2.RELEASE/reference/