溫馨提示×

溫馨提示×

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

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

Spring Boot如何連接LDAP

發(fā)布時間:2021-07-08 14:02:10 來源:億速云 閱讀:283 作者:小新 欄目:編程語言

小編給大家分享一下Spring Boot如何連接LDAP,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

本文目的:使用Spring Boot構(gòu)建項目,幫助讀者快速配置并使用Spring LDAP操作LDAP。大致步驟如下:

1.創(chuàng)建Spring Boot項目(約1分鐘)

 2.添加pom.xml文件中Spring LDAP依賴(約1分鐘)

3.配置Spring LDAP連接信息(約1分鐘)

4.創(chuàng)建實體類作為LDAP中的entry映射(ODM映射功能,類似ORM)

5.使用ldapTemplate書寫service層的方法(約3分鐘)

6.編寫controller層(約3分鐘)

1.創(chuàng)建Spring Boot項目(約1分鐘)

IDEA中點擊file - new - project

Spring Boot如何連接LDAP

圖1

如上圖,選擇左側(cè)的 Spring Initializr幫助初始化spring項目,配置好SDK后,點擊next。

Spring Boot如何連接LDAP

圖2

點擊后,如圖2,如果只是做demo,該頁面默認(rèn)即可,點擊next。

Spring Boot如何連接LDAP

圖3

如圖3,我們選擇web,右側(cè)會顯示web相關(guān)的組件,我們選擇右側(cè)中的Web,將其前面的框勾選上。這代表在創(chuàng)建的spring boot項目中會引入web相關(guān)的依賴。點擊next。

Spring Boot如何連接LDAP

圖4

如圖4,這里自己命名即可,點擊finish。

2.添加pom.xml文件中Spring LDAP依賴(約1分鐘)

Spring Boot如何連接LDAP

圖5

如上圖圖5,在項目中雙擊pom.xml來添加依賴。

Spring Boot如何連接LDAP

圖6

如圖6所示,文件中已經(jīng)加載了spring-boot-starter-web依賴,我們要使用Spring LDAP來操作LDAP服務(wù)器需要添加spring-boot-starter-data-ldap。該依賴會自動加載spring-ldap-core 與 spring-data-ldap依賴。其中spring-ldap-core是ldap操作的核心依賴,而spring-data-ldap提供了ODM的功能,能夠簡化操作。我們可以在項目的External Libraries中看到這兩個依賴,如下圖圖7中三個黃色高亮處:

Spring Boot如何連接LDAP

圖7

3.配置Spring LDAP連接信息

Spring Boot如何連接LDAP

圖8

如上圖圖8,根據(jù)spring boot官網(wǎng)對ldap配置的說明來配置,可以看這里。這樣配置之后,spring boot會自動讀取該配置。

4.創(chuàng)建實體類作為LDAP中的entry映射

本例中使用ODM功能,極大的簡化了LDAP的操作,關(guān)于ODM更多的信息,可以參考翻譯的官方文檔。

我們在項目中創(chuàng)建如下結(jié)構(gòu):

Spring Boot如何連接LDAP

圖9

現(xiàn)在,我們在entry包下寫與entry互相映射的實體類。其中,我的LDAP結(jié)構(gòu)如下

Spring Boot如何連接LDAP

圖10

新建Person類

package com.example.demo.entry;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import org.springframework.ldap.support.LdapNameBuilder;
import javax.naming.Name;
/**
 * @Author: geng_pool
 * @Description:
 * @Date: Created in 2017/12/27 10:24
 * @Modified by:
 */
@Entry(objectClasses = {"organizationalPerson","person","top"},base = "o=myorg")
public class Person {
 @Id
 @JsonIgnore
 private Name dn;

 @Attribute(name="cn")
 private String cn;

 @Attribute(name="sn")
 private String sn;

 @Attribute(name="userPassword")
 private String userPassword;

 public Person(String cn) {
  Name dn = LdapNameBuilder.newInstance()
    .add("o", "myorg")
    .add("cn", cn)
    .build();
  this.dn = dn;
 }
 public Person(){}

 /* getter */
 public Name getDn() {
  return dn;
 }

 public String getCn() {
  return cn;
 }

 public String getSn() {
  return sn;
 }

 public String getUserPassword() {
  return userPassword;
 }

 /* setter */
 public void setDn(Name dn) {
  this.dn = dn;
 }

 public void setCn(String cn) {
  this.cn = cn;
  if(this.dn==null){
   Name dn = LdapNameBuilder.newInstance()
     .add("o", "myorg")
     .add("cn", cn)
     .build();
   this.dn = dn;
  }
 }

 public void setSn(String sn) {
  this.sn = sn;
 }

 public void setUserPassword(String userPassword) {
  this.userPassword = userPassword;
 }

 @Override
 public String toString() {
  return "Person{" +
    "dn=" + dn.toString() +
    ", cn='" + cn + '\'' +
    ", sn='" + sn + '\'' +
    ", userPassword='" + userPassword + '\'' +
    '}';
 }
}

注意@Entry與@Id為必須的。而@JsonIgnore是為了將person傳給前端時不報錯,因為Name類型的無法自動解析成json格式。注意我為了方便,在 public Person(String cn) {}構(gòu)造方法中寫上了DN值的生成方法,在setCn中也寫上了該方法,當(dāng)然存在代碼重復(fù)問題,忽略就好。

5.使用ldapTemplate書寫service層的方法

在service包中,新建OdmPersonRepo類

