溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springboot+LDAP調(diào)研日志的方法是什么

發(fā)布時間:2021-11-15 15:41:13 來源:億速云 閱讀:202 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“Springboot+LDAP調(diào)研日志的方法是什么”,在日常操作中,相信很多人在Springboot+LDAP調(diào)研日志的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Springboot+LDAP調(diào)研日志的方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

LDAP

(一)概念

LDAP是輕量目錄訪問協(xié)議,英文全稱是Lightweight Directory Access Protocol,一般都簡稱為LDAP。它是基于X.500標準的,但是簡單得多并且可以根據(jù)需要定制。與X.500不同,LDAP支持TCP/IP,這對訪問Internet是必須的。LDAP的核心規(guī)范在RFC中都有定義,所有與LDAP相關(guān)的RFC都可以在LDAPman RFC網(wǎng)頁中找到。以上內(nèi)容來源于百度百科

(二)企業(yè)級LDAP場景

  1. 難題
    每個企業(yè)在運行過程中,會使用郵箱、考勤、CRM、ERP等系統(tǒng),每個系統(tǒng)都需要賬號去登錄認證,每一個新員工入職的時候,HR需要為其開通好多個系統(tǒng)賬號,一方面,需要開通的賬號比較多,員工離職的時候再挨個去將這些賬號凍結(jié),增加了HR的工作量;另一方面,員工自己擁有這么多賬號和密碼,管理起來也不是很方便,聰明的需要做一個personInfo.txt去維護了。
    這個時候搭建一個統(tǒng)一的賬號認證中心,使用一個賬號,可以到處登錄,然后在每個系統(tǒng)中去分配不同的權(quán)限即可,這樣就可以解決上述兩個問題。

  2. 為什么用LDAP認證

  • 是對讀操作進行優(yōu)化的種數(shù)據(jù)庫,讀操作效率高。

  • 可以靈活的改變數(shù)據(jù)類型,增加字段不會影響到查詢。

  • LDAP是個開放的標準協(xié)議,提供所有的程序語言的標準API接口

  • 由于LDAP數(shù)據(jù)庫的數(shù)據(jù)存儲是樹結(jié)構(gòu),分支可以單獨放到單臺服務器,能夠支持分布式、負載均衡、跨域等

  • LDAP支持強認證方式,可以達到很高的安全別。在國際化方面,LDAP使用了UTF-8編碼來存儲各種語言的字符

搭建OpenLDAP

先上官網(wǎng)鏈接 http://www.openldap.org/ 本人是在docker中啟動的,如果選擇在linux中啟動,可以參考 https://yq.aliyun.com/articles/549058 這篇帖子

docker方式啟動

如果對docker命令不是特別熟悉,本人自己另一篇帖子可供簡單了解 https://www.jianshu.com/p/af7977b1075c

  1. 拉取鏡像

 docker pull osixia/openldap:1.2.2
  1. 啟動鏡像

docker run -p 389:389 -p 689:689 --name my-openldap \
 --env LDAP_ORGANISATION="my-company" --env LDAP_DOMAIN="my-company.com" --env LDAP_ADMIN_PASSWORD="123456" --detach osixia/openldap:1.2.2
  1. 查看

docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS                                                 NAMES
d90a057443b0        osixia/openldap:1.2.2   "/container/tool/run"    47 hours ago        Up 47 hours                 0.0.0.0:389->389/tcp, 0.0.0.0:689->689/tcp, 636/tcp   my-openldap

可以看到我已經(jīng)啟動成功,映射出兩個端口,389和689,我們的主要操作就在389上

  1. 使用客戶端工具進行連接
    下載地址:http://directory.apache.org/studio
    Springboot+LDAP調(diào)研日志的方法是什么
    ConnectionName 是自己給這個連接起個容易記住的名字
    Hostname 是自己服務器IP地址,我是本地啟動的
    Port 是端口,默認都是389
    Springboot+LDAP調(diào)研日志的方法是什么
    AuthenticationMethod : Simple Authentication 簡單驗證
    Bind DN or User: cn=admin,dc=my_company,dc=com 之前設(shè)置的管理員用戶名
    Bind Password :設(shè)置的管理員密碼

LDAP相關(guān)概念

  1. 簡寫含義

屬性含義舉栗子
cCountry國家c=chinese
dcDomainComponent,常用來指一個域名的一部分dc=my_company,dc=com
cnCommonName,一個對象的名字,如果指人,使用全名cn=calvin
ouOrganizationalUnit 一個組織單元的名字ou = bj_develop(北京研發(fā)部)
snSurname,一個人的姓sn=趙、錢、孫、李
uidUserid,某個用戶的登錄名,與Linux系統(tǒng)中用戶的uid不同給一個唯一的ID
oOrganization 組織的名字o=develop
  1. 核心Attribute

名稱描述必要屬性
domain

organizationo
organizationalUnitou
personsn,cn
organizationPersoncn,sn
top抽象型,頂級ObjectClass
posixAccountLinux用戶cn,gidNumber,homeDirectory,uid,uidNumber
posixGroupLinux用戶組cn,gidNumber

以上資料來源https://cloud.tencent.com/developer/article/1444535

Springboot整合LDAP

經(jīng)過上面的安裝,我們就算是成功啟動了一個OpenLdap的服務,環(huán)境準備好了,接下來正式開始搭建項目

項目核心類構(gòu)建

  1. pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.calvin.ldap</groupId>
    <artifactId>ldap-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ldap-test</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- LDAP 相關(guān)配置-->
        <dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>ldapbp</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  1. application.yml

calvin:
  ldap:
    url: 'ldap://127.0.0.1:389'
    base: 'dc=my-company,dc=com'
    user_dn: 'cn=admin,dc=my-company,dc=com'
    password: '123456'
  1. LdapConfigruation.java

