Spring LDAP的使用

LDAP入門http://www.reibang.com/p/7e4d99f6baaf

  • Spring LDAP呻待,是Spring的一個組件,實現(xiàn)對LDAP的操作奏篙。

  • 在編程操作MySQL時迫淹,我們除了用JDBC,可能都會選用一些框架肺稀,比如JbdcTemplate应民。

  • JdbcTemplate的實現(xiàn)是通過傳入sql語句和RowMapper,query返回目標(biāo)列表稿静,或是傳入sql和參數(shù)改备,執(zhí)行update方法蔓倍。JdbcTemplate的優(yōu)點是簡化了與數(shù)據(jù)庫連接的代碼,以及避免了一些常見的錯誤默勾。

  • 同樣的聚谁,Spring LDAP框架也提供了類似的特性——LdapTemplate。

  • 優(yōu)點都是相通的环疼,Spring LdapTemplate的優(yōu)點是簡化了與LDAP交互的代碼朵耕。

  • 按之前Spring配置JavaBean的方式,在xml文件配置LdapTemplate及其屬性值即可伪阶,本文將演示使用Springboot 用Java代碼的方式定義LdapTemplate处嫌,完成Spring ldap連接數(shù)據(jù)庫服務(wù)及進(jìn)行相關(guān)操作锰霜。

下面是使用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,因為網(wǎng)上有很多教程厨剪,在給出的工程依賴中有用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連接池。

  • 據(jù)本人嘗試巫湘,這些版本之間,變化差異很大尚氛。在新版本中可能有些關(guān)鍵的核心類阅嘶,都會被移動到不同的package下;一些老版本完成的愚鈍功能,可能在新版本中有了更好的實現(xiàn)或支持宪巨,所以在新版本中捏卓,一些“愚鈍”實現(xiàn)可能會被移除。

  • 比如LdapTemplate遥金,原先在org.springframework.ldap包蒜田,在最新版本被移至core包。在spring-ldap-core的<version>2.0.2.RELEASE</version>版本中支持類似于JPA方式的LdapRepository美莫,但在2.3.2最新版本中梯捕,完全被移除傀顾,但是新版本增強(qiáng)的LdapTemplate,使得LdapTemplate功能更強(qiáng)大寒砖,可以完全替代LdapRepository嫉拐。

下面是用Java代碼的方式定義LdapTemplate,完成用Spring ldap連接LDAP服務(wù)器
import com.xxx.xxx.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.url);
        contextSource.setBase(LdapConstans.BASE_DC);
        contextSource.setUserDn(LdapConstans.username);
        contextSource.setPassword(LdapConstans.password);

        //  解決 亂碼 的關(guān)鍵一句
        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定義孝偎,是最關(guān)鍵的一步忱叭。因為后續(xù)的操作,對于LDAP目錄樹的CRUD操作侯谁,全都靠它完成章钾。
  • 通過上面的代碼,在IOC容器完成bean的定義惨撇,我們在外部就可以注入使用LdapTemplate了府寒。
下面給出用LdapTemplate完成CRUD功能:
import com.xxx.xxx.sim.ldap.attribute.LdapDeptAttributeMapper;
import com.xxx.xxx.ldap.attribute.LdapUserAttributeMapper;
import com.xxx.xxx.xxx.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;

    /**
     * 獲取所有 internal人員
     * ou=Internal,ou=People
     */
    @Test
    public void listUsers(){
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectClass", "person"));

        //查詢所有內(nèi)部人員
        List<LdapUser> users = ldapTemplate.search("ou=internal,ou=People", filter.encode(), new LdapUserAttributeMapper());
        for (LdapUser user: users ) {
            System.out.println(user);
        }

//        Assert.assertEquals(123, users.size());
    }

    /**
     * 根據(jù)userid 查找單個人員
     */
    @Test
    public void findUser(){

        //uid=123,ou=internal,ou=People,o=xxx.com,o=xxx
        DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("uid=123,ou=internal,ou=People");//BASE_DC 不用填
        System.out.println(obj);
    }

    /**
     * 根據(jù)部門編號o剖淀,查找部門
     */
    @Test
    public void findDept(){
        //o=abcd,ou=Organizations,ou=People,o=xxx.com,o=xxx
        DirContextAdapter obj = (DirContextAdapter) ldapTemplate.lookup("o=abcd,ou=Organizations");//BASE_DC 不用填
        System.out.println(obj);
    }

    @Test
    public void listDepts(){
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectClass", "organization"));
        //search是根據(jù)過濾條件進(jìn)行查詢纵隔,第一個參數(shù)是父節(jié)點的dn炮姨,可以為空,不為空時查詢效率更高
        List depts = ldapTemplate.search("", filter.encode(), new LdapDeptAttributeMapper());
        System.out.println(depts.size());
//        Assert.assertEquals(305600, depts.size());
    }
}
  • 在ldap中糊啡,有兩個"查詢"概念吁津,search和lookup。search是ldaptemplate對每一個entry進(jìn)行查詢碍脏,lookup是通過DN直接找到某個條目典尾。
  • 在Ldap中,新增與刪除叫做綁定bind和解綁unBind河闰。這些方法LdapTemplate全部提供,并且還提供各種條件過濾等方法姜性,不如findAll(),list()等部念。

