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

Spring Boot 連接LDAP的方法

 更新時間:2017年12月28日 09:25:47   作者:耳邊的火  
這篇文章主要介紹了Spring Boot 連接LDAP的方法,僅僅涉及基本的使用ODM來快速實現(xiàn)LDAP增刪改查操作。具有一定的參考價值,有興趣的可以了解一下

本文是Spring Boot系列文集中關于LDAP連接相關操作的一文。僅僅涉及基本的使用ODM來快速實現(xiàn)LDAP增刪改查操作。詳細的關于Spring LDAP的其他操作,可以參考翻譯的官方文檔。

本文目的:使用Spring Boot構建項目,幫助讀者快速配置并使用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

圖1

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

圖2

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

圖3

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

圖4

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

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

圖5

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

圖6

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

圖7

3.配置Spring LDAP連接信息

圖8

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

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

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

我們在項目中創(chuàng)建如下結構:

圖9

現(xiàn)在,我們在entry包下寫與entry互相映射的實體類。其中,我的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) {}構造方法中寫上了DN值的生成方法,在setCn中也寫上了該方法,當然存在代碼重復問題,忽略就好。

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)了,我們只要調用一下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,全選-復制-在console中粘貼-回車,如下圖:

圖11

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

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

圖12

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

圖13

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

圖14

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

圖15

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

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

圖16

我們查看LDAP中是否修改

圖17

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

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

 

圖18

查看LDAP中是否刪除

圖19

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

其他說明

  1. 剛才的例子中,代碼有需要完善的地方,但對于demo演示來說完全可以忍受。大家可能也看到了這么做也有些缺點,我在update的時候,需要將修改后的person的所有屬性值都傳到后臺來(這也不算啥缺點,關系數(shù)據(jù)庫的更新也是這樣),并且不能修改cn的值(這就是為什么其他例子中都是使用uid來作為dn的一部分,類似于關系數(shù)據(jù)庫的主鍵的作用),因為修改后該entry的dn值就變化了,ODM就無法確定更新哪個數(shù)據(jù)。會報 javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object] 錯誤。
  2. 刪除操作也像關系數(shù)據(jù)庫的操作一樣,直接給cn即可,這是因為我們在person類中setCn()方法內寫了dn的生成函數(shù),這樣ODM才能根據(jù)被@Id所注釋的dn來找到LDAP中的entry并執(zhí)行刪除操作。
  3. 我們在Person類中寫了Name類型的dn值的構建方法,但是我一開始按照官網(wǎng)的代碼來寫,總是出問題,在stackOverFlow中找到了答案。鏈接在這里。
  4. 想要更深入的了解,可以參考翻譯的官方文檔。了解更自由更個性化的操作。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring中@RestControllerAdvice注解的使用詳解

    Spring中@RestControllerAdvice注解的使用詳解

    這篇文章主要介紹了Spring中@RestControllerAdvice注解的使用詳解,@RestControllerAdvice是一個組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,需要的朋友可以參考下
    2024-01-01
  • 接口簽名怎么用Java實現(xiàn)

    接口簽名怎么用Java實現(xiàn)

    今天帶大家學習java的相關知識,文章圍繞怎么用Java實現(xiàn)接口簽名展開,文中有非常詳細的代碼示例及介紹,需要的朋友可以參考下
    2021-06-06
  • SpringBoot+Vue項目打包部署完整步驟教程

    SpringBoot+Vue項目打包部署完整步驟教程

    這篇文章主要介紹了SpringBoot+Vue項目打包部署的相關資料,包括Vue項目的打包設置、SpringBoot的配置修改、跨域問題處理、使用Nginx配置反向代理以及最終的項目啟動,教程假定開發(fā)者已具備完整的前后端分離項目和配置好環(huán)境的服務器,需要的朋友可以參考下
    2024-10-10
  • Java面向對象編程之繼承和多態(tài)以及包的解析與使用范例

    Java面向對象編程之繼承和多態(tài)以及包的解析與使用范例

    繼承就是可以直接使用前輩的屬性和方法。自然界如果沒有繼承,那一切都是處于混沌狀態(tài)。多態(tài)是同一個行為具有多個不同表現(xiàn)形式或形態(tài)的能力。多態(tài)就是同一個接口,使用不同的實例而執(zhí)行不同操作
    2021-11-11
  • java生成二維碼并且給二維碼添加logo

    java生成二維碼并且給二維碼添加logo

    這篇文章主要介紹了java生成二維碼并且給二維碼添加logo的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Spring AOP的概念與實現(xiàn)過程詳解

    Spring AOP的概念與實現(xiàn)過程詳解

    AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,可通過運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術。AOP是 Spring框架中的一個重要內容
    2023-02-02
  • java  中OkHttp的使用方法及實例

    java 中OkHttp的使用方法及實例

    這篇文章主要介紹了java 中OkHttp的使用方法及實例的相關資料,需要的朋友可以參考下
    2017-06-06
  • springboot+nacos+gateway實現(xiàn)灰度發(fā)布的實例詳解

    springboot+nacos+gateway實現(xiàn)灰度發(fā)布的實例詳解

    灰度發(fā)布是一種在軟件部署過程中用于平滑過渡的技術,通過引入灰度發(fā)布SDK和配置網(wǎng)關策略實現(xiàn),本文就來介紹一下,感興趣的可以了解一下
    2022-03-03
  • Spring?Boot應用中如何動態(tài)指定數(shù)據(jù)庫實現(xiàn)不同用戶不同數(shù)據(jù)庫的問題

    Spring?Boot應用中如何動態(tài)指定數(shù)據(jù)庫實現(xiàn)不同用戶不同數(shù)據(jù)庫的問題

    讓我們創(chuàng)建一個 Spring Boot 項目首先設置一個具有必要依賴項的新 Spring Boot項目,在項目配置中包括 Spring Web、Spring Data JPA 和關于數(shù)據(jù)庫的依賴項,接下來介紹Spring?Boot應用中如何動態(tài)指定數(shù)據(jù)庫,實現(xiàn)不同用戶不同數(shù)據(jù)庫的場景?,需要的朋友可以參考下
    2024-04-04
  • Java線程池必知必會知識點總結

    Java線程池必知必會知識點總結

    這篇文章主要給大家介紹了關于Java線程池必知必會知識點的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-02-02

最新評論