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

Spring Boot 連接LDAP的方法

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

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

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

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

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

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

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

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

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

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

IDEA中點(diǎn)擊file - new - project

圖1

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

圖2

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

圖3

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

圖4

如圖4,這里自己命名即可,點(diǎn)擊finish。

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

圖5

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

圖6

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

圖7

3.配置Spring LDAP連接信息

圖8

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

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

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

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

圖9

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

圖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傳給前端時(shí)不報(bào)錯(cuò),因?yàn)镹ame類型的無法自動(dòng)解析成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);
 }

}

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

6.編寫controller層

在controller包下,新建一個(gè)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);
 }

}

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

測試

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

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

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

圖11

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

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

圖12

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

圖13

而在LDAP服務(wù)器中,也顯示了新增的數(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演示來說完全可以忍受。大家可能也看到了這么做也有些缺點(diǎn),我在update的時(shí)候,需要將修改后的person的所有屬性值都傳到后臺來(這也不算啥缺點(diǎn),關(guān)系數(shù)據(jù)庫的更新也是這樣),并且不能修改cn的值(這就是為什么其他例子中都是使用uid來作為dn的一部分,類似于關(guān)系數(shù)據(jù)庫的主鍵的作用),因?yàn)樾薷暮笤揺ntry的dn值就變化了,ODM就無法確定更新哪個(gè)數(shù)據(jù)。會(huì)報(bào) javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object] 錯(cuò)誤。
  2. 刪除操作也像關(guān)系數(shù)據(jù)庫的操作一樣,直接給cn即可,這是因?yàn)槲覀冊趐erson類中setCn()方法內(nèi)寫了dn的生成函數(shù),這樣ODM才能根據(jù)被@Id所注釋的dn來找到LDAP中的entry并執(zhí)行刪除操作。
  3. 我們在Person類中寫了Name類型的dn值的構(gòu)建方法,但是我一開始按照官網(wǎng)的代碼來寫,總是出問題,在stackOverFlow中找到了答案。鏈接在這里。
  4. 想要更深入的了解,可以參考翻譯的官方文檔。了解更自由更個(gè)性化的操作。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

    Spring中@RestControllerAdvice注解的使用詳解

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

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

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

    SpringBoot+Vue項(xiàng)目打包部署完整步驟教程

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

    Java面向?qū)ο缶幊讨^承和多態(tài)以及包的解析與使用范例

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

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

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

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

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

    java 中OkHttp的使用方法及實(shí)例

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

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

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

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

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

    Java線程池必知必會(huì)知識點(diǎn)總結(jié)

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

最新評論