我們注意到,findAll(),list()肯定是返回一個java.util.List<T>妓湘,包括乌询,

//查詢所有internal人員
List<LdapUser> users = ldapTemplate.search("ou=internal,ou=People", filter.encode(), new LdapUserAttributeMapper());

也是返回列表楣责,列表里裝的是查詢出來的結(jié)果聂沙。但是上一篇文章用JNDI方式查詢出來的是
Attributes attrs = ctx.getAttributes("uid=123,ou=Internal,ou=People");//獲取到一個人員
Spring-ldap是基于JNDI實現(xiàn)的封裝,那是哪里實現(xiàn)的把Attributes轉(zhuǎn)成我們需要的Java Bean對象呢沮趣?
答案在new LdapUserAttributeMapper()坷随,這個接口實現(xiàn)了查詢結(jié)果到對象的轉(zhuǎn)化温眉。

import com.xxx.csb.sim.ldap.module.dto.LdapUser;
import org.springframework.ldap.core.AttributesMapper;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;

/**
 * 將ldap返回的結(jié)果,轉(zhuǎn)成指定對象
 */
public class LdapUserAttributeMapper implements AttributesMapper {

    /**
     * 將單個Attributes轉(zhuǎn)成單個對象
     * @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;
    }
}

可以看到轉(zhuǎn)化的過程非常繁瑣,無非就是拿JNDI查詢到的Attributes砂心,不停的獲取屬性值蛇耀,再設(shè)置到Java對象中;attrs.get("uid").get().toString()然后set译暂。

那好了,在每次查詢的時候褐桌,要查詢到多少列象迎,在這個AttributesMapper轉(zhuǎn)化方法中就要寫多少個砾淌,判斷及賦值。而且赃春,如果因為業(yè)務(wù)不同劫乱,要查詢不同的列,那AttributesMapper接口的實現(xiàn)必須重新寫狭吼。那有沒有支持復(fù)用的方式呢殖妇?答案是肯定的谦趣。下節(jié)分享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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市舰绘,隨后出現(xiàn)的幾起案子除盏,更是在濱河造成了極大的恐慌,老刑警劉巖窃祝,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件粪小,死亡現(xiàn)場離奇詭異,居然都是意外死亡探膊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來腌闯,“玉大人姿骏,你說我怎么就攤上這事≌盒海” “怎么了嘲玫?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵趁冈,是天一觀的道長拜马。 經(jīng)常有香客問我,道長旺坠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任扮超,我火速辦了婚禮取刃,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘出刷。我一直安慰自己璧疗,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布馁龟。 她就那樣靜靜地躺著崩侠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪坷檩。 梳的紋絲不亂的頭發(fā)上却音,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天改抡,我揣著相機(jī)與錄音系瓢,去河邊找鬼阿纤。 笑死,一個胖子當(dāng)著我的面吹牛夷陋,可吹牛的內(nèi)容都是我干的欠拾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼肌稻,長吁一口氣:“原來是場噩夢啊……” “哼清蚀!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起爹谭,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤枷邪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后诺凡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體东揣,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年腹泌,在試婚紗的時候發(fā)現(xiàn)自己被綠了嘶卧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡凉袱,死狀恐怖芥吟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情专甩,我是刑警寧澤钟鸵,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站涤躲,受9級特大地震影響棺耍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜种樱,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一蒙袍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嫩挤,春花似錦害幅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春叼风,著一層夾襖步出監(jiān)牢的瞬間取董,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工无宿, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留茵汰,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓孽鸡,卻偏偏與公主長得像蹂午,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子彬碱,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354