最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)對(duì)ldap配置的說明來配置,可以看這里。這樣配置之后,spring boot會(huì)自動(dòng)讀取該配置。

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

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

我們?cè)陧?xiàng)目中創(chuàng)建如下結(jié)構(gòu):

圖9

現(xiàn)在,我們?cè)趀ntry包下寫與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類來測(cè)試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完成啦。下面我們測(cè)試一下

測(cè)試

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

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

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

圖11

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

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

圖12

正如controller層的testController要求的那樣,我們?cè)诘刂?/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. 剛才的例子中,代碼有需要完善的地方,但對(duì)于demo演示來說完全可以忍受。大家可能也看到了這么做也有些缺點(diǎn),我在update的時(shí)候,需要將修改后的person的所有屬性值都傳到后臺(tái)來(這也不算啥缺點(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)槲覀冊(cè)趐erson類中setCn()方法內(nèi)寫了dn的生成函數(shù),這樣ODM才能根據(jù)被@Id所注釋的dn來找到LDAP中的entry并執(zhí)行刪除操作。
  3. 我們?cè)赑erson類中寫了Name類型的dn值的構(gòu)建方法,但是我一開始按照官網(wǎng)的代碼來寫,總是出問題,在stackOverFlow中找到了答案。鏈接在這里。
  4. 想要更深入的了解,可以參考翻譯的官方文檔。了解更自由更個(gè)性化的操作。

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

相關(guān)文章

  • SpringSession會(huì)話管理之Redis與JDBC存儲(chǔ)實(shí)現(xiàn)方式

    SpringSession會(huì)話管理之Redis與JDBC存儲(chǔ)實(shí)現(xiàn)方式

    本文將詳細(xì)介紹Spring Session的核心概念、特性以及如何使用Redis和JDBC來實(shí)現(xiàn)會(huì)話存儲(chǔ),幫助開發(fā)者構(gòu)建更加健壯和可擴(kuò)展的應(yīng)用系統(tǒng),希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java之Arrays的各種功能和用法總結(jié)

    Java之Arrays的各種功能和用法總結(jié)

    數(shù)組在?Java?中是一種常用的數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)和操作大量數(shù)據(jù)。Arrays?是我們?cè)谔幚頂?shù)組時(shí)的一把利器。它提供了豐富的方法和功能,使得數(shù)組操作變得更加簡單、高效和可靠。接下來我們一起看看?Arrays?的各種功能和用法,,需要的朋友可以參考下
    2023-05-05
  • SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)

    SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)

    Prometheus作為一個(gè)開源的監(jiān)控和告警工具,以其強(qiáng)大的數(shù)據(jù)采集、存儲(chǔ)和查詢能力,受到了眾多開發(fā)者的青睞,本文主要介紹了SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn),感興趣的可以了解一下
    2024-07-07
  • 如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作

    如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作

    本文主要介紹了如何使用MyBatis框架實(shí)現(xiàn)增刪改查(CRUD)操作。首先介紹了MyBatis框架的基本概念和使用方法,然后分別介紹了如何使用MyBatis實(shí)現(xiàn)增刪改查操作。最后,通過一個(gè)簡單的示例演示了如何使用MyBatis框架實(shí)現(xiàn)CRUD操作。
    2023-05-05
  • Java讀取txt文件的方法

    Java讀取txt文件的方法

    這篇文章主要為大家詳細(xì)介紹了Java讀取txt文件的方法,分享Java讀取txt文件代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Maven 主模塊和子模塊pom.xml依賴聲明

    Maven 主模塊和子模塊pom.xml依賴聲明

    這篇文章主要介紹了Maven 主模塊和子模塊pom.xml依賴聲明,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java 中限制方法的返回時(shí)間最新方法

    Java 中限制方法的返回時(shí)間最新方法

    最近在研究 ChatGPT 的 API 調(diào)用,因?yàn)?nbsp;ChatGPT 的 API 調(diào)用時(shí)間通常超過 30 秒,所以我們希望在程序中限制這個(gè)方法的執(zhí)行時(shí)間,不要讓方法花太長時(shí)間去執(zhí)行了,今天通過本文給大家分享Java 中如何限制方法的返回時(shí)間,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • ReentrantLock源碼詳解--公平鎖、非公平鎖

    ReentrantLock源碼詳解--公平鎖、非公平鎖

    ReentrantLock重入鎖,是實(shí)現(xiàn)Lock接口的一個(gè)類,也是在實(shí)際編程中使用頻率很高的一個(gè)鎖,表示能夠?qū)蚕碣Y源能夠重復(fù)加鎖,即當(dāng)前線程獲取該鎖再次獲取不會(huì)被阻塞。下面我們來深入了解一下它吧
    2019-06-06
  • mybatis和mybatis-plus設(shè)置值為null不起作用問題及解決

    mybatis和mybatis-plus設(shè)置值為null不起作用問題及解決

    Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查詢時(shí)對(duì)空值的處理策略,通過配置不同的策略類型,可以靈活地處理實(shí)體對(duì)象的空值問題
    2025-02-02
  • java開發(fā)模式的深度研究

    java開發(fā)模式的深度研究

    下面小編就為大家?guī)硪黄钊肜斫鈐ava工廠模式。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-07-07

最新評(píng)論

临朐县| 武功县| 疏附县| 乌鲁木齐县| 邢台市| 罗田县| 田林县| 延川县| 贺兰县| 阿勒泰市| 拉萨市| 宝清县| 北辰区| 淄博市| 滨海县| 射阳县| 新闻| 斗六市| 远安县| 普陀区| 乾安县| 松桃| 正阳县| 崇文区| 榆中县| 通化市| 理塘县| 阳城县| 东阳市| 松江区| 天峨县| 招远市| 贵阳市| 东港市| 秭归县| 大庆市| 凤城市| 宝应县| 巴彦淖尔市| 景泰县| 得荣县|