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

Spring LDAP目錄服務(wù)的使用示例

 更新時(shí)間:2025年04月13日 09:39:43   作者:程序媛學(xué)姐  
本文主要介紹了Spring LDAP目錄服務(wù)的使用示例

引言

在企業(yè)環(huán)境中,輕量級目錄訪問協(xié)議(LDAP)扮演著重要角色,作為集中式用戶管理和身份驗(yàn)證的標(biāo)準(zhǔn)協(xié)議。LDAP服務(wù)器存儲組織結(jié)構(gòu)化數(shù)據(jù),包括用戶、組織和權(quán)限信息。Spring LDAP是Spring家族的一個(gè)子項(xiàng)目,它簡化了Java應(yīng)用與LDAP服務(wù)器的交互過程。本文將深入探討Spring LDAP的核心概念、LdapTemplate的使用方法以及如何執(zhí)行常見的LDAP操作,幫助開發(fā)者有效地將LDAP集成到Spring應(yīng)用中。

一、Spring LDAP基礎(chǔ)

Spring LDAP提供了一個(gè)抽象層,使開發(fā)者能夠以Spring風(fēng)格的方式與LDAP交互,避免直接處理底層JNDI API的復(fù)雜性。它遵循與Spring JDBC相似的模板模式,通過LdapTemplate提供了簡潔的接口來執(zhí)行LDAP操作。

要開始使用Spring LDAP,首先需要添加相關(guān)依賴。對于Maven項(xiàng)目,可以在pom.xml中添加:

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
    <version>2.4.1</version>
</dependency>

<!-- 集成Spring Boot -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>

在Spring Boot項(xiàng)目中,配置LDAP連接信息可以在application.properties或application.yml中完成:

# LDAP服務(wù)器配置
spring.ldap.urls=ldap://ldap.example.com:389
spring.ldap.base=dc=example,dc=com
spring.ldap.username=cn=admin,dc=example,dc=com
spring.ldap.password=admin_password

二、LdapTemplate詳解

LdapTemplate是Spring LDAP的核心類,它封裝了LDAP操作的復(fù)雜性,提供了一套簡潔的API。在Spring Boot環(huán)境中,LdapTemplate會被自動配置,可以直接注入使用:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;

@Service
public class LdapService {
    
    private final LdapTemplate ldapTemplate;
    
    @Autowired
    public LdapService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    // 使用ldapTemplate執(zhí)行LDAP操作
}

如果不使用Spring Boot,則需要手動配置LdapTemplate:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;

@Configuration
public class LdapConfig {
    
    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl("ldap://ldap.example.com:389");
        contextSource.setBase("dc=example,dc=com");
        contextSource.setUserDn("cn=admin,dc=example,dc=com");
        contextSource.setPassword("admin_password");
        return contextSource;
    }
    
    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());
    }
}

LdapTemplate提供了多種方法來執(zhí)行LDAP操作,包括搜索、綁定、修改和刪除等。它還支持回調(diào)方法,允許開發(fā)者自定義結(jié)果處理邏輯。

三、LDAP對象映射

Spring LDAP提供了對象-目錄映射(ODM)功能,類似于ORM(對象-關(guān)系映射),可以將LDAP條目映射到Java對象。通過使用注解,可以輕松實(shí)現(xiàn)LDAP條目與Java類之間的轉(zhuǎn)換:

import org.springframework.ldap.odm.annotations.*;
import javax.naming.Name;

@Entry(base = "ou=people", objectClasses = {"person", "inetOrgPerson"})
public class User {
    
    @Id
    private Name id;
    
    @Attribute(name = "cn")
    private String commonName;
    
    @Attribute(name = "sn")
    private String surname;
    
    @Attribute(name = "mail")
    private String email;
    
    @Attribute(name = "telephoneNumber")
    private String phoneNumber;
    
    // Getters and setters
    public Name getId() {
        return id;
    }
    
