spring 使用嵌入式 Ldap 进行身份验证

spring authentication with Embedded Ldap

我正在尝试将 spring 身份验证与嵌入式 ldap 集成。

我在本地 ldif 文件中有用户信息。

用户 1

 dn: uid=joe,ou=otherpeople,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Joe Smeth
sn: Smeth
uid: joe
userPassword: joespassword

用户 2

dn: uid=bob,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Bob Hamilton
sn: Hamilton
uid: bob
userPassword: bobspassword

Spring 网络安全配置文件

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
            .formLogin();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth
            .ldapAuthentication()
                .userDnPatterns("uid={0},ou=people")
                .contextSource()
                    .url("ldap://localhost:8389/dc=springframework,dc=org")
                    .and()
                .passwordCompare()
                    .passwordAttribute("userPassword");
   }
}

userDnPattern 在配置文件中我使用了 ou=people (uid={0},ou=people) 所以我能够验证 bob。 说到joe他的目录路径就不同了。 所以我无法使用 joe 的用户名和密码登录。

无论目录结构如何,我的Spring验证所有用户的配置应该是什么?

使用 userSearchFilter.

对 DIT(目录信息树)中的任何用户进行身份验证

Spring配置为,

auth.ldapAuthentication()
            .userSearchFilter("(uid={0})")
                    .contextSource()
                        .url("ldap://localhost:8389/dc=springframework,dc=org")
                        .and()
                    .passwordCompare()        
                .passwordAttribute("userPassword");

感谢@EricLavault