嵌入式Ldap的Spring身份验证

时间:2019-12-15 14:19:35

标签: spring authentication ldap openldap

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

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

User1

 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 WebsecurityConfigFile

@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的用户名和密码登录。

enter image description here

我的SpringConfiguration应该如何验证所有用户,而与目录结构无关?

1 个答案:

答案 0 :(得分:1)

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

Spring配置是

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

感谢@EricLavault