欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot操作ldap全過程

 更新時(shí)間:2024年05月20日 10:30:40   作者:請(qǐng)君擇日再來  
這篇文章主要介紹了springboot操作ldap全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

以下可能不是特別深入講解,只是開發(fā)用到,淺談一下。盡量用數(shù)據(jù)庫的方式去對(duì)應(yīng)

LDAP:輕量級(jí)目錄訪問協(xié)議。

微軟的AD域,openldap之類的都可以通過LDAP協(xié)議去連接訪問。

ad(active diretory)活動(dòng)目錄;openldap就是ldap協(xié)議的具體實(shí)現(xiàn)。

在這里把他們當(dāng)成一個(gè)數(shù)據(jù)庫來看待,目錄存儲(chǔ)數(shù)據(jù)庫。

如下圖,就是openldap的層級(jí)圖。

1. 專有名詞

關(guān)于這一塊涉及到很多專有名詞這里簡(jiǎn)單談一下。

  • dc:表示域名的意思,就是一個(gè)域的名稱,可以當(dāng)成數(shù)據(jù)庫中的庫名來看待。
  • ou:組織單元,就像一層層的文件夾看待,這個(gè)ou可以有多個(gè)層級(jí),就是ou下還可以有ou,當(dāng)成數(shù)據(jù)庫中的表看待
  • cn:用戶名或服務(wù)器名。就相當(dāng)于數(shù)據(jù)庫某一行用戶信息的主鍵
  • dn:相當(dāng)于絕對(duì)路徑,由cn+ou+dc組成。如上圖alice的dn就是 cn=alice,ou=school,dc=demo,dc=com

每個(gè)用戶是有多個(gè)屬性值的,就好比數(shù)據(jù)庫中的字段,一行用戶有多個(gè)字段信息

2. springboot連接

導(dǎo)入依賴

        <!--ldap-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-ldap</artifactId>
        </dependency>

application.properties配置文件。這里連接的賬戶不是ldap目錄下管理的用戶,就是別用上述的alice這種用戶去連接。

#AD認(rèn)證
spring.ldap.urls=ldap://127.0.0.1:389
spring.ldap.username=cn=admin,dc=demo,dc=com
spring.ldap.password=123456
spring.ldap.base=ou=school,dc=demo,dc=com

自定義配置類

@Configuration
public class LdapConfig {
 
    @Value("${spring.ldap.urls}")
    private String ldapUrl;
    @Value("${spring.ldap.username}")
    private String userName;
    @Value("${spring.ldap.password}")
    private String password;
    
 
    @Bean
    public LdapContextSource ldapContextSource() {
        LdapContextSource source = new LdapContextSource();
        source.setUrl(ldapUrl);
        source.setUserDn(userName);
        source.setPassword(password);
        return source;
    }
 
    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(ldapContextSource());
    }
}

基本上如上配置就可以操作了

測(cè)試是否可以連接,直接調(diào)用接口,能成功連接就會(huì)輸出數(shù)據(jù),無法連接會(huì)出現(xiàn)認(rèn)證異常。

3. 按條件查詢

先上案例,查詢cn屬性值為alice的。這里沒有指定返回的屬性有哪些,所以返回的就是所有屬性。稍后再解釋Person和PersonMapper

Person就是一個(gè)普通實(shí)體類,就像平時(shí)查詢數(shù)據(jù)庫數(shù)據(jù)時(shí)都是用實(shí)體類接收數(shù)據(jù)

@Data
public class Person {
    private String cn;
    private String displayName;
    private String givenName;
    private String eMail;
    private String ou;
}

PersonMapper類就不一樣了,必須繼承AttributesMapper類,實(shí)現(xiàn)mapFromAttributes方法。

當(dāng)ldapTemplate通過條件篩選出數(shù)據(jù)時(shí),一行數(shù)據(jù)就會(huì)調(diào)用一次這個(gè)方法,attributes帶有查出的屬性值

@Slf4j
public class PersonMapper implements AttributesMapper<Person> {
 
    @Override
    public Person mapFromAttributes(Attributes attributes) throws NamingException {
        log.info(attributes.toString());
        Person result = new Person();
        result.setCn(attributes.get("cn").get().toString());
        return result;
    }
}

篩選方式有很多,必須有篩選條件才能查詢。

加上attributes(new String[]{"cn", "mail"}),就只會(huì)查詢出這兩個(gè)屬性值

LdapQuery query = LdapQueryBuilder.query()
                .base(base)
                .searchScope(SearchScope.SUBTREE)
                .attributes(new String[]{"cn", "mail"})
                .where("cn")
                .is("alice");

可以用filter篩選,也是cn屬性值為alice的篩選條件。

LdapQuery query = LdapQueryBuilder.query()
                .base(base)
                .searchScope(SearchScope.SUBTREE)
                .filter("(cn=alice)");

base下所有內(nèi)容帶有objectClass屬性,都會(huì)被查出來。

如果base自身也帶有,自身也會(huì)被查出來

LdapQuery query = LdapQueryBuilder.query()
                .base(base)
                .searchScope(SearchScope.SUBTREE)
                .filter("(objectClass=*)");

多條件篩選,cn為alice并且mail為123@qq.com的內(nèi)容。

LdapQuery query = LdapQueryBuilder.query()
                .base(base)
                .searchScope(SearchScope.SUBTREE)
                .filter("(&(cn=alice)(mail=123@qq.com))");

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論