    public void setId(Name id) {
        this.id = id;
    }
    
    public String getCommonName() {
        return commonName;
    }
    
    public void setCommonName(String commonName) {
        this.commonName = commonName;
    }
    
    // 其他getters和setters
}

在上面的例子中,@Entry注解定義了LDAP條目的基本信息,@Id注解標(biāo)記了條目的唯一標(biāo)識符,@Attribute注解將Java屬性映射到LDAP屬性。

四、基本LDAP操作

4.1 查詢操作

使用LdapTemplate進(jìn)行查詢是最常見的操作。可以使用各種方法來執(zhí)行搜索:

import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.Filter;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.stereotype.Service;

import javax.naming.directory.Attributes;
import java.util.List;

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public List<String> getAllUsernames() {
        return ldapTemplate.search(
            "ou=people", // 搜索基礎(chǔ)
            "(objectclass=person)", // 搜索過濾器
            (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get() // 屬性映射
        );
    }
    
    public List<User> findUserByEmail(String email) {
        Filter filter = new EqualsFilter("mail", email);
        return ldapTemplate.search(
            "ou=people",
            filter.encode(),
            (AttributesMapper<User>) attrs -> {
                User user = new User();
                user.setCommonName((String) attrs.get("cn").get());
                user.setSurname((String) attrs.get("sn").get());
                user.setEmail((String) attrs.get("mail").get());
                return user;
            }
        );
    }
}

使用ODM功能,可以直接將搜索結(jié)果映射到Java對象:

import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.ldap.query.LdapQueryBuilder;

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public List<User> findUserByEmail(String email) {
        LdapQuery query = LdapQueryBuilder.query()
            .base("ou=people")
            .where("objectclass").is("person")
            .and("mail").is(email);
        
        return ldapTemplate.find(query, User.class);
    }
}

4.2 添加操作

添加新條目可以通過直接創(chuàng)建對象然后使用LdapTemplate的create方法:

import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;

import javax.naming.Name;

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public void createUser(String username, String surname, String email) {
        Name dn = LdapNameBuilder.newInstance()
            .add("ou", "people")
            .add("cn", username)
            .build();
        
        DirContextAdapter context = new DirContextAdapter(dn);
        
        context.setAttributeValues("objectclass", new String[]{"top", "person", "inetOrgPerson"});
        context.setAttributeValue("cn", username);
        context.setAttributeValue("sn", surname);
        context.setAttributeValue("mail", email);
        
        ldapTemplate.bind(context);
    }
}

使用ODM功能,可以更簡單地創(chuàng)建和保存對象:

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public void createUser(String username, String surname, String email) {
        User user = new User();
        user.setId(LdapNameBuilder.newInstance()
            .add("cn", username)
            .build());
        user.setCommonName(username);
        user.setSurname(surname);
        user.setEmail(email);
        
        ldapTemplate.create(user);
    }
}

4.3 修改操作

修改現(xiàn)有條目可以通過查找條目,修改屬性,然后更新:

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public void updateUserEmail(String username, String newEmail) {
        Name dn = LdapNameBuilder.newInstance()
            .add("ou", "people")
            .add("cn", username)
            .build();
        
        DirContextOperations context = ldapTemplate.lookupContext(dn);
        context.setAttributeValue("mail", newEmail);
        
        ldapTemplate.modifyAttributes(context);
    }
}

使用ODM功能:

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public void updateUserEmail(String username, String newEmail) {
        LdapQuery query = LdapQueryBuilder.query()
            .base("ou=people")
            .where("cn").is(username);
        
        User user = ldapTemplate.findOne(query, User.class);
        if (user != null) {
            user.setEmail(newEmail);
            ldapTemplate.update(user);
        }
    }
}

4.4 刪除操作

刪除條目的操作比較簡單:

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public void deleteUser(String username) {
        Name dn = LdapNameBuilder.newInstance()
            .add("ou", "people")
            .add("cn", username)
            .build();
        
        ldapTemplate.unbind(dn);
    }
}