package com.example.demo.service;
import com.example.demo.entry.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;
import static org.springframework.ldap.query.LdapQueryBuilder.query;

/**
 * @Author: geng_pool
 * @Description:
 * @Date: Created in 2017/12/27 10:37
 * @Modified by:
 */
@Service
public class OdmPersonRepo {

 @Autowired
 private LdapTemplate ldapTemplate;

 public Person create(Person person){
  ldapTemplate.create(person);
  return person;
 }

 public Person findByCn(String cn){
  return ldapTemplate.findOne(query().where("cn").is(cn),Person.class);
 }

 public Person modifyPerson(Person person){
  ldapTemplate.update(person);
  return person;
 }

 public void deletePerson(Person person){
  ldapTemplate.delete(person);
 }

}

可以看到,基本的增刪改查操作都幫我們實現(xiàn)了,我們只要調(diào)用一下ldapTemplate中的方法即可。若要更自由的操作ldap的增刪改查,可參閱翻譯的官方文檔。

6.編寫controller層

在controller包下,新建一個testController類來測試LDAP的操作。

package com.example.demo.controller;

import com.example.demo.entry.Person;
import com.example.demo.service.OdmPersonRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: geng_pool
 * @Description:
 * @Date: Created in 2017/12/27 10:50
 * @Modified by:
 */
@RestController
public class testController {
 @Autowired
 private OdmPersonRepo odmPersonRepo;
 
 @RequestMapping(value = "/findOne",method = RequestMethod.POST)
 public Person findByCn(@RequestParam(name = "cn",required = true) String cn){
  return odmPersonRepo.findByCn(cn);
 }

 @PostMapping(value = "/create")
 public Person create(@RequestParam(name = "cn") String cn,@RequestParam(name = "sn") String sn,@RequestParam(name = "userPassword") String userPassworld){
  Person person = new Person();
  person.setCn(cn);
  person.setSn(sn);
  person.setUserPassword(userPassworld);
  return odmPersonRepo.create(person);
 }



 @PostMapping(value = "/update")
 public Person update(@RequestParam(name = "cn") String cn,@RequestParam(name = "sn") String sn,@RequestParam(name = "userPassword") String userPassworld){
  Person person = new Person();
  person.setCn(cn);
  person.setSn(sn);
  person.setUserPassword(userPassworld);
  return odmPersonRepo.modifyPerson(person);
 }

 @PostMapping(value = "/delete")
 public void delete(@RequestParam(name = "cn")String cn){
  Person person = new Person();
  person.setCn(cn);
  odmPersonRepo.deletePerson(person);
 }

}

至此,一個基本的demo完成啦。下面我們測試一下

測試

為了大家都能跟著步驟來,我就不使用Postman來測試,而是在瀏覽器中測試接口。、

啟動spring boot,沒有報錯的話,打開瀏覽器到 localhost:8080/ ,按下F12,彈出開發(fā)者模式,找到console控制臺方便我們發(fā)送測試語句。

首先,引入jquery.js。打開jquery.js,全選-復(fù)制-在console中粘貼-回車,如下圖:

Spring Boot如何連接LDAP

圖11

顯示為true,代表加載成功,我們可以使用jquery的ajax來測試了。

新增數(shù)據(jù)

Spring Boot如何連接LDAP

圖12

正如controller層的testController要求的那樣,我們在地址 /create 上使用post方法,將數(shù)據(jù)cn sn userPassword傳過去

Spring Boot如何連接LDAP

圖13

而在LDAP服務(wù)器中,也顯示了新增的數(shù)據(jù)

Spring Boot如何連接LDAP

圖14

查找數(shù)據(jù)

Spring Boot如何連接LDAP

圖15

也能根據(jù)cn正確查找到數(shù)據(jù)。

修改數(shù)據(jù)

Spring Boot如何連接LDAP

圖16

我們查看LDAP中是否修改

Spring Boot如何連接LDAP

圖17

可以看到能夠正常修改數(shù)據(jù)

刪除數(shù)據(jù)

 Spring Boot如何連接LDAP

圖18

查看LDAP中是否刪除

Spring Boot如何連接LDAP

圖19

可以看到,數(shù)據(jù)被正確刪除了。

其他說明

  1. 剛才的例子中,代碼有需要完善的地方,但對于demo演示來說完全可以忍受。大家可能也看到了這么做也有些缺點,我在update的時候,需要將修改后的person的所有屬性值都傳到后臺來(這也不算啥缺點,關(guān)系數(shù)據(jù)庫的更新也是這樣),并且不能修改cn的值(這就是為什么其他例子中都是使用uid來作為dn的一部分,類似于關(guān)系數(shù)據(jù)庫的主鍵的作用),因為修改后該entry的dn值就變化了,ODM就無法確定更新哪個數(shù)據(jù)。會報 javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object] 錯誤。

  2. 刪除操作也像關(guān)系數(shù)據(jù)庫的操作一樣,直接給cn即可,這是因為我們在person類中setCn()方法內(nèi)寫了dn的生成函數(shù),這樣ODM才能根據(jù)被@Id所注釋的dn來找到LDAP中的entry并執(zhí)行刪除操作。

  3. 我們在Person類中寫了Name類型的dn值的構(gòu)建方法,但是我一開始按照官網(wǎng)的代碼來寫,總是出問題,在stackOverFlow中找到了答案。鏈接在這里。

  4. 想要更深入的了解,可以參考翻譯的官方文檔。了解更自由更個性化的操作。

以上是“Spring Boot如何連接LDAP”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

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

AI