什么是 LDAP

LDAP(轻型目录访问协议)是一种软件协议 ,使任何人都可以在公共互联网或公司内网上查找网络中的组织,个人和其他资源(例如文件和设备)的数据

这里有更详细的介绍 百科信息

Spring中使用LDAP

可以按Spring文档,自己跑一下LDAP的流程,加深理解

首先使用springboot简单的创建一个web应用,这个很简单

一、创建一个HomeController

@RestController
public class HomeController {
  @GetMapping("/")
  public String index() {
    return "Welcome to the home page!";
  }
}

二、启动器代码

AuthenticatingLdapApplication

@SpringBootApplication
public class AuthenticatingLdapApplication {
  public static void main(String[] args) {
    SpringApplication.run(AuthenticatingLdapApplication.class, args);
  }
}

直接启动AuthenticatingLdapApplication后,就可以正常访问 http://localhost:8080!

三、安全启动

添加下面的maven依赖

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
		<groupId>org.springframework.ldap</groupId>
		<artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
		<groupId>com.unboundid</groupId>
		<artifactId>unboundid-ldapsdk</artifactId>
</dependency>

四、写一个安全配置类

WebSecurityConfig

@Configuration
public class WebSecurityConfig {
  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
      .authorizeHttpRequests((authorize) -> authorize
        .anyRequest().fullyAuthenticated()
      )
      .formLogin(Customizer.withDefaults());
    return http.build();
  }
  @Autowired
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .ldapAuthentication()
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource()
          .url("ldap://localhost:8389/dc=springframework,dc=org")
          .and()
        .passwordCompare()
          .passwordEncoder(new BCryptPasswordEncoder())
          .passwordAttribute("userPassword");
  }
}

解释下上面的代码:

auth.ldapAuthentication() 指定认证类型为 LDAP,通过 LDAP 查询用户信息和进行认证。

userDnPatterns("uid={0},ou=people") 定义用户的 DN(Distinguished Name)模板,{0} 是占位符,表示登录时用户输入的用户名,Spring Security 会根据此模板生成用户的完整 DN(用户的唯一标识,类似于数据库中的主键),然后在 LDAP 服务器中查找对应的用户条目。

groupSearchBase("ou=groups") 定义用户组搜索的基准路径,例如:用户组信息存储在 ou=groups 节点下,Spring Security 会从这里开始搜索用户所属的组。

ldap://localhost:8389 是 LDAP 服务器的地址和端口。dc=springframework,dc=org 是 LDAP 数据的根节点(即基础 DN,Base DN)

五、配置用户数据

新增IDIF配置数据:src/main/resources/test-server.ldif,IDIF格式的数据用于LDAP服务的数据交互格式。

dn: dc=springframework,dc=org
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: springframework

dn: ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: groups

dn: ou=subgroups,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: subgroups

dn: ou=people,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=ben,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Ben Alex
sn: Alex
uid: ben
userPassword: $2a$10$c6bSeWPhg06xB1lvmaWNNe4NROmZiSpYhlocU/98HNr2MhIOiSt36

dn: cn=developers,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: developers
ou: developer
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

目录树入下

dc=springframework,dc=org
├── ou=groups
│   ├── ou=subgroups
│   └── cn=developers
│       ├── ou: developer
│       └── uniqueMember: uid=ben,ou=people,dc=springframework,dc=org
└── ou=people
    └── uid=ben

使用文件并非标准方案,仅仅是为了进行测试目的。

六、添加配置引入文件

spring.ldap.embedded.ldif=classpath:test-server.ldif
spring.ldap.embedded.base-dn=dc=springframework,dc=org
spring.ldap.embedded.port=8389

到这里您就可以使用ldap验证用户名和密码了!