使用ODM功能:

@Service
public class UserService {
    
    private final LdapTemplate ldapTemplate;
    
    public UserService(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
    
    public void deleteUser(String username) {
        LdapQuery query = LdapQueryBuilder.query()
            .base("ou=people")
            .where("cn").is(username);
        
        User user = ldapTemplate.findOne(query, User.class);
        if (user != null) {
            ldapTemplate.delete(user);
        }
    }
}

五、認(rèn)證與授權(quán)

Spring LDAP可以與Spring Security集成,實(shí)現(xiàn)基于LDAP的認(rèn)證和授權(quán):

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
            .userDnPatterns("cn={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource()
            .url("ldap://ldap.example.com:389/dc=example,dc=com")
            .and()
            .passwordCompare()
            .passwordAttribute("userPassword");
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/user/**").hasRole("USER")
            .anyRequest().authenticated()
            .and()
            .formLogin();
    }
    
    @Bean
    public PasswordEncoder passwordEncoder() {
        // 注意:生產(chǎn)環(huán)境不應(yīng)使用NoOpPasswordEncoder
        return NoOpPasswordEncoder.getInstance();
    }
}

六、高級特性與最佳實(shí)踐

Spring LDAP提供了一些高級特性,如分頁查詢、排序和連接池配置,這些對于處理大型目錄服務(wù)尤為重要:

// 配置連接池
@Bean
public LdapContextSource contextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl("ldap://ldap.example.com:389");
    contextSource.setBase("dc=example,dc=com");
    contextSource.setUserDn("cn=admin,dc=example,dc=com");
    contextSource.setPassword("admin_password");
    
    // 連接池配置
    contextSource.setPooled(true);
    
    return contextSource;
}

@Bean
public PoolingContextSource poolingContextSource(LdapContextSource contextSource) {
    DefaultTlsDirContextAuthenticationStrategy strategy = new DefaultTlsDirContextAuthenticationStrategy();
    strategy.setHostnameVerifier((hostname, session) -> true);
    contextSource.setAuthenticationStrategy(strategy);
    
    PoolConfig poolConfig = new PoolConfig();
    poolConfig.setMinIdle(5);
    poolConfig.setMaxTotal(20);
    poolConfig.setMaxIdle(10);
    
    PoolingContextSource poolingContextSource = new PoolingContextSource();
    poolingContextSource.setContextSource(contextSource);
    poolingContextSource.setPoolConfig(poolConfig);
    
    return poolingContextSource;
}

// 分頁查詢示例
public List<User> findUsersPaged(int pageSize, int pageNumber) {
    PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(pageSize);
    LdapQuery query = LdapQueryBuilder.query()
        .base("ou=people")
        .where("objectclass").is("person");
    
    // 執(zhí)行第一頁查詢
    List<User> users = new ArrayList<>();
    for (int i = 0; i < pageNumber; i++) {
        users = ldapTemplate.search(query, new PersonAttributesMapper(), processor);
        
        // 如果沒有更多結(jié)果或者已經(jīng)到達(dá)請求的頁碼,則停止
        if (!processor.hasMore() || i == pageNumber - 1) {
            break;
        }
        
        // 設(shè)置cookie以獲取下一頁
        processor.updateCookie();
    }
    
    return users;
}

總結(jié)

Spring LDAP為開發(fā)者提供了一個(gè)強(qiáng)大且靈活的框架,簡化了與LDAP目錄服務(wù)的交互。通過LdapTemplate,開發(fā)者可以輕松執(zhí)行各種LDAP操作,而無需深入了解底層JNDI API的復(fù)雜性。對象-目錄映射功能讓LDAP條目與Java對象的轉(zhuǎn)換變得簡單直觀,提高了代碼的可讀性和可維護(hù)性。與Spring Security的集成使得實(shí)現(xiàn)基于LDAP的身份驗(yàn)證和授權(quán)變得輕而易舉。在企業(yè)應(yīng)用中,特別是需要集中式用戶管理的場景下,Spring LDAP是一個(gè)理想的選擇。

到此這篇關(guān)于Spring LDAP目錄服務(wù)的使用示例的文章就介紹到這了,更多相關(guān)Spring LDAP目錄服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式

    SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式

    這篇文章給大家介紹了SpringBoot根據(jù)各地區(qū)時(shí)間設(shè)置接口有效時(shí)間的實(shí)現(xiàn)方式,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫分表的方案

    SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫分表的方案

    實(shí)現(xiàn)億級數(shù)據(jù)量分庫分表的項(xiàng)目是一個(gè)挑戰(zhàn)性很高的任務(wù),下面是一個(gè)基于Spring Boot的簡單實(shí)現(xiàn)方案,感興趣的朋友一起看看吧
    2024-03-03
  • Java 條件控制與循環(huán)控制實(shí)例

    Java 條件控制與循環(huán)控制實(shí)例

    下面小編就為大家?guī)硪黄狫ava 條件控制與循環(huán)控制實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • JAVA實(shí)現(xiàn)KMP算法理論和示例代碼

    JAVA實(shí)現(xiàn)KMP算法理論和示例代碼

    本文從理論到代碼講解了JAVA對KMP算法的實(shí)現(xiàn),大家可以參考一下
    2013-11-11
  • Java Web制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析

    Java Web制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析

    這篇文章主要介紹了Java Web制作登錄驗(yàn)證碼實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java報(bào)錯Java.text.ParseException的解決方法匯總

    Java報(bào)錯Java.text.ParseException的解決方法匯總

    在Java開發(fā)的復(fù)雜世界中,錯誤處理是開發(fā)者必須面對的關(guān)鍵挑戰(zhàn)之一,其中,Java.text.ParseException就像一個(gè)隱藏在代碼叢林中的陷阱,常常讓開發(fā)者們陷入困惑,本文給大家介紹了Java報(bào)錯Java.text.ParseException的解決方法,需要的朋友可以參考下
    2024-10-10
  • Java ShardingJDBC實(shí)戰(zhàn)演練

    Java ShardingJDBC實(shí)戰(zhàn)演練

    Sharding-JDBC 采用在 JDBC 協(xié)議層擴(kuò)展分庫分表,是一個(gè)以 jar 形式提供服務(wù)的輕量級組件,其核心思路是小而美地完成最核心的事情
    2021-11-11
  • Java ThreadLocal類使用詳解

    Java ThreadLocal類使用詳解

    這篇文章主要介紹了Java ThreadLocal類詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Java中的@Builder注解問題詳解

    Java中的@Builder注解問題詳解

    這篇文章主要介紹了Java中的@Builder注解詳解,@Builder 注解的其中一個(gè)大坑會導(dǎo)致默認(rèn)值失效,這是使用此注解出現(xiàn)的一個(gè)問題,總的來說,不推薦再使用 @Builder 注解,接下來講重點(diǎn)介紹其原因和替代方案,需要的朋友可以參考下
    2023-10-10
  • SpringMVC使用自定義驗(yàn)證器進(jìn)行數(shù)據(jù)驗(yàn)證的方法

    SpringMVC使用自定義驗(yàn)證器進(jìn)行數(shù)據(jù)驗(yàn)證的方法

    SpringMVC?提供了強(qiáng)大的數(shù)據(jù)驗(yàn)證機(jī)制,可以方便地驗(yàn)證表單提交的數(shù)據(jù),除了自帶的驗(yàn)證器之外,SpringMVC?還支持自定義驗(yàn)證器,允許開發(fā)者根據(jù)業(yè)務(wù)需求自定義驗(yàn)證規(guī)則,本文將介紹如何在?SpringMVC?中使用自定義驗(yàn)證器
    2023-07-07

最新評論