/**
 * <p>
 *     LDAP配置類
 * </p>
 * @author Calvin
 * @date 2019/10/14
 * @since 1.0
 */
@Configuration
public class LdapConfiguration {

    /**
     * 服務器地址
     */
    @Value("${calvin.ldap.url}")
    private String ldapUrl;

    /**
     * 公司、部門
     */
    @Value("${calvin.ldap.base}")
    private String baseDC;

    /**
     * 管理員用戶
     */
    @Value("${calvin.ldap.user_dn}")
    private String ldapUser;

    /**
     * 管理員密碼
     */
    @Value("${calvin.ldap.password}")
    private String ldapPassword;

    /**
     * LDAP環(huán)境配置
     * @return
     */
    @Bean
    public LdapContextSource ldapContextSource(){
        LdapContextSource source = new LdapContextSource();

        Map<String, Object> config = new HashMap<>();
        config.put("java.naming.ldap.attributes.binary", "objectGUID");

        source.setUrl(ldapUrl);
        source.setBase(baseDC);
        source.setPassword(ldapPassword);
        source.setUserDn(ldapUser);
        source.setPooled(true);
        source.setBaseEnvironmentProperties(config);

        return source;
    }

    /**
     * LDAP操作類的Bean定義
     * @return
     */
    @Bean
    public LdapTemplate ldapTemplate(){
        LdapTemplate ldapTemplate = new LdapTemplate();
        ldapTemplate.setContextSource(ldapContextSource());
        return ldapTemplate;
    }
}
  1. JSONObjectMapper.java

/**
 * <p>
 *     JSONObjectMapper,轉(zhuǎn)換類,將Attributes轉(zhuǎn)換成一個JSONObject方便接收打印
 * </p>
 *
 * @author Calvin
 * @date 2019/10/17
 * @since
 */
public class JSONObjectMapper implements AttributesMapper<JSONObject> {
    @Override
    public JSONObject mapFromAttributes(Attributes attributes) throws NamingException {
        NamingEnumeration<? extends Attribute> all = attributes.getAll();
        JSONObject jsonObject = new JSONObject();
        while (all.hasMore()){
            Attribute next = all.next();
            jsonObject.put(next.getID(),next.get());
        }
        return jsonObject;
    }
}
  1. LdapTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class LdapTest {

    @Autowired
    private LdapTemplate ldapTemplate;

}

測試和簡單調(diào)用API

  1. 先查詢一下admin用戶,確保配置正確

/**
 * 查詢一下Admin用戶
 */
@Test
public void test1() {
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("cn", "admin"));
    List search = ldapTemplate.search("", filter.encode(), new JSONObjectMapper());
    search.forEach(System.out::println);
}

Springboot+LDAP調(diào)研日志的方法是什么
上圖中顯示已經(jīng)能正常查詢admin用戶,所以確定配置正確

  1. 開始創(chuàng)建一個組

/**
     * 添加一個組織
     */
    @Test
    public void test2(){
        BasicAttributes attributes = new BasicAttributes();
        BasicAttribute objectClass = new BasicAttribute("objectClass");
        objectClass.add("organizationalUnit");
        objectClass.add("top");
        attributes.put(objectClass);
        attributes.put("description","this is develop dept");
        LdapNameBuilder nameBuilder = LdapNameBuilder.newInstance();
        nameBuilder.add("ou","develop");
        ldapTemplate.bind(nameBuilder.build(),null, attributes);
    }

通過工具查看,就可以看到我們的組織已經(jīng)添加好了
Springboot+LDAP調(diào)研日志的方法是什么

  1. 在這個組中添加一個員工

/**
 * 增加一個員工
 */
@Test
public void test3(){
    //設(shè)置objectClass
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("person");
    objectClass.add("inetOrgPerson");
    objectClass.add("organizationalPerson");
    Attributes attr = new BasicAttributes();
    attr.put(objectClass);
    //設(shè)置其他屬性
    attr.put("cn", "Jack");
    attr.put("sn", "Ma");
    attr.put("description", "this is first Employee");
    attr.put("userPassword", DigestUtils.md5DigestAsHex("123456".getBytes()));
    attr.put("telephoneNumber", "138 8888 8888");
    //設(shè)置dn
    LdapNameBuilder nameBuilder = LdapNameBuilder.newInstance();
    nameBuilder.add("ou","develop");
    nameBuilder.add("uid","0000001");
    //bind方法即是添加一條記錄。
    ldapTemplate.bind(nameBuilder.build(), null, attr);
}

工具客戶端查看,員工已經(jīng)添加成功 Springboot+LDAP調(diào)研日志的方法是什么
接下來我們更換uid和cn,sn,多增加幾個員工
Springboot+LDAP調(diào)研日志的方法是什么

  1. 使用代碼查詢員工列表

/**
 * 查詢develop部門下的員工
 */
@Test
public void test4(){
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectClass", "person"));
    LdapQueryBuilder queryBuilder = LdapQueryBuilder.query();
    List search = ldapTemplate.search(queryBuilder.base("ou=develop").filter(filter), new JSONObjectMapper());
    search.forEach(System.out::println);
}

Springboot+LDAP調(diào)研日志的方法是什么

  1. 使用創(chuàng)建的用戶登錄

 /**
  * 嘗試登陸
  */
 @Test
 public void test5(){
     AndFilter filter = new AndFilter();
     filter.and(new EqualsFilter("objectClass", "person"));
     boolean isSuccess = ldapTemplate.authenticate("uid=0000001,ou=develop",
             filter.encode(), DigestUtils.md5DigestAsHex("123456".getBytes()));
     System.out.println(isSuccess);
 }

到此,關(guān)于“Springboot+LDAP調(diào)研日志的方法是什